Ps. This tutorial is for the #AI Chatbot Design under the #AI-chatbot writing contest by Coze and HackerNoon!
Click the Plugins tab, and click Create Plug-in. Alternatively, you can visit the agent's Develop page and create Plugins.
Select Cloud Plugin - Create in Coze IDE under the Plugin Tool creation method dropdown.
Select Python as your language of choice under IDE runtime.
Click Confirm to be redirected to the Plugin page to create your tool.
Click Create tool in IDE on the Plugin page.
Click Confirm to be redirected to the Coze IDE page, where you can code your tool.
Click the + icon on the Packages panel on your left to add dependencies. Here, you need to install the requests package so we can send HTTP.
from runtime import Args
import requests
API_KEY = "****"
BASE_URL = "//api.coingecko.com/api/v3"
def get_coin_data(coin_id: str, currency: str = "usd", logger=None):
endpoint = f"{BASE_URL}/coins/markets"
params = {
"vs_currency": currency,
"ids": coin_id.lower(), # Convert to lowercase
"x_cg_demo_api_key": API_KEY
}
try:
response = requests.get(endpoint, params=params)
response.raise_for_status() # Raise an exception for bad status codes
data = response.json()
if logger:
logger.info(f"API Response: {data}")
return data
except requests.RequestException as e:
if logger:
logger.error(f"API Request failed: {str(e)}")
return {"error": f"API request failed: {str(e)}"}
def handler(args: Args) -> dict:
args.logger.info(f"Received args: {args}")
args.logger.info(f"Type of args.input: {type(args.input)}")
args.logger.info(f"Content of args.input: {args.input}")
# Handle CustomNamespace object
if hasattr(args.input, 'coin_id') and hasattr(args.input, 'currency'):
coin_id = getattr(args.input, 'coin_id', 'bitcoin')
currency = getattr(args.input, 'currency', 'usd')
else:
return {
"message": f"Error: Invalid input format. Expected CustomNamespace with coin_id and currency attributes. Input: {args.input}",
"data": None
}
args.logger.info(f"Processed input - coin_id: {coin_id}, currency: {currency}")
try:
coin_data = get_coin_data(coin_id, currency, args.logger)
if "error" in coin_data:
return {
"message": f"Error: {coin_data['error']}",
"data": None
}
else:
# Check if we got any data
if not coin_data:
return {
"message": f"No data found for {coin_id}",
"data": None
}
# Assuming the API returns a list with one item for the specified coin
coin_info = coin_data[0] if coin_data else {}
return {
"message": f"Successfully retrieved data for {coin_id}",
"data": {
"name": coin_info.get("name"),
"symbol": coin_info.get("symbol"),
"current_price": coin_info.get("current_price"),
"market_cap": coin_info.get("market_cap"),
"price_change_24h": coin_info.get("price_change_24h")
}
}
except Exception as e:
args.logger.error(f"An error occurred: {str(e)}")
return {
"message": f"An error occurred while processing the request: {str(e)}",
"data": None
}
Input parameters: coin_id (string): The ID of the cryptocurrency (e.g., "bitcoin", "ethereum").
Input parameters: currency (string): The target currency for market data (e.g., "usd", "eur")
Output parameters: message (string): A message indicating the request's status.
Output parameters: data (object): Within this data object, use these parameters.
Within your data output parameters: name (string): The name of the cryptocurrency.
Within your data output parameters: symbol (string): The symbol of the cryptocurrency.
Within your data output parameters: current_price (number): The current price in the specified currency.
Within your data output parameters: market_cap (number): The market capitalization in the specified currency.
Within your data output parameters: price_change_24h (number): The price change in the last 24 hours
Input your input parameter under the Test Code tab to test, and run your plugin.
Click Run.
View the Output Value to view the response of your tool.
View the logs in the Console panel to see if it was successful or any error.
Click Publish to publish your plugin if the test is successful.
Tick No in the Privacy Collection Statement dialog box, as our plugin does not collect user data. However, if your plugin requires user data, tick Yes.
Click Publish. Head over to the store and explore this .