mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-22 07:04:59 +01:00
fix bugs
This commit is contained in:
@@ -13,9 +13,11 @@ class DeeplTranslatePlugin(Plugin):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
deepl_api_key = os.getenv('DEEPL_API_KEY')
|
deepl_api_key = os.getenv('DEEPL_API_KEY')
|
||||||
if not deepl_api_key:
|
deepl_api_pro = os.getenv('DEEPL_API_PRO', 'false').lower() == 'true'
|
||||||
raise ValueError('DEEPL_API_KEY environment variable must be set to use DeeplPlugin')
|
if not deepl_api_key or not deepl_api_pro:
|
||||||
|
raise ValueError('DEEPL_API_KEY and DEEPL_API_PLAN environment variable must be set to use DeepL Plugin')
|
||||||
self.api_key = deepl_api_key
|
self.api_key = deepl_api_key
|
||||||
|
self.api_pro = deepl_api_pro
|
||||||
|
|
||||||
def get_source_name(self) -> str:
|
def get_source_name(self) -> str:
|
||||||
return "DeepL Translate"
|
return "DeepL Translate"
|
||||||
@@ -35,13 +37,20 @@ class DeeplTranslatePlugin(Plugin):
|
|||||||
}]
|
}]
|
||||||
|
|
||||||
async def execute(self, function_name, **kwargs) -> Dict:
|
async def execute(self, function_name, **kwargs) -> Dict:
|
||||||
translator = requests.get(
|
if self.api_pro:
|
||||||
"https://api.deepl.com/v2/translate",
|
url = "https://api.deepl.com/v2/translate"
|
||||||
params={
|
else:
|
||||||
"auth_key": self.api_key,
|
url = "https://api-free.deepl.com/v2/translate"
|
||||||
"target_lang": kwargs['to_language'],
|
|
||||||
|
headers = {
|
||||||
|
"Authorization": f"DeepL-Auth-Key {self.api_key}",
|
||||||
|
"User-Agent": "chatgpt-telegram-bot/0.2.7",
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded"
|
||||||
|
}
|
||||||
|
|
||||||
|
data = {
|
||||||
"text": kwargs['text'],
|
"text": kwargs['text'],
|
||||||
},
|
"target_lang": kwargs['to_language']
|
||||||
)
|
}
|
||||||
translated_text = translator.json()["translations"][0]["text"]
|
|
||||||
return translated_text
|
return requests.post(url, headers=headers, data=data).json()["translations"][0]["text"]
|
||||||
|
|||||||
Reference in New Issue
Block a user