-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkatrinbot.py
78 lines (62 loc) · 2.19 KB
/
katrinbot.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
import logging
from flask import Flask
from flask import jsonify
from flask import request
from siteworker import get_latest, get_random
from telegram import Bot
app = Flask(__name__)
app.config.from_object('katrinbot_settings')
URL = "https://vitko.info/katrinphotobot/wh/"
WH_URL = app.config['URL'] + app.config['TOKEN']
bot = Bot(app.config['BOT_TOKEN'])
@app.route("/")
def index():
return "index page"
def processwh(msg):
repl_url = "http://lene.pois.org.ru/Katrin/img/{}"
chat = msg["message"]["chat"]
message_id = msg["message"]["message_id"]
text = msg["message"].get("text", None)
if text is not None:
if text in ["/start", "/help"]:
response = "Используй команды /random или /latest."
elif text.startswith("/latest"):
item = get_latest()
response = repl_url.format(item)
elif text.startswith("/random"):
item = get_random()
response = repl_url.format(item)
else:
response = "Шо?"
# set keyboard
if chat["type"] == "private":
reply_markup = {"keyboard": [["/latest"], ["/random"]]}
else:
reply_markup = {}
bot.send_message(chat["id"], response, reply_markup=reply_markup)
else:
response = "No text in the message."
return response
@app.route("/wh/%s" % app.config['TOKEN'], methods=['GET', 'POST'])
def webhook():
app.logger.debug("got message")
if request.method == 'POST':
msg = request.get_json()
app.logger.debug(msg)
answer = processwh(msg)
return jsonify(status="ok", answer=answer)
else:
return jsonify(status="ok")
if __name__ == "__main__":
if not app.debug:
# In production mode, add log handler to sys.stderr.
ch = logging.StreamHandler()
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
app.logger.addHandler(ch)
app.logger.setLevel(logging.DEBUG)
wh_result = bot.register_webhook(WH_URL)
app.logger.debug(wh_result)
app.run()