-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_app.py
More file actions
40 lines (32 loc) · 1.28 KB
/
my_app.py
File metadata and controls
40 lines (32 loc) · 1.28 KB
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
import telebot
import conf
from collections import Counter
import flask
import re
WEBHOOK_URL_BASE = "https://{}:{}".format(conf.WEBHOOK_HOST, conf.WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/{}/".format(conf.TOKEN)
bot = telebot.TeleBot(conf.TOKEN, threaded=False)
bot.remove_webhook()
bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH)
app = flask.Flask(__name__)
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.send_message(message.chat.id, 'Здравствуйте! Это бот, который напишет Вам самые частотные буквы в сообщении.')
@bot.message_handler(func=lambda m: True)
def send_sym(message):
clean_message = re.sub('[^А-Яа-яA-Za-z]', '', message.text)
reply0 = Counter(str(clean_message)).most_common(3)
reply = str(reply0)
bot.send_message(message.chat.id, reply)
@app.route('/', methods=['GET', 'HEAD'])
def index():
return 'ok'
@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
flask.abort(403)