0

Im developing a telegram bot with this api and I tried to use its webhook example to set up my own bot with webhook method. I have an Ubuntu server and I have set up nginx on it.

now when trying to run my python bot, I get this error:

Traceback (most recent call last):
  File "bot.py", line 106, in <module>
    router.run(host=WEBHOOK_LISTEN, port=int(WEBHOOK_PORT), ssl_context= (WEBHOOK_SSL_CERT, WEBHOOK_PRIV_CERT), debug=True)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 720, in run_simple
    s.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

so I checked what is using my port 443 and the process is nginx:

root     30734     1  0 Aug21 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

I can't turn off nginx because my website is on it, and I need the port to set up telegram bot.

EDIT: I will put my code here for more clarification:

WEBHOOK_HOST = 'mywebsite.com'
WEBHOOK_PORT = '8443'
WEBHOOK_LISTEN = '0.0.0.0'

WEBHOOK_SSL_CERT = "/etc/letsencrypt/live/mywebsite.com/cert.pem"
WEBHOOK_PRIV_CERT = "/etc/letsencrypt/live/mywebsite.com/privkey.pem"

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (TOKEN.get_token())

router = flask.Flask(__name__)

@router.route('/', methods=['GET', 'HEAD'])
def index():
    return 'OK'

@router.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
    if flask.request.headers.get('content-type') == 'application/json':
    json_string = flask.request.json
    print json_string["message"]["text"] # here I get the text of message
    return ''
else:
    flask.abort(403)


bot.remove_webhook()
time.sleep(3)

bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,certificate=open(WEBHOOK_SSL_CERT, 'r'))

router.run(host=WEBHOOK_LISTEN, port=int(WEBHOOK_PORT), ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_PRIV_CERT), debug=True)
ganjim
  • 1,234
  • 1
  • 16
  • 30
  • 1
    as said in telegram, I could use port 8443 too, but that did not work and I don't receive any messages – ganjim Aug 25 '17 at 07:27
  • Show what you have run – Tarun Lalwani Aug 25 '17 at 07:36
  • 1
    you mean the code? – ganjim Aug 25 '17 at 07:40
  • Yes I mean post the code and config you used – Tarun Lalwani Aug 25 '17 at 07:43
  • 1
    I used the same stuff used in the example of the API I used, and I gave the link of the API in the question – ganjim Aug 25 '17 at 07:44
  • You should be able to use any port you like. No need to use exactly port 443. If it didn't work with another port, i guess that nginx isn't configured right. I think you need to open that port first – Endogen Aug 25 '17 at 19:29
  • 1
    I configured nginx with digitalocean tutorial, and as I can see the result, it seems like its working fine, but I cant run two processes that use the same port, my telegram bot uses flask to listen on port 443 and nginx is listening on port 443 for requests. – ganjim Aug 26 '17 at 10:45

0 Answers0