kertser commited on
Commit
768ac34
1 Parent(s): 71acbff

Upload WarOnline_Chat.py

Browse files

Updated with option to answer direct messages, starting with @WarBot

Files changed (1) hide show
  1. WarOnline_Chat.py +32 -10
WarOnline_Chat.py CHANGED
@@ -189,12 +189,19 @@ def getMessages(thread_url=config.thread_url, quotedUser="", startingPage=1):
189
  if blockquote:
190
  # Extract the text
191
  text = data.find('div', {'class': 'bbWrapper'})
 
192
  for bq in text.find_all('blockquote'):
193
  bq.extract()
194
  reply = text.get_text().replace('\n', ' ').strip()
195
 
196
  allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': quotedID})
197
 
 
 
 
 
 
 
198
  except:
199
  continue # There was no text in this quote, move to the next
200
 
@@ -225,7 +232,13 @@ def WarOnlineBot():
225
  # IDs of the quoted messages, replied by the bot:
226
  messages_by_bot_IDs = []
227
 
 
 
 
228
  for msg in allMessages:
 
 
 
229
  # Set a list of replied messages IDs
230
  if msg['messengerName'] == config.username: #message posted by the WarBot
231
  messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
@@ -235,6 +248,10 @@ def WarOnlineBot():
235
  # All messages (with quotes) sent _FOR_ the Bot:
236
  messagesForBot = getMessages(thread_url=config.thread_url, quotedUser=config.username, startingPage=startingPage)
237
 
 
 
 
 
238
  # IDs of the messages, quoting the bot:
239
  messages_for_bot_IDs = []
240
 
@@ -247,6 +264,7 @@ def WarOnlineBot():
247
  # Filter to leave just the unanswered messages IDs:
248
  messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]
249
 
 
250
  # Reply the unanswered messages:
251
  for msg in messagesForBot:
252
  if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
@@ -265,16 +283,19 @@ def WarOnlineBot():
265
 
266
  # Init Connection
267
  db = conversationDB.DataBase()
268
- # Get the previous dialogue from the database
269
- dbmessages = db.getmessages(msg['messengerName'])
270
- for dbmessage in dbmessages:
271
- previous_dialogue += dbmessage[0]+' '+dbmessage[1]+' '
272
- # Update the string and preprocess it
273
- quote = previous_dialogue + quote
274
- quote = remove_non_english_russian_chars(quote)
275
- quote = remove_extra_spaces(quote)
276
- # Truncate the quote to return only the last MaxWords of words:
277
- quote = " ".join(quote.split()[-config.MaxWords:])
 
 
 
278
 
279
  # Fix the quote string, to eliminate errors:
280
  quote = fixString(quote)
@@ -314,6 +335,7 @@ if __name__ == '__main__':
314
  while True:
315
  print('Starting Session')
316
  WarOnlineBot()
 
317
 
318
  timer = range(60 * config.timeout)
319
  for t in timer:
 
189
  if blockquote:
190
  # Extract the text
191
  text = data.find('div', {'class': 'bbWrapper'})
192
+
193
  for bq in text.find_all('blockquote'):
194
  bq.extract()
195
  reply = text.get_text().replace('\n', ' ').strip()
196
 
197
  allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': quotedID})
198
 
199
+ else: # Looking for a direct message "@WarBot"
200
+ text = data.find('div', {'class': 'bbWrapper'})
201
+ if text.get_text().startswith('@WarBot'):
202
+ reply = text.get_text().replace('@WarBot','').replace('\n', ' ').strip()
203
+ allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': 'post: 0'})
204
+
205
  except:
206
  continue # There was no text in this quote, move to the next
207
 
 
232
  # IDs of the quoted messages, replied by the bot:
233
  messages_by_bot_IDs = []
234
 
235
+ # Initiate the direct messages
236
+ direct_messages = []
237
+
238
  for msg in allMessages:
239
+ # Direct message to the bot
240
+ if msg['quotedID'].split(': ')[-1] == '0': #debug
241
+ direct_messages.append(msg)
242
  # Set a list of replied messages IDs
243
  if msg['messengerName'] == config.username: #message posted by the WarBot
244
  messages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])
 
248
  # All messages (with quotes) sent _FOR_ the Bot:
249
  messagesForBot = getMessages(thread_url=config.thread_url, quotedUser=config.username, startingPage=startingPage)
250
 
251
+ # Append the direct messages to the messagesForBot:
252
+ for msg in direct_messages:
253
+ messagesForBot.append(msg)
254
+
255
  # IDs of the messages, quoting the bot:
256
  messages_for_bot_IDs = []
257
 
 
264
  # Filter to leave just the unanswered messages IDs:
265
  messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]
266
 
267
+
268
  # Reply the unanswered messages:
269
  for msg in messagesForBot:
270
  if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:
 
283
 
284
  # Init Connection
285
  db = conversationDB.DataBase()
286
+
287
+ if msg['quotedID'].split(': ')[-1] != '0': # It is dialogue. Look-up for the previous quotes
288
+
289
+ # Get the previous dialogue from the database
290
+ dbmessages = db.getmessages(msg['messengerName'])
291
+ for dbmessage in dbmessages:
292
+ previous_dialogue += dbmessage[0]+' '+dbmessage[1]+' '
293
+ # Update the string and preprocess it
294
+ quote = previous_dialogue + quote
295
+ quote = remove_non_english_russian_chars(quote)
296
+ quote = remove_extra_spaces(quote)
297
+ # Truncate the quote to return only the last MaxWords of words:
298
+ quote = " ".join(quote.split()[-config.MaxWords:])
299
 
300
  # Fix the quote string, to eliminate errors:
301
  quote = fixString(quote)
 
335
  while True:
336
  print('Starting Session')
337
  WarOnlineBot()
338
+ print('Session finished. Timeout...')
339
 
340
  timer = range(60 * config.timeout)
341
  for t in timer: