forked from nightshadows/yachbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yachbot.py
282 lines (235 loc) · 8.58 KB
/
yachbot.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import hashlib
import json
import leveldb
import logging
import optparse
import requests
import sys
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
YachBotConfiguration = collections.namedtuple("YachBotConfiguration", "db_location token")
def read_configuration(configuration_file):
from ConfigParser import ConfigParser
cp = ConfigParser()
if configuration_file in cp.read([configuration_file]):
db_location = cp.get("bot", "db_dir")
token = cp.get("bot", "telegram_token")
return YachBotConfiguration(db_location, token)
CONFIG = read_configuration("./yachbot.cfg")
DB = leveldb.LevelDB(CONFIG.db_location)
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def ParseArgs():
parser = optparse.OptionParser()
return parser.parse_args()
#######################################
#
# database keys:
## size_{room_id} - number of messages in room
## chat_{chat_id} - current room for given companion
## room_{room_nm} - list (space separated) of listeners for room
## message_{index}_{room_id} - message #index in specific room
#
#######################################
def log_info(text):
logger.warn(text)
def getRoomHistorySize(room_id):
room_size = "size_%s" % room_id
try:
return int(DB.Get(room_size))
except:
return 0
def incRoomHistorySize(room_id):
room_size = "size_%s" % room_id
try:
rs = getRoomHistorySize(room_id)
DB.Put(room_size, str(rs + 1))
except:
pass
def getRoomByChat(update):
try:
chat_name = "chat_%d" % update.message.chat_id
return DB.Get(chat_name)
except:
return None
def getChatsByRoom(room_name):
try:
return DB.Get(room_name).split()
except:
return []
def getCommentNumberForReply(update):
try:
reply = update.message.reply_to_message
if not reply:
return None
comment_number = int(reply.text[1:reply.text.find(':')])
return comment_number
except:
return None
def getReplyByChat(update):
try:
room_id = getRoomByChat(update)
comment_number = getCommentNumberForReply(update)
if not comment_number:
return {}
msg_list_record = "mid_%s_%s" % (comment_number, room_id)
msg_idx = DB.Get(msg_list_record).split()
result = {}
for msg in msg_idx:
ch_id, msg_id = msg.split(':')
result[ch_id] = int(msg_id)
return result
except:
return {}
def startcommand(bot, update):
# do nothing, this is just to supress "/start" messages in the chat
pass
def room(bot, update):
if update.message.text == "/room":
try:
bot.sendMessage(update.message.chat_id, text="Please specify the /room name")
except:
pass
return
room_name = "room_%s" % update.message.text
chat_name = "chat_%d" % update.message.chat_id
try:
chat_idx = DB.Get(room_name).split()
except:
chat_idx = []
if update.message.chat_id in chat_idx:
return
exitroom(bot, update)
chat_idx.append(str(update.message.chat_id))
DB.Put(room_name, ' '.join(chat_idx))
DB.Put(chat_name, room_name)
ping(bot, update)
def exitroom(bot, update):
try:
chat_name = "chat_%d" % update.message.chat_id
room_name = DB.Get(chat_name)
DB.Delete(chat_name)
chat_idx = DB.Get(room_name).split()
chat_idx.remove(str(update.message.chat_id))
DB.Put(room_name, ' '.join(chat_idx))
except:
pass
def helpcommand(bot, update):
txt = "Anonymous bot. To enter the channel use '/room <channel name>; To leave the channel '/exit'; To show the current channel name '/ping'."
try:
bot.sendMessage(update.message.chat_id, txt)
except:
error(bot, update, "Help")
def deletecommand(bot, update, args):
try:
room_id = getRoomByChat(update)
comment_number = args[0]
msg_list_record = "mid_%s_%s" % (comment_number, room_id)
msg_idx = DB.Get(msg_list_record).split()
for msg in msg_idx:
ch_id, msg_id = msg.split(':')
try:
bot.editMessageText(chat_id=int(ch_id), message_id=int(msg_id), text=u"РосКомНадзор")
except Exception as e:
print e
except:
pass
def ping(bot, update):
try:
room_id = getRoomByChat(update)
if room_id == None:
bot.sendMessage(update.message.chat_id, text="No channel, use '/room <channel name>' command to enter one")
return
chat_idx = getChatsByRoom(room_id)
bot.sendMessage(update.message.chat_id, text="Channel %s, %d users here, history size is %d" % (room_id[11:], len(chat_idx), getRoomHistorySize(room_id)))
except:
error(bot, update, "Ping")
def get_comment_number_text(number):
if number != None:
return "#%d: " % number
return "None"
def echo(bot, update):
# get the room from the sending user, send message to all users in that room
room_id = getRoomByChat(update)
if room_id == None:
update.message.text = "/room yach"
room(bot, update)
bot.sendMessage(update.message.chat_id, text="You were sent to room 'yach', use '/room <room name>' command to select another one.")
return
rs = getRoomHistorySize(room_id)
message_text = get_comment_number_text(rs) + update.message.text
send_to_sender = False
chat_idx = sorted(set(getChatsByRoom(room_id)))
msg_idx = []
reply_dict = getReplyByChat(update)
reply_number = getCommentNumberForReply(update)
for chat_id in chat_idx:
kwargs = {}
if chat_id in reply_dict:
kwargs['reply_to_message_id'] = reply_dict[chat_id]
message_text = get_comment_number_text(rs) + get_comment_number_text(reply_number) + update.message.text
if (send_to_sender or update.message.chat_id != int(chat_id)) or (room_id == "room_/room test"):
try:
if update.message.sticker:
r = bot.sendSticker(int(chat_id), sticker=update.message.sticker.file_id, **kwargs)
try:
msg_idx.append("%d:%d" % (r.chat.id, r.message_id))
except:
pass
elif update.message.photo:
r = bot.sendPhoto(int(chat_id), photo=update.message.photo[0].file_id, **kwargs)
try:
msg_idx.append("%d:%d" % (r.chat.id, r.message_id))
except:
pass
else:
r = bot.sendMessage(int(chat_id), text=message_text, **kwargs)
try:
msg_idx.append("%d:%d" % (r.chat.id, r.message_id))
except:
pass
except:
pass
try:
history_record = "message_%d_%s" % (rs, room_id)
DB.Put(history_record, message_text.encode('utf-8'))
message_record = "mid_%d_%s" % (rs, room_id)
DB.Put(message_record, ' '.join(msg_idx))
incRoomHistorySize(room_id)
except Exception as e:
error(bot, update, e.text)
def history(bot, update):
try:
room_id = getRoomByChat(update)
rs = getRoomHistorySize(room_id)
historysz = 5
startid = max(0, rs - historysz)
for mess in range(startid, rs):
history_record = "message_%d_%s" % (mess, room_id)
bot.sendMessage(update.message.chat_id, text=DB.Get(history_record).decode('utf-8'))
except:
error(bot, update, "History")
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def yachbot():
updater = Updater(CONFIG.token)
dp = updater.dispatcher
dp.add_handler(CommandHandler("room", room))
dp.add_handler(CommandHandler("start", startcommand))
dp.add_handler(CommandHandler("help", helpcommand))
dp.add_handler(CommandHandler("ping", ping))
dp.add_handler(CommandHandler("history", history))
dp.add_handler(CommandHandler("exit", exitroom))
dp.add_handler(CommandHandler("delete", deletecommand, pass_args=True))
dp.add_handler(MessageHandler(filters=False, callback=echo))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == "__main__":
opt, args = ParseArgs()
yachbot()