Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CoinMarketCap plugin #648

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ ALLOWED_TELEGRAM_USER_IDS=USER_ID_1,USER_ID_2
# TTS_PRICES=0.015,0.030
# BOT_LANGUAGE=en
# ENABLE_VISION_FOLLOW_UP_QUESTIONS="true"
# VISION_MODEL="gpt-4-vision-preview"
# VISION_MODEL="gpt-4-vision-preview"
# COINMARKETCAP_KEY="YOUR-API-KEY"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Check out the [official API reference](https://platform.openai.com/docs/api-refe
| `whois` | Query the whois domain database - by [@jnaskali](https://github.com/jnaskali) | - | `whois` |
| `webshot` | Screenshot a website from a given url or domain name - by [@noriellecruz](https://github.com/noriellecruz) | - | |
| `auto_tts` | Text to speech using OpenAI APIs - by [@Jipok](https://github.com/Jipok) | - | |
| `coinmarketcap` | The largest selection of current cryptocurrency rates - by [@zchk0](https://github.com/zchk0) | `COINMARKETCAP_KEY` | |

#### Environment variables
| Variable | Description | Default value |
Expand All @@ -164,6 +165,7 @@ Check out the [official API reference](https://platform.openai.com/docs/api-refe
| `WORLDTIME_DEFAULT_TIMEZONE` | Default timezone to use, i.e. `Europe/Rome` (required only for the `worldtimeapi` plugin, you can get TZ Identifiers from [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)) | - |
| `DUCKDUCKGO_SAFESEARCH` | DuckDuckGo safe search (`on`, `off` or `moderate`) (optional, applies to `ddg_web_search` and `ddg_image_search`) | `moderate` |
| `DEEPL_API_KEY` | DeepL API key (required for the `deepl` plugin, you can get one [here](https://www.deepl.com/pro-api?cta=header-pro-api)) | - |
| `COINMARKETCAP_KEY` | CoinMarketCap API key (required for the `coinmarketcap` plugin, you can get one [here](https://coinmarketcap.com/api/documentation/v1/)) | - |

### Installing
Clone the repository and navigate to the project directory:
Expand Down
2 changes: 2 additions & 0 deletions bot/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from plugins.ddg_image_search import DDGImageSearchPlugin
from plugins.ddg_translate import DDGTranslatePlugin
from plugins.spotify import SpotifyPlugin
from plugins.coinmarketcap import CoinMarketCap
from plugins.crypto import CryptoPlugin
from plugins.weather import WeatherPlugin
from plugins.ddg_web_search import DDGWebSearchPlugin
Expand All @@ -29,6 +30,7 @@ def __init__(self, config):
'wolfram': WolframAlphaPlugin,
'weather': WeatherPlugin,
'crypto': CryptoPlugin,
'coinmarketcap': CoinMarketCap,
'ddg_web_search': DDGWebSearchPlugin,
'ddg_translate': DDGTranslatePlugin,
'ddg_image_search': DDGImageSearchPlugin,
Expand Down
54 changes: 54 additions & 0 deletions bot/plugins/coinmarketcap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
from typing import Dict

import requests

from .plugin import Plugin


# Author: https://github.com/zchk0
class CoinMarketCap(Plugin):
"""
A plugin to fetch the current rate of various cryptocurrencies
"""
def get_source_name(self) -> str:
return "CoinMarketCap by zchk0"

def get_spec(self) -> [Dict]:
return [{
"name": "get_crypto_rate",
"description": "Get the current rate of various cryptocurrencies from coinmarketcap",
"parameters": {
"type": "object",
"properties": {
"asset": {"type": "string", "description": "Ticker of the cryptocurrency in uppercase (e.g., BTC, ETH, XRP)"}
},
"required": ["asset"],
},
}]

def get_crypto_price(self, asset):
headers = {
'X-CMC_PRO_API_KEY': os.environ.get('COINMARKETCAP_KEY', '')
}
params = {
'symbol': asset,
'convert': 'USD'
}
try:
response = requests.get("https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest", headers=headers, params=params)
response.raise_for_status()
data = response.json().get('data', {})
if asset in data:
price = data[asset]['quote']['USD']['price']
return price
else:
return "Not found"
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None

async def execute(self, function_name, helper, **kwargs) -> dict:
asset = kwargs.get('asset', '')
rate = self.get_crypto_price(asset)
return {"asset": asset, "rate": rate}
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
python-dotenv~=1.0.0
pydub~=0.25.1
tiktoken==0.7.0
openai==1.29.0
python-telegram-bot==21.1.1
tiktoken==0.8.0
openai==1.54.3
python-telegram-bot==21.7
requests~=2.31.0
tenacity==8.3.0
wolframalpha~=5.0.0
Expand Down