This repository has been archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
65 lines (43 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Simple echo Telegram Bot example on Aiogram framework using
Yandex.Cloud functions.
"""
import asyncio
import json
import logging
import os
from aiogram import Bot, Dispatcher, types
# Logger initialization and logging level setting
log = logging.getLogger(__name__)
log.setLevel(os.environ.get('LOGGING_LEVEL', 'INFO').upper())
# Handlers
async def start(message: types.Message):
await message.reply('Hello, {}!'.format(message.from_user.first_name))
async def echo(message: types.Message):
await message.answer(message.text)
# Functions for Yandex.Cloud
async def register_handlers(dp: Dispatcher):
"""Registration all handlers before processing update."""
dp.register_message_handler(start, commands=['start'])
dp.register_message_handler(echo)
log.debug('Handlers are registered.')
async def process_event(event, dp: Dispatcher):
"""
Converting an Yandex.Cloud functions event to an update and
handling tha update.
"""
update = json.loads(event['body'])
log.debug('Update: ' + str(update))
Bot.set_current(dp.bot)
update = types.Update.to_object(update)
await dp.process_update(update)
async def handler(event, context):
"""Yandex.Cloud functions handler."""
if event['httpMethod'] == 'POST':
# Bot and dispatcher initialization
bot = Bot(os.environ.get('TOKEN'))
dp = Dispatcher(bot)
await register_handlers(dp)
await process_event(event, dp)
return {'statusCode': 200, 'body': 'ok'}
return {'statusCode': 405}