Ejemplo n.º 1
0
async def tg_webhook(update: Update):
    try:
        reply = MessageReply(
            chat_id=update.message.chat.id,
            text=update.message.text.capitalize(),
        )
        async with aiohttp.ClientSession() as sesss:
            url = f"https://api.telegram.org/bot{settings.bot_token}/sendMessage"
            async with sesss.post(url, json=reply.dict()) as resp:
                payload = await resp.json()
                debug(resp, payload)
    finally:
        return {"ok": True}
Ejemplo n.º 2
0
async def tg_webhook(update: Update):
    debug(settings)
    try:
        if update.message.chat.type == "private":
            result = await process_way(update)
        if update.message.forward_from_chat is not None:
            result = await repost_chanel(update)
        result = await word_check(update)

        return result

    finally:
        return {"ok": True}
Ejemplo n.º 3
0
async def handle_setup_webhook(password: str = Form(...)):
    if password != settings.admin_password:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Only admin is allowed to configure webhook",
        )

    new_webhook_url = (
        f"{settings.service_url}/webhook/{settings.webhook_secret}/")
    debug(new_webhook_url)

    await set_webhook(url=new_webhook_url)

    return RedirectResponse(
        "/",
        status_code=status.HTTP_303_SEE_OTHER,
    )
Ejemplo n.º 4
0
async def handle_index(request: Request):
    webhook_unsafe = await get_webhook_info()
    debug(webhook_unsafe)

    webhook_safe = shadow_webhook_secret(webhook_unsafe)

    context = {
        "url_webhook_current": webhook_safe.url,
        "url_webhook_new": f"{settings.service_url}/webhook/",
    }

    response = templates.TemplateResponse("index.html", {
        "request": request,
        **context
    })

    return response
Ejemplo n.º 5
0
async def Send_api_telegram(
    method_name: str,
    data: Optional[BaseModel],
    output_type: Optional[Type[OutputDataT]],
) -> Optional[OutputDataT]:
    url = f"{TELEGRAM_BOT_API}/{method_name}"
    request_kw = {}

    if data is not None:
        request_kw.update(dict(json=data.dict(), ))

    async with aiohttp.ClientSession() as session:
        async with session.post(url, **request_kw) as response:
            payload = await response.json()
        if response.status != status.HTTP_200_OK:
            debug(response)
            debug(payload)
            errmsg = (f"method {method_name!r}"
                      f" failed with status {response.status}")
            raise RuntimeError(errmsg)

    response_tg = TelegramResponse.parse_obj(payload)

    if not response_tg.ok:
        debug(response_tg)
        errmsg = f"method {method_name!r} failed: {response_tg.result}"
        raise RuntimeError(errmsg)

    result = response_tg

    if output_type is not None:
        result = output_type.parse_obj(response_tg.result)

    return result
Ejemplo n.º 6
0
async def invoke_api_method(
    method_name: str,
    data: BaseModel,
    output_type: Type[OutputDataT],
) -> OutputDataT:
    """
    Generic function which invokes an arbitrary method from Telegram Bot API
    :param method_name: name of the API method
    :param data: some data for the method
    :param output_type: type of response data
    :return: object of output type
    """

    url = f"{TELEGRAM_BOT_API}/{method_name}"

    async with aiohttp.ClientSession() as session:
        response_http: ClientResponse
        async with session.post(url, json=data.dict()) as response_http:
            payload = await response_http.json()

            if response_http.status != status.HTTP_200_OK:
                debug(response_http)
                debug(payload)
                errmsg = (f"method {method_name!r}"
                          f" failed with status {response_http.status}")
                raise RuntimeError(errmsg)

    response_tg = TelegramResponse.parse_obj(payload)

    if not response_tg.ok:
        debug(response_tg)
        errmsg = f"method {method_name!r} failed: {response_tg.result}"
        raise RuntimeError(errmsg)

    result = output_type.parse_obj(response_tg.result)

    return result
Ejemplo n.º 7
0
async def handle_settings():
    debug(settings)
    return settings