Context
I'm currently following this tutorial. - Telegram Bot with Python Tutorial #3: Creating Bot and Webhook | Project
FIRST STEP
I have set-up a Flask server using the following python code:
from flask import Flask
from flask import request
from flask import Response
import json
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
print(request)
message = request.json()
with open('telegram_request.json', 'w', encoding='utf-8') as filename:
json.dump(message, filename, ensure_ascii=False, indent=4)
# prevents telegram from spamming
return Response('Ok', status=200)
else:
return """
<h1> Flask Server </h1>
<h2> Up and running </h2>
"""
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8443)
SECOND STEP
I port forwarded port 8443 in my router to make the server visible to the outside world (tunneling step in tutorial).
The domain name "myprivatedomain.com:8443" now redirects/refers to the flask server that is set-up.
THIRD STEP
I set-up the Telegram-API webhook correctly, getting the following response code from Telegram:
{"ok":true,"result":true,"description":"Webhook was set"}
NOW
Before sending a message in the Telegram chat: there were no errors.
After sending a message in the chat, the following errors popped up:
code 400, message Bad HTTP/0.9 request type ('RANDOM BYTE VALUES like \x00\x03')
code 400, message Bad request syntax ('RANDOM BYTE VALUES like \x00\x03')
code 400, message Bad request version ('RANDOM BYTE VALUES like \x00\x03')
WHAT I WANT
According to the tutorial, you can write a .json file when Telegram makes a POST request (see example: here). I want to save the message object provided by the Telegram webhook (as showcased in the tutorial video). Using the webhook for getting updates is better than constantly querying the getUpdates() method; that method returns old messages also.
WHAT I'VE TRIED
I've tried to add:
ssl_context='adhoc'
to
app.run(debug=True, host='0.0.0.0', port=8443)
to make the connection HTTPS.
While using this ssl_context, loading the homepage isn't possible either..
PREFERABLE OUTPUT
When the user sends a message inside the Telegram chat --> Python saves a .json file of the message object.