-
Notifications
You must be signed in to change notification settings - Fork 10
/
tutorial6.py
32 lines (27 loc) · 1.06 KB
/
tutorial6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#webhook heroku - this tutorial works on cloud.
# to run flask on local server
# export FLASK_APP=tutorial6
# flask run
#https://github.com/python-telegram-bot/python-telegram-bot/wiki/Where-to-host-Telegram-Bots#vps
#https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks
#https://python-telegram-bot.readthedocs.io/
#https://seminar.io/2018/09/03/building-serverless-telegram-bot/
#https://www.heroku.com/
from flask import Flask, render_template, request
import os
import telegram
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def webhook():
bot = telegram.Bot(token=os.environ["YOURAPIKEY"])
if request.method == "POST":
update = telegram.Update.de_json(request.get_json(force=True), bot)
chat_id = update.effective_chat.id
text = update.message.text
first_name = update.effective_chat.first_name
# Reply with the same message
bot.sendMessage(chat_id=chat_id, text=f"{text} {first_name}")
return 'ok'
return 'error'
def index():
return webhook()