0

I'm developing a telegram bot with the help of this API, and I tried to implement it with webhook method, first I tried my bot with getUpdated method and it works like a charm, but when I updated it to webhook, it doesn't seem to work.

I implemented webhook with flask, exactly following this example, except for the ssl certificate and IP, because I have an https domain, so instead of using IP and self signed certificate, I used my domain.

Then I tested my webhook with postman and sent requests with the data I got from api.telegram.org, and again it worked, but when I send a message to my bot in Telegram application, I get no message in the server, seems like telegram does not send the message as it is supposed to.

EDIT: Here I put my code 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 should 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
  • Yes i know, this doesn't answer your question, but anyway: Maybe you want to think about using [this python wrapper](https://github.com/python-telegram-bot/python-telegram-bot)? It supports the newest Telegram API (your wrapper doesn't) and there is a pretty good Telegram group where you get instant help and it is used more often then the wrapper you use. Just saying... – Endogen Aug 25 '17 at 19:20
  • @Endogen I will try that wrapper too, thank you. and can you please give me the link of the telegram group you mentioned? – ganjim Aug 26 '17 at 10:39

1 Answers1

0

i think you sould use domain name or real IP address.

a little comment from your example link:

In some VPS you may need to put here the IP addr

if you develop on localhost, you can use somesing like ngrok.io

dzNET
  • 930
  • 1
  • 9
  • 14
  • well I have to say I'm sorry if I didn't mention, I'm developing my bot on my server from digitalocean, I have a VPS , and I have an HTTPS domain, so I send my requests to my domain and my flask server receives them – ganjim Sep 12 '17 at 08:36
  • are you using a debug mode? try if not. url and port correctly? also try to scan connections by `netstat` for example. NOTE: are you use self-signed ssl? – dzNET Sep 12 '17 at 10:53
  • I used digitalocean tutorial to create ssl certificate using letsEncrypt. I'm not sure which type it is, – ganjim Sep 12 '17 at 21:47
  • yes url and port are set correctly, I have used nmap to check if ports are open, but I will try netstat for connections later, how am I supposed to run my bot in debug mode? – ganjim Sep 12 '17 at 21:49
  • sorry for my inattention. debug mode are used on code example above. `router.run(host=WEBHOOK_LISTEN, port=int(WEBHOOK_PORT), ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_PRIV_CERT), debug=True)` why did you make port as string, and then again as integer? – dzNET Sep 13 '17 at 02:38
  • I used the port once to create the URL_BASE, but in router.run method, it needed port as an integer. – ganjim Sep 16 '17 at 11:36
  • 1
    So, why do you make it as string? `WEBHOOK_PORT = '8443'` change it to `WEBHOOK_PORT = 8443` and in URL_BASE `WEBHOOK_URL_BASE = "https://%s:%d" % (WEBHOOK_HOST, WEBHOOK_PORT)` or use the `format()` function. read [docs](https://docs.python.org/3.6/library/stdtypes.html#str.format) – dzNET Sep 16 '17 at 14:56
  • Yea thank you for that, I never learned python in a pythonic way, but I will :)) – ganjim Sep 16 '17 at 20:55