-
Notifications
You must be signed in to change notification settings - Fork 1
/
babelegram.py
89 lines (72 loc) · 2.69 KB
/
babelegram.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
An Telegram bot for translating messages.
The bot uses the Microsoft Translator API via microsofttranslator library.
"""
import asyncio
import logging
import os
from aiohttp import web
import microsofttranslator
import handlers
async def server_init(loop, bot, queue, base_url, url_path, port):
""" Starts server and plugs-in the bot."""
async def webhook(request):
""" Handles all income messages from Telegram API"""
data = await request.text()
await queue.put(data) # pass update to bot
return web.Response(body='OK'.encode('utf-8'))
app = web.Application(loop=loop)
app.router.add_route('GET', url_path, webhook)
app.router.add_route('POST', url_path, webhook)
# Just for development purposes. Later it would be served via nginx
app.router.add_static('/img', 'assets/')
logging.info('Starting the server...')
srv = await loop.create_server(app.make_handler(), '', port)
logging.info('Server started...')
# sets the url for webhook
await bot.setWebhook(base_url + url_path)
return srv
def main():
""" Application entry point.
"""
# Configuring log
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p'
)
# Getting environment variables
logging.info('Loading environment variables...')
bot_token = os.environ['BOT_TOKEN']
az_client_id = os.environ['AZ_CLIENT_ID']
az_client_secret = os.environ['AZ_CLIENT_SECRET']
# Creating the translator
logging.info('Creating translator...')
mstranslator = microsofttranslator.Translator(
az_client_id, az_client_secret)
# Creating the bot
logging.info('Creating the bot...')
bot = handlers.create_bot(bot_token, mstranslator)
# Starting the main loop
loop = asyncio.get_event_loop()
if os.environ.get('IS_PROD'):
url = os.environ['BOT_BASE_URL']
url_path = os.environ['BOT_WEBHOOK_PATH']
port = os.environ['PORT']
message_queue = asyncio.Queue() # channel between web app and bot
server_task = server_init(
loop, bot, message_queue, url, url_path, port)
loop.run_until_complete(server_task)
logging.info('Starting the bot...')
loop.create_task(bot.message_loop(source=message_queue))
else:
logging.info('Deleting webhook')
loop.create_task(bot.setWebhook()) # deleting any webhook available
logging.info('Starting the bot...')
loop.create_task(bot.message_loop()) # starting bot pooling
try:
loop.run_forever()
logging.info('Bot listening...')
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()