【问题标题】:send_message not sending message for pytelegrambotapisend_message 不为 pytelegrambotapi 发送消息
【发布时间】:2020-12-24 08:17:12
【问题描述】:

我正在尝试使用 django 和 pyTelegramBotApi 在 python 中制作一个简单的 tg bot,它在 /start 上回答“Hello”。我已经设置了 webhook,并且 bot 正在正确获取更新,但是当涉及到 send_message 时,它​​只是进入了低谷并且什么也没发送。

@csrf_exempt
def update_bot(request):
    json_str = request.body.decode("UTF-8")
    update = types.Update.de_json(json_str)
    tele_bot.process_new_updates([update])
    return HttpResponse({'code': 200})


@tele_bot.message_handler(commands=['start'])
def start_message(message):
    print("sending message")
    print(tele_bot.send_message(message.chat.id, "Hello"))

控制台输出是

sending message
[24/Dec/2020 10:54:46] "POST /bot/imagesmanager/ HTTP/1.1" 200 4

【问题讨论】:

    标签: python python-3.x python-telegram-bot django-2.2


    【解决方案1】:

    我不确定错误是什么,因为消息没有被打印出来,但我一直在使用这个:https://api.telegram.org/{bot_id}/sendMessage 很长时间以来它工作得很好。 documentation 非常详细地介绍了如何以这种形式使用 api。

    【讨论】:

    • 是的,我尝试过使用电报的 api,它工作正常,但我知道为什么 send_message 根本不起作用:/
    • 您可能想检查您的聊天ID,有时甚至一个字符错误都会导致问题。
    • 我做到了。实际上,我确实从我的程序中获取了 chat_id 来尝试通过 tg api 发送消息,正如我所说 - 它工作正常
    【解决方案2】:

    我已经解决了这个问题。我刚刚阅读了我的令牌,最后带有 \n(使用 repr(token) 找到),我知道为什么更新工作正常,但简单的条带对我有足够的帮助)

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多