无效的 webhook url 在 Telegram bot 中指定错误

Posted

技术标签:

【中文标题】无效的 webhook url 在 Telegram bot 中指定错误【英文标题】:Invalid webhook url specified Error in Telegram bot 【发布时间】:2021-12-29 12:26:35 【问题描述】:

我正在尝试制作一个简单的电报回声机器人并在其中使用 webhook (ngrok)。我收到此错误,我似乎无法弄清楚我做错了什么。我也试过释放8443端口,但还是没有。

我得到的错误(URL是正确的,我直接从运行ngrok终端复制它):

(myvenv) (base) akriti@hp:~/Desktop/Get-Informed$ python bot.py
Traceback (most recent call last):
  File "bot.py", line 61, in <module>
    bot.set_webhook("http://c026-122-161-51-216.ngrok.io" + TOKEN)
  File "/home/akriti/Desktop/Get-Informed/myvenv/lib/python3.8/site-packages/telegram/bot.py", line 130, in decorator
    result = func(*args, **kwargs)
  File "/home/akriti/Desktop/Get-Informed/myvenv/lib/python3.8/site-packages/telegram/bot.py", line 2965, in set_webhook
    result = self._post('setWebhook', data, timeout=timeout, api_kwargs=api_kwargs)
  File "/home/akriti/Desktop/Get-Informed/myvenv/lib/python3.8/site-packages/telegram/bot.py", line 295, in _post
    return self.request.post(
  File "/home/akriti/Desktop/Get-Informed/myvenv/lib/python3.8/site-packages/telegram/utils/request.py", line 361, in post
    result = self._request_wrapper(
  File "/home/akriti/Desktop/Get-Informed/myvenv/lib/python3.8/site-packages/telegram/utils/request.py", line 279, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Invalid webhook url specified

以下是我的代码:

import logging 
from flask import Flask, request
from telegram import Update, Bot
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, Dispatcher


# enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

TOKEN = "213181**********"

# create Flask app object
app = Flask(__name__)

@app.route('/')
def index():
    return "Hello!"

# create view to handle webhooks
@app.route(f'/TOKEN', methods=['GET', 'POST'])
def webhook():
    """webhook view which receives updates from telegram"""
    # create update object from json-format request data
    update = Update.de_json(request.get_json(), bot)
    # process update
    dp.process_update(update)
    return "ok"


def start(update: Update, context: CallbackContext):
    print(update)
    author = update.message.from_user.first_name
    # msg = update.message.text
    reply = "Hi! ".format(author)
    context.bot.send_message(chat_id = update.message.chat_id, text=reply)
     

def _help(update: Update, context: CallbackContext):
    help_txt = "Hey! This is a help text."
    context.bot.send_message(chat_id = update.message.chat_id, text=help_txt)


def echo_text(update: Update, context: CallbackContext):
    reply = update.message.text
    context.bot.send_message(chat_id = update.message.chat_id, text=reply)
    

def echo_sticker(update: Update, context: CallbackContext):
    context.bot.send_sticker(chat_id = update.message.chat_id, sticker=update.message.sticker.file_id)


def error(update, context):
    logger.error("Update '%s' caused error '%s'", update, update.error)


if __name__ == "__main__":
    # create telegram bot object
    bot = Bot(TOKEN)
    # set webhook for telegram bot 
    bot.set_webhook("http://c026-122-161-51-216.ngrok.io" + TOKEN)

    dp = Dispatcher(bot, None)
    # add handlers
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", _help))
    dp.add_handler(MessageHandler(Filters.text, echo_text))
    dp.add_handler(MessageHandler(Filters.sticker, echo_sticker))
    dp.add_error_handler(error)
    app.run(port=8443)

【问题讨论】:

你在你的代码中留下了你的 private 机器人令牌。人们可能会滥用它!你应该撤销它! 是的,我编辑了它 它在history 中仍然可见,所以你应该向@BotFather 询问新的 哦,好的,那我改一下。谢谢 【参考方案1】:

"http://c026-122-161-51-216.ngrok.io" + TOKEN 将导致"http://c026-122-161-51-216.ngrok.ioTOKEN",它不是一个有效的 URL。您可能希望使用带有斜线的"http://c026-122-161-51-216.ngrok.io/" + TOKEN

【讨论】:

非常感谢,你是救命稻草!!

以上是关于无效的 webhook url 在 Telegram bot 中指定错误的主要内容,如果未能解决你的问题,请参考以下文章

从 Mandrill Webhook 接收到的 JSON 字符串无效

Paypal webhook 而不是返回 url

如何使用 ngrok 将 webhook 发布到本地计算机上的 url?

在dialogflow webhooks中设置http url

我对 webhook 的定义有疑问

BitBucket Webhook - 无法连接到 Jenkins URL