电报机器人 - 请求的地址在其上下文中无效

时间:2017-08-27 13:02:44

标签: python python-2.7 python-telegram-bot

我使用python-telegram-bot构建了一个电报机器人,我想将它连接到一个Web钩子。当我运行它时,它给了我一个错误:

Exception in thread updater:
Traceback (most recent call last):
  File "G:\python2.7.9\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "G:\python2.7.9\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "G:\python2.7.9\lib\site-packages\telegram\ext\updater.py", line     133, in _thread_wrapper
    target(*args, **kwargs)
  File "G:\python2.7.9\lib\site-packages\telegram\ext\updater.py", line 319, in _start_webhook
    self.bot)
  File "G:\python2.7.9\lib\site-packages\telegram\utils\webhookhandler.py", line 28, in __init__
    super(WebhookServer, self).__init__(server_address, RequestHandlerClass)
  File "G:\python2.7.9\lib\SocketServer.py", line 420, in __init__
    self.server_bind()
  File "G:\python2.7.9\lib\BaseHTTPServer.py", line 108, in server_bind
    SocketServer.TCPServer.server_bind(self)
  File "G:\python2.7.9\lib\SocketServer.py", line 434, in server_bind
    self.socket.bind(self.server_address)
  File "G:\python2.7.9\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10049] The requested address is not valid in its context

我想使用网络钩子方法将电报机器人连接到服务器,但它给了我一个错误。

我的代码:

# -*- coding: utf-8 -*-
import os
from telegram.ext import Updater, MessageHandler, Filters
import re                                                                                                                          


def delete_method(bot, update):
    if not update.message.text:
        print("it does not contain text")
        return

    mlist=[u"سلام", u"شاد"]


    for i in mlist:
        if re.search(i, update.message.text):
            bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id)


def main():
    TOKEN = "TOKEN"
    PORT = int(os.environ.get('PORT', '5000'))
    updater = Updater(TOKEN)
    updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)
    updater.bot.set_webhook("https://my account.herokuapp.com/" + TOKEN)
    dispatcher = updater.dispatcher
    dispatcher.add_handler(MessageHandler(Filters.all, delete_method))


    updater.start_polling()

    updater.idle()

if __name__ == '__main__':
    main()
# for exit
# updater.idle()

您知道除了Web Hook方法之外将bot连接到服务器的方法吗?请解释一下。

1 个答案:

答案 0 :(得分:0)

因为设置webhook并不是一件重复的事情,我建议你手动完成并为更复杂的问题节省时间。只需在下面的链接中替换bot-token和script-url,然后在浏览器上运行它。

https://api.telegram.org/botBOT-TOKEN/setwebhook?url=Script-URL

请记住,您的Script_URL应以https

开头