Telegram bot 使用webhook部署电报
我花了一天多的时间研究如何使用webhook部署电报机器人,而不是轮询 即使是在官方文件里,我也没用 有人可以向我解释哦,给我一些工作指南的链接,如何部署像这样最基本的机器人Telegram bot 使用webhook部署电报,telegram-bot,python-telegram-bot,telegram-webhook,Telegram Bot,Python Telegram Bot,Telegram Webhook,我花了一天多的时间研究如何使用webhook部署电报机器人,而不是轮询 即使是在官方文件里,我也没用 有人可以向我解释哦,给我一些工作指南的链接,如何部署像这样最基本的机器人 #!/usr/bin/env python # -*- coding: utf-8 -*- import os from telegram.ext import Updater, CommandHandler, MessageHandler, Filters def start(update, context):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help_command(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def echo(update, context):
"""Echo the user message."""
update.message.reply_text(update.message.text)
def main():
TOKEN = "telegramToken"
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_command))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
PORT = int(os.environ.get('PORT', '5000'))
SERVER = 'myipserver/'
CERT = 'cert.pem'
updater.bot.setWebhook(SERVER + TOKEN, certificate=open(CERT, 'rb'))
# updater.bot.setWebhook(SERVER + TOKEN) also dont working
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)
# updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
我在Heroku上运行我的电报机器人,通常启动webhook,然后将其设置为:
updater.start_webhook(listen="0.0.0.0",
port=int(os.environ.get("PORT", 5000)),
url_path='token'
updater.bot.setWebhook("https://myapp.com/token")
updater.idle()
在Heroku上部署时,使用SSL()获得应用程序的URL,然后通过BotFather进行配置
必须为BotFather提供HTTPS url,我想知道您是否没有这样做,或者您使用的SSL证书(自签名?)有问题。我想在谷歌云上部署它。。我不想部署它heroku。。我将我的ip实例和开放端口5000放在防火墙规则中。这很好,我试图帮助您给出一个真实的示例,您应该能够遵循相同的方法,并在任何云提供商上部署。您应该向聊天机器人添加一个测试URL(添加可能的烧瓶),以确认您可以使用HTTPS访问应用程序