-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
53 lines (39 loc) · 1.31 KB
/
example.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask
from flask import render_template, request
import logging
import telegram
import os
import requests
HOST = "https://<your-app-name>.herokuapp.com/"
app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
global bot
bot = telegram.Bot(token='TOKEN')
botName = "Bot Name" #Without @
@app.route("/", methods=["POST", "GET"])
def setWebhook():
if request.method == "GET":
logging.info("Hello, Telegram!")
print "Done"
return "OK, Telegram Bot!"
@app.route("/verify", methods=["POST"])
def verification():
if request.method == "POST":
update = telegram.Update.de_json(request.get_json(force=True),bot)
if update is None:
return "Show me your TOKEN please!"
logging.info("Calling {}".format(update.message))
handle_message(update.message)
return "ok"
def handle_message(msg):
text = msg.text
print msg
# An echo bot
bot.sendMessage(chat_id=msg.chat.id, text=text)
if __name__ == "__main__":
s = bot.setWebhook("{}/verify".format(HOST))
if s:
logging.info("{} WebHook Setup OK!".format(botName))
else:
logging.info("{} WebHook Setup Failed!".format(botName))
app.run(host= "0.0.0.0", debug=True)