-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
113 lines (98 loc) · 3.97 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import requests
import time
import re
import sqlalchemy
import os
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy import Column, Integer, String
engine = create_engine('mysql://root:@localhost/henry?charset=utf8', pool_recycle=3600,encoding='utf-8')
Base = declarative_base()
session = Session(engine)
class Response(Base):
__tablename__ = 'Response'
id = Column(Integer, primary_key=True)
trigger = Column(String)
response = Column(String)
def __init__(self, trigger,response):
self.trigger = trigger
self.response = response
def __repr__(self):
return "<Response(trigger='%s', response='%s')>" % (
self.trigger, self.response)
API_KEY = os.environ['HENRY_API_KEY']
dict = {}
def parse_message(msg, data):
if 'username' in data['message']['from']:
username = "@" + data['message']['from']['username']
elif 'first_name' in data['message']['from']:
username = data['message']['from']['first_name']
else:
username = 'Unknown'
return msg.replace("{user}", username)
def parse_response(json):
if 'message' not in json:
return
if 'text' in json['message']:
match = re.search(r'(\w+):([^:]+):(.+)', json['message']['text'])
if match is not None:
if(match.group(1) == "add"):
pattern = match.group(2)
answer = match.group(3)
if len(pattern) > 2:
dict[pattern.upper()] = answer
session.add(Response(pattern.upper(),answer))
session.commit()
elif match.group(1) == "henry":
if match.group(2) == "delete":
pattern = match.group(3)[:-1]
pattern_capi = pattern.upper()
if pattern_capi in dict.keys():
dict.pop(pattern_capi)
responses = session.query(Response).filter_by(trigger=pattern).all()
for res in responses:
session.delete(res)
session.commit()
elif match.group(2) == "dump":
requests.get("https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}".format(API_KEY, json['message']['chat']['id'], "\n".join(map(str,dict.keys())).encode('utf8')))
else:
answer = ""
for key in dict.keys():
pre = r'((ge)|(ver)|(in)|(on)|(non)|(wan))?'
rex = r'(^|[?!.,:;\s]+)(' + pre + re.escape(key) + r')+[?!.,:;]*($|((\s.*)+))'
match = re.search(rex, json['message']['text'].upper(), re.IGNORECASE)
if match:
answer += dict[key]
answer += "\n"
if answer != "":
requests.get("https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}".format(API_KEY, json['message']['chat']['id'], parse_message(answer,json).encode('utf8')))
def loop(id = 0):
data = requests.get("https://api.telegram.org/bot{}/getUpdates?offset={}&timeout=30".format(API_KEY, id))
data.encoding = 'utf8'
if data is not None:
try:
json = data.json(encoding='utf8')
except:
return id
result = json['result']
if len(result) > 0:
id = result[0]['update_id']
parse_response(result[0])
return id
if __name__ == '__main__':
q = session.query(Response).all()
for response in q:
dict[response.trigger.upper()] = response.response
id = 0
while True:
id = loop(id) + 1
from random import randint
from imgurpython import ImgurClient
def get_random_spongebob():
client_id = 'dac6c4014f7abd0'
client_secret = ''
client = ImgurClient(client_id, client_secret)
images = client.get_album_images('4ybk7')
random_img = images[randint(0, len(images) - 1)]
print(random_img.link)