【问题标题】:Telegram bot - requested address is not valid in its context电报机器人 - 请求的地址在其上下文中无效
【发布时间】:2017-08-27 13:02:44
【问题描述】:

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

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

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

我的代码:

# -*- 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连接服务器的方法吗?请解释一下。

【问题讨论】:

  • 你网址https://my account....中的空格是故意的吗?
  • @tobifasc我不懂请解释

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


【解决方案1】:

因为设置 webhook 不是重复性的事情,我建议您手动进行,这样可以节省您处理更复杂问题的时间。只需替换下面链接中的 bot-token 和 script-url 并在浏览器上运行即可。

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

请记住,您的 Script_URL 应该以 https 开头

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2020-12-25
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2022-01-07
    相关资源
    最近更新 更多