create app

This commit is contained in:
Dmitry Afanasyev 2022-08-16 10:47:36 +03:00
parent aeb45c9d7d
commit 585b9b221d
4 changed files with 57 additions and 23 deletions

View File

@ -40,24 +40,6 @@ def get_keyboard() -> types.InlineKeyboardMarkup:
return markup
@dispatcher.message_handler(commands=['command1'])
async def send_message_1(message: types.Message) -> None:
logger.info("command 1 started")
await asyncio.sleep(15)
await bot.send_message(message.chat.id, 'message 1 on screen')
logger.info('command 1 ends')
@dispatcher.message_handler(commands=['command2'])
async def send_message_2(message: types.Message) -> None:
logger.info("command 2 started")
await asyncio.sleep(15)
await bot.send_message(message.chat.id, 'message 2 on screen')
logger.info('command 2 ends')
@dispatcher.callback_query_handler(stations_cb.filter(direction='home->office'))
async def home_office(
query: types.CallbackQuery, callback_data: dict[str, str]
@ -89,7 +71,7 @@ async def office_home(
@dispatcher.message_handler(commands=['chatid'])
async def chat_id(message: types.Message) -> SendMessage:
logger.info('Hello World')
return SendMessage(message.chat.id, message.chat.id)

View File

@ -1,5 +1,9 @@
from aiogram import Dispatcher
from http import HTTPStatus
from aiogram import Bot, Dispatcher
from aiogram.types import Update
from aiogram.utils.executor import start_polling, start_webhook
from aiohttp import web
from app.core.bot import bot, dispatcher
from app.core.logger import logger
from app.core.scheduler import asyncio_schedule
@ -53,8 +57,26 @@ def bot_webhook() -> None:
)
async def index(request: web.Request) -> web.Response:
data = await request.json()
Bot.set_current(dispatcher.bot)
Dispatcher.set_current(dispatcher)
tg_update = Update(**data)
await dispatcher.process_update(tg_update)
return web.Response(status=HTTPStatus.OK)
async def create_app() -> web.Application:
app = web.Application()
app.router.add_post('/', index)
return app
if __name__ == '__main__':
if START_WITH_WEBHOOK:
bot_webhook()
else:
bot_polling()
# bot_polling() # type: ignore
app = create_app()
web.run_app(app=app, host='localhost', port=8084)

View File

@ -23,7 +23,7 @@ WEBHOOK_PATH = config('WEBHOOK_PATH', default='')
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
# webserver settings
WEBAPP_HOST = config('WEBAPP_HOST', default='') # or ip
WEBAPP_PORT = config('WEBAPP_PORT', cast=int, default=0)
WEBAPP_HOST = config('WEBAPP_HOST', default='localhost') # or ip
WEBAPP_PORT = config('WEBAPP_PORT', cast=int, default=8084)
START_WITH_WEBHOOK = config('START_WITH_WEBHOOK', cast=bool, default=False)

View File

@ -1,3 +1,5 @@
import time
import pytest
from aiogram import Bot, types
from aiogram.dispatcher.filters.builtin import Command
@ -31,3 +33,31 @@ async def test_command1(bot: Bot) -> None:
if handl:
command = handl[0].filter.commands[0]
assert command
async def test_update() -> None:
data = {
"update_id": 957250703,
"message": {
"message_id": 417070387,
"from": {
"id": 417070387,
"is_bot": False,
"first_name": "Dmitry",
"last_name": "Afanasyev",
"username": "Balshtg",
"language_code": "en",
},
"chat": {
"id": 417070387,
"first_name": "Dmitry",
"last_name": "Afanasyev",
"username": "Balshtg",
"type": "private",
},
"date": time.time(),
"text": "/chatid",
"entities": [{"type": "bot_command", "offset": 0, "length": 7}],
},
}
assert data