Exemplo n.º 1
0
async def myupdate():
    update = {
        "update_id": 960323333,
        "message": {
            "message_id": 3846,
            "from": {
                "id": 20936078,
                "is_bot": False,
                "first_name": "ᴅᴀᴠʟᴀᴛᴊᴏɴ🐼",
                "username": "******",
                "language_code": "ru"
            },
            "chat": {
                "id": 20936078,
                "first_name": "ᴅᴀᴠʟᴀᴛᴊᴏɴ🐼",
                "username": "******",
                "type": "private"
            },
            "date": 1602509595,
            "text": "/start",
            "entities": [{
                "offset": 0,
                "length": 6,
                "type": "bot_command"
            }]
        }
    }
    # mydict = {k: v.encode("utf-8") for k, v in update.items()}
    # update = json.dumps(update)
    update = types.Update(**update)

    Bot.set_current(dp.bot)
    Dispatcher.set_current(dp)
    await dp.process_updates([update])
Exemplo n.º 2
0
async def callback_telegram(request: Dict[str, Any] = Body(...),
                            dp: Dispatcher = Depends(bot_dispatcher)):
    Bot.set_current(dp.bot)
    Dispatcher.set_current(dp)
    telegram_update = types.Update(**request)
    await dp.process_update(telegram_update)
    return {'status': 'OK'}
Exemplo n.º 3
0
async def execute(req: web.Request) -> web.Response:
    upds = [types.Update(**(await req.json()))]
    Bot.set_current(dp.bot)
    Dispatcher.set_current(dp)
    try:
        await dp.process_updates(upds)
    except Exception as e:
        logger.error(e)
    finally:
        return web.Response()
Exemplo n.º 4
0
    async def process_update(self, update: types.Update):
        """Process update object with user availability in database check.

        If bot doesn't know the user, it pretends they sent /start message.
        """
        user = None
        if update.message:
            user = update.message.from_user
            chat = update.message.chat
        elif update.callback_query and update.callback_query.message:
            user = update.callback_query.from_user
            chat = update.callback_query.message.chat
        if user:
            await database.users.update_many(
                {
                    "id": {
                        "$ne": user.id
                    },
                    "mention": user.mention
                },
                {"$set": {
                    "has_username": False
                }},
            )
            document = await database.users.find_one_and_update(
                {
                    "id": user.id,
                    "chat": chat.id
                },
                {
                    "$set": {
                        "mention": user.mention,
                        "has_username": bool(user.username),
                    }
                },
                return_document=ReturnDocument.AFTER,
            )
            if document is None:
                if update.message:
                    if not update.message.text.startswith("/start "):
                        update.message.text = "/start"
                elif update.callback_query:
                    await update.callback_query.answer()
                    update = types.Update(
                        update_id=update.update_id,
                        message={
                            "message_id": -1,
                            "from": user.to_python(),
                            "chat": chat.to_python(),
                            "date": int(time()),
                            "text": "/start",
                        },
                    )
            database_user.set(document)
        return await super().process_update(update)
Exemplo n.º 5
0
async def webhook_handle(request):
    update = await request.json()
    update = types.Update(**update)
    Bot.set_current(dp.bot)  # без этого не работает
    Dispatcher.set_current(dp)
    try:
        await dp.process_update(update)
    except Exception:
        traceback.print_exception(*sys.exc_info())

    return web.Response(text='ok')
Exemplo n.º 6
0
async def getUpdate(request):
    asyncio.create_task(dp.updates_handler.notify(types.Update(**(await request.json()))))
    return web.Response(text="OK")
Exemplo n.º 7
0
from aiogram import types
from .dataset import UPDATE

update = types.Update(**UPDATE)


def test_export():
    exported = update.to_python()
    assert isinstance(exported, dict)
    assert exported == UPDATE


def test_update_id():
    assert isinstance(update.update_id, int)
    assert hash(update) == UPDATE['update_id']
    assert update.update_id == UPDATE['update_id']


def test_message():
    assert isinstance(update.message, types.Message)
Exemplo n.º 8
0
async def execute(req: web.Request) -> web.Response:
    token = req.match_info['token']
    with dp.bot.with_token(token, validate_token=True):
        upds = [types.Update(**(await req.json()))]
        await dp.process_updates(upds)
    return web.Response()
Exemplo n.º 9
0
async def proceed_update(req: web.Request):
    upds = [types.Update(**(await req.json()))]
    Bot.set_current(req.app['bot'])
    Dispatcher.set_current(req.app['dp'])
    await req.app['dp'].process_updates(upds)
Exemplo n.º 10
0
def parse_update():
    return types.Update(**request.json)
Exemplo n.º 11
0
async def main(**data):
    update = types.Update(**data)
    return await dp.process_update(update)