Upload WarOnline_Chat.py
Browse filesUpdated with option to answer direct messages, starting with @WarBot
188 unmodified lines18919019119219319419519619719819920024 unmodified lines2252262272282292302313 unmodified lines2352362372382392406 unmodified lines24724824925025125212 unmodified lines26526626726826927027127227327427527627727827928033 unmodified lines314315316317318319188 unmodified linesif blockquote:# Extract the texttext = data.find('div', {'class': 'bbWrapper'})for bq in text.find_all('blockquote'):bq.extract()reply = text.get_text().replace('\n', ' ').strip()allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': quotedID})except:continue # There was no text in this quote, move to the next24 unmodified lines# IDs of the quoted messages, replied by the bot:messages_by_bot_IDs = []for msg in allMessages:# Set a list of replied messages IDsif msg['messengerName'] == config.username: #message posted by the WarBotmessages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])3 unmodified lines# All messages (with quotes) sent _FOR_ the Bot:messagesForBot = getMessages(thread_url=config.thread_url, quotedUser=config.username, startingPage=startingPage)# IDs of the messages, quoting the bot:messages_for_bot_IDs = []6 unmodified lines# Filter to leave just the unanswered messages IDs:messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]# Reply the unanswered messages:for msg in messagesForBot:if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:12 unmodified lines# Init Connectiondb = conversationDB.DataBase()# Get the previous dialogue from the databasedbmessages = db.getmessages(msg['messengerName'])for dbmessage in dbmessages:previous_dialogue += dbmessage[0]+' '+dbmessage[1]+' '# Update the string and preprocess itquote = previous_dialogue + quotequote = remove_non_english_russian_chars(quote)quote = remove_extra_spaces(quote)# Truncate the quote to return only the last MaxWords of words:quote = " ".join(quote.split()[-config.MaxWords:])# Fix the quote string, to eliminate errors:quote = fixString(quote)33 unmodified lineswhile True:print('Starting Session')WarOnlineBot()timer = range(60 * config.timeout)for t in timer:188 unmodified lines18919019119219319419519619719819920020120220320420520620724 unmodified lines2322332342352362372382392402412422432443 unmodified lines2482492502512522532542552562576 unmodified lines26426526626726826927012 unmodified lines28328428528628728828929029129229329429529629729829930030133 unmodified lines335336337338339340341188 unmodified linesif blockquote:# Extract the texttext = data.find('div', {'class': 'bbWrapper'})for bq in text.find_all('blockquote'):bq.extract()reply = text.get_text().replace('\n', ' ').strip()allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': quotedID})else: # Looking for a direct message "@WarBot"text = data.find('div', {'class': 'bbWrapper'})if text.get_text().startswith('@WarBot'):reply = text.get_text().replace('@WarBot','').replace('\n', ' ').strip()allquotes.append({'reply': reply, 'messengerName': messengerName, 'messageID': messageID, 'quotedID': 'post: 0'})except:continue # There was no text in this quote, move to the next24 unmodified lines# IDs of the quoted messages, replied by the bot:messages_by_bot_IDs = []# Initiate the direct messagesdirect_messages = []for msg in allMessages:# Direct message to the botif msg['quotedID'].split(': ')[-1] == '0': #debugdirect_messages.append(msg)# Set a list of replied messages IDsif msg['messengerName'] == config.username: #message posted by the WarBotmessages_by_bot_IDs.append(msg['quotedID'].split(': ')[-1])3 unmodified lines# All messages (with quotes) sent _FOR_ the Bot:messagesForBot = getMessages(thread_url=config.thread_url, quotedUser=config.username, startingPage=startingPage)# Append the direct messages to the messagesForBot:for msg in direct_messages:messagesForBot.append(msg)# IDs of the messages, quoting the bot:messages_for_bot_IDs = []6 unmodified lines# Filter to leave just the unanswered messages IDs:messages_for_bot_IDs = [ID for ID in messages_for_bot_IDs if ID not in messages_by_bot_IDs]# Reply the unanswered messages:for msg in messagesForBot:if msg['messageID'].split('-')[-1] in messages_for_bot_IDs:12 unmodified lines# Init Connectiondb = conversationDB.DataBase()if msg['quotedID'].split(': ')[-1] != '0': # It is dialogue. Look-up for the previous quotes# Get the previous dialogue from the databasedbmessages = db.getmessages(msg['messengerName'])for dbmessage in dbmessages:previous_dialogue += dbmessage[0]+' '+dbmessage[1]+' '# Update the string and preprocess itquote = previous_dialogue + quotequote = remove_non_english_russian_chars(quote)quote = remove_extra_spaces(quote)# Truncate the quote to return only the last MaxWords of words:quote = " ".join(quote.split()[-config.MaxWords:])# Fix the quote string, to eliminate errors:quote = fixString(quote)33 unmodified lineswhile True:print('Starting Session')WarOnlineBot()print('Session finished. Timeout...')timer = range(60 * config.timeout)for t in timer:
WarOnline_Chat.py+32-10