-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (38 loc) · 1.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
from aiogram import Bot, Dispatcher, executor, types
from oxfordlookapp import getDefinitions
from googletrans import Translator
translator = Translator()
API_TOKEN = '5043771272:AAFeAtOW6Hm84Rlgt7w8haPuPBYONu5tWvc'
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/start` or `/help` command
"""
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
@dp.message_handler()
async def tarjimon(message: types.Message):
print(message)
lang = translator.detect(message.text).lang
if len(message.text.split()) > 2:
dest = 'uz' if lang == 'en' else 'en'
await message.reply(translator.translate(message.text, dest).text)
else:
if lang=='en':
word_id = message.text
else:
word_id = translator.translate(message.text, dest='en').text
lookup = getDefinitions(word_id)
if lookup:
await message.reply(f"Word: {word_id} \nDefinitions:\n{lookup['definitions']}")
if lookup.get('audio'):
await message.reply_voice(lookup['audio'])
else:
await message.reply("Bunday so'z topilmadi")
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)