Telegram bot - requested address is not valid in its context

211 views Asked by At

I built a telegram bot using python-telegram-bot, I want to connect it to a web hook. When I run it, it gives me an error:

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

I want to connect the telegram bot to the server using the web hook method, but it gives me an error.

My code:

# -*- 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()

Do you know the method of connecting the bot to the server other than the web Hook method? Please explain.

1

There are 1 answers

0
Samuel Dagon On

since setting up a webhook is not a repetitive thing i recommend you to do it manually and save your time for more complex issues. just replace bot-token and script-url in the link below and run it on your browser.

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

remember that your Script_URL should start with https