-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
88 lines (68 loc) · 2.29 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
import telepot
import re
import os
import random
import string
import time
from flask import Flask, render_template, session, url_for, redirect, request
from telepot.namedtuple import *
USERNAME = "PYTHONANYWHERE_USERNAME"
TOKEN = "TELEGRAM_BOT_API"
SECRET = ''.join(random.choice(string.ascii_letters) for x in range(20))
URL = f"https://{USERNAME}.pythonanywhere.com/{SECRET}"
telepot.api.set_proxy('http://proxy.server:3128')
bot = telepot.Bot(TOKEN)
bot.setWebhook(URL, max_connections=10)
def processing(msg):
if 'chat' in msg and msg['chat']['type'] == 'channel':
return
id = msg['from']['id']
if 'text' in msg:
msg['text'] = str(msg['text']) # FEELING SAFER ;)
msg['type'] = 'text'
elif 'data' in msg:
msg['type'] = 'callback'
msg['text'] = f"%callback {msg['data']}"
else:
msg['type'] = 'nontext'
types = ['audio', 'voice', 'document', 'photo',
'video', 'contact', 'location']
for type in types:
if type in msg:
msg['text'] = f'%{type}'
break
if 'text' in msg:
for entry in regex:
if re.match(entry, msg["text"]):
matches = re.match(entry, msg["text"]).groups()
parser(msg, list(matches))
return
app = Flask(__name__)
@app.route(f'/{SECRET}', methods=["POST"])
def webhook():
update = request.get_json()
if "message" in update:
processing(update['message'])
elif 'callback_query' in update:
processing(update['callback_query'])
return 'OK'
regex = [
r'^[!/](start)',
r'^[!/](echo) (.*)'
]
def parser(msg, matches):
usr = msg['from']
if msg['type'] == "text":
if matches[0] == 'start':
text = "welcome message"
markup = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text='BTN1', callback_data='btn1'),
InlineKeyboardButton(text='BTN2', callback_data='btn2')]
])
bot.sendMessage(usr['id'], text, reply_markup=markup)
return
if matches[0] == 'echo' and matches[1]:
bot.sendMessage(usr['id'], matches[1])
return
if __name__ == "__main__":
app.run()