无效的 webhook url 在 Telegram 机器人中指定错误
Invalid webhook url specified Error in Telegram bot
我正在尝试制作一个简单的电报回显机器人并在其中使用 webhooks (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)
"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
和斜杠
我正在尝试制作一个简单的电报回显机器人并在其中使用 webhooks (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)
"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
和斜杠