diff --git a/README.md b/README.md index bd1a8ab..ef6bbb8 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Check out the [Budget Manual](https://github.com/n3d1117/chatgpt-telegram-bot/di | `SPOTIFY_CLIENT_ID` | Spotify app Client ID (required for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | `-` | | `SPOTIFY_CLIENT_SECRET` | Spotify app Client Secret (required for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | `-` | | `SPOTIFY_REDIRECT_URI` | Spotify app Redirect URI (required for the `spotify` plugin, you can find it on the [dashboard](https://developer.spotify.com/dashboard/)) | `-` | +| `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)) | `-` | #### Available plugins | Name | Description | Required API key(s) | @@ -119,6 +120,7 @@ Check out the [Budget Manual](https://github.com/n3d1117/chatgpt-telegram-bot/di | `crypto` | Live cryptocurrencies rate (powered by [CoinCap](https://coincap.io)) - by [@stumpyfr](https://github.com/stumpyfr) | `-` | | `spotify` | Spotify top tracks/artists, currently playing song and content search (powered by [Spotify](https://spotify.com)). Requires one-time authorization. | `SPOTIFY_CLIENT_ID`, `SPOTIFY_CLIENT_SECRET`, `SPOTIFY_REDIRECT_URI` | | `translate` | Translate text to any language (powered by [DuckDuckGo](https://duckduckgo.com)) | `-` | +| `deepl` | Translate text to any language (powered by [DeepL](https://deepl.com)) | `DEEPL_API_KEY` | | `image_search` | Search image or GIF (powered by [DuckDuckGo](https://duckduckgo.com)) | `-` | Check out the [official API reference](https://platform.openai.com/docs/api-reference/chat) for more details. diff --git a/bot/plugin_manager.py b/bot/plugin_manager.py index 2180565..9eaf327 100644 --- a/bot/plugin_manager.py +++ b/bot/plugin_manager.py @@ -7,6 +7,7 @@ from plugins.crypto import CryptoPlugin from plugins.weather import WeatherPlugin from plugins.web_search import WebSearchPlugin from plugins.wolfram_alpha import WolframAlphaPlugin +from plugins.deepl import DeeplTranslatePlugin class PluginManager: @@ -23,6 +24,7 @@ class PluginManager: 'spotify': SpotifyPlugin(), 'translate': TranslatePlugin(), 'image_search': ImageSearchPlugin(), + 'deepl': DeeplTranslatePlugin() } self.plugins = [plugin_mapping[plugin] for plugin in enabled_plugins] diff --git a/bot/plugins/deepl.py b/bot/plugins/deepl.py new file mode 100644 index 0000000..0ed655a --- /dev/null +++ b/bot/plugins/deepl.py @@ -0,0 +1,40 @@ +import os +from typing import Dict + +import deepl + +from .plugin import Plugin + + +class DeeplTranslatePlugin(Plugin): + """ + A plugin to translate a given text from a language to another, using DeepL + """ + + def __init__(self): + deepl_api_key = os.getenv('DEEPL_API_KEY') + if not deepl_api_key: + raise ValueError('DEEPL_API_KEY environment variable must be set to use DeeplPlugin') + self.api_key = deepl_api_key + + def get_source_name(self) -> str: + return "DeepL Translate" + + def get_spec(self) -> [Dict]: + return [{ + "name": "translate", + "description": "Translate a given text from a language to another", + "parameters": { + "type": "object", + "properties": { + "text": {"type": "string", "description": "The text to translate"}, + "to_language": {"type": "string", "description": "The language to translate to (e.g. 'it')"} + }, + "required": ["text", "to_language"], + }, + }] + + async def execute(self, function_name, **kwargs) -> Dict: + translator = deepl.Translator(self.api_key) + answer = translator.translate_text(kwargs['text'], target_lang=kwargs['to_language']) + return answer.text diff --git a/requirements.txt b/requirements.txt index 7070ed3..d1e32b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ requests~=2.31.0 tenacity==8.2.2 wolframalpha==5.0.0 duckduckgo_search==3.8.3 -spotipy==2.23.0 \ No newline at end of file +spotipy==2.23.0 +deepl>=1.15.0 \ No newline at end of file