added deepl plugin

This commit is contained in:
Bacer
2023-06-27 22:56:31 +03:00
parent 00cd509683
commit 6d32ae916c
4 changed files with 46 additions and 1 deletions

View File

@@ -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_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_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/)) | `-` | | `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 #### Available plugins
| Name | Description | Required API key(s) | | 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) | `-` | | `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` | | `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)) | `-` | | `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)) | `-` | | `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. Check out the [official API reference](https://platform.openai.com/docs/api-reference/chat) for more details.

View File

@@ -7,6 +7,7 @@ from plugins.crypto import CryptoPlugin
from plugins.weather import WeatherPlugin from plugins.weather import WeatherPlugin
from plugins.web_search import WebSearchPlugin from plugins.web_search import WebSearchPlugin
from plugins.wolfram_alpha import WolframAlphaPlugin from plugins.wolfram_alpha import WolframAlphaPlugin
from plugins.deepl import DeeplTranslatePlugin
class PluginManager: class PluginManager:
@@ -23,6 +24,7 @@ class PluginManager:
'spotify': SpotifyPlugin(), 'spotify': SpotifyPlugin(),
'translate': TranslatePlugin(), 'translate': TranslatePlugin(),
'image_search': ImageSearchPlugin(), 'image_search': ImageSearchPlugin(),
'deepl': DeeplTranslatePlugin()
} }
self.plugins = [plugin_mapping[plugin] for plugin in enabled_plugins] self.plugins = [plugin_mapping[plugin] for plugin in enabled_plugins]

40
bot/plugins/deepl.py Normal file
View File

@@ -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

View File

@@ -8,3 +8,4 @@ tenacity==8.2.2
wolframalpha==5.0.0 wolframalpha==5.0.0
duckduckgo_search==3.8.3 duckduckgo_search==3.8.3
spotipy==2.23.0 spotipy==2.23.0
deepl>=1.15.0