-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
129 lines (110 loc) · 4.37 KB
/
bot.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import telepot, time, json, os, datetime, threading, logging, sys
from reactosrss import ReactosRss
from telepot.loop import MessageLoop
from telepot.exception import BotWasKickedError, BotWasBlockedError
logging.basicConfig(
filename="jirainformer.log",
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s"
)
histfilename = 'history.json'
configfilename = 'config.json'
def save_setts():
with open(configfilename, 'w') as f:
txt = json.dumps(settings, indent=4, sort_keys=True)
f.write(txt)
def create_settings():
settings = { 'bot_token':'', 'chat_ids':[] }
settings['bot_token'] = input("Enter bot token:")
save_setts()
def save_history():
with open(histfilename, 'w') as f:
txt = json.dumps(history, indent=4, sort_keys=True)
f.write(txt)
def add_history_entry(el):
if len(history) > 10:
history['rectosrss_last_posts'].pop()
history['rectosrss_last_posts'].insert(0, el)
if os.path.isfile(configfilename):
with open(configfilename) as config:
settings = json.load(config)
else:
create_settings()
if os.path.isfile(histfilename):
with open(histfilename) as histconfig:
history = json.load(histconfig)
else:
history = { 'reactosrss_last_post':1504345539.0,
'rectosrss_last_posts': []
}
save_history()
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
logging.info("Handled {0} message from {1} chat with id: {2};".format(content_type, chat_type, chat_id))
if content_type == 'text':
if msg['text'] == '/start' or msg['text'] == '/start@reactosspamerbot':
if not(chat_id in settings['chat_ids']):
bot.sendMessage(chat_id, "Now I'm working here!")
settings['chat_ids'].append(chat_id)
save_setts()
logging.info('Bot was started in {0} chat with id: {1}'.format(chat_type, chat_id))
else:
bot.sendMessage(chat_id, "I'm already working!")
elif msg['text'] == '/help' or msg['text'] == '/help@reactosspamerbot':
bot.sendMessage(chat_id, "Спасибо за то, что открыли помощь!")
elif msg['text'] == '/stop' or msg['text'] == '/stop@reactosspamerbot':
settings['chat_ids'].remove(chat_id)
bot.sendMessage(chat_id, "Good bye!")
save_setts()
logging.info('Bot was stopped in {0} chat with id: {1}'.format(chat_type, chat_id))
elif chat_type == 'private':
bot.sendMessage(chat_id, "Неправильная команда!\nВот правильные:\n/help - открыть помощь.")
bot = telepot.Bot(settings['bot_token'])
me = bot.getMe()
print(me)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')
def reactosrss_posting_thread():
while True:
try:
els = ReactosRss.get()
except Exception as e:
logging.error("Error while receiving RSS from Jira: {0}".format(e))
time.sleep(10)
else:
for el in reversed(els):
postdatetime = datetime.datetime.strptime(el['pubDate'], "%a, %d %b %Y %H:%M:%S %z")
postunixtime = time.mktime(postdatetime.timetuple())
if postunixtime < (history['reactosrss_last_post'] +1):
continue
else:
history['reactosrss_last_post'] = postunixtime
save_history()
for old_el in history['rectosrss_last_posts']:
if (old_el['title'] == el['title']) and (old_el['fixversion'] == el['fixversion']) and (old_el['status'] == el['status']):
break
else:
if el['fixversion'] != '':
post = '*{0}*\nwas resolved as *{1}* by [{2}]({3}) and will be merged in *{4}*\n{5}'.format(el['title'], el['status'],el['resolver'], el['resolverlink'], el['fixversion'], el['link'])
else:
post = '*{0}*\nwas resolved as *{1}* by [{2}]({3})\n{4}'.format(el['title'], el['status'],el['resolver'], el['resolverlink'], el['link'])
for cid in settings['chat_ids'][:]:
for _ in range(3):
try:
bot.sendMessage(cid, post, parse_mode='Markdown')
except (BotWasKickedError, BotWasBlockedError) as e:
settings['chat_ids'].remove(cid)
save_setts()
logging.info("Bot was kicked or blocked from chat with id: {0}".format(cid))
except Exception as e:
logging.error("Unknown error while sending message: {0}; {1}".format(e, sys.exc_info()[0]))
continue
else:
break
add_history_entry(el)
time.sleep(60)
t = threading.Thread(target=reactosrss_posting_thread)
t.daemon = True
t.start()
while 1:
time.sleep(1000)