Uploading Velasco v1.4.1

- More modularity
- Improved echo
This commit is contained in:
vylion 2017-09-21 17:25:18 +02:00
parent 455373b378
commit 6682510176
7 changed files with 206 additions and 8 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -59,3 +59,7 @@ class Chatlog(object):
return new_log
else:
return Chatlog(lines[0], lines[1], lines[2], lines[4:], int(lines[3]))
def fuse_with(chatlog):
self.count += chatlog.count
self.gen.fuse_with(chatlog.gen)

View file

@ -30,8 +30,8 @@ def triples(wordlist):
class Markov(object):
def __init__(self, text=None, from_json=False):
self.cache = {}
if not from_json:
self.cache = {}
if text is not None:
for line in text:
self.add_text(line)
@ -73,3 +73,11 @@ class Markov(object):
else:
w1, w2 = w2, random.choice(self.cache[getkey(w1, w2)])
return ' '.join(gen_words)
def fuse_with(self, gen):
d = gen.cache
for key in gen.cache:
if key in self.cache:
self.cache[key].extend(d[key])
else:
self.cache[key] = list(d[key])

180
ocsalev.py Normal file
View file

@ -0,0 +1,180 @@
#!/usr/bin/env python3
# FAILED ATTEMPT TO MAKE BOT THAT USES VELASCO MEMORY IN ALL GROUPS SIMULTANEOUSLY
import sys, os
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram.error import *
from markov import *
from velasco import GUILLERMO_ID, LOG_DIR, LOG_EXT
import logging
import argparse
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
generator = Markov()
chatfreqs = {}
DEFAULT_FREQ = 10
def parse_file(text):
lines = text.splitlines()
if lines[1] != "private":
ident = lines[0]
freq = int(lines[3])
chatfreqs[ident] = (freq, 0)
if lines[4] == "dict:":
cache = '\n'.join(lines[5:])
gen = Markov.from_json(cache)
return gen
else:
return Markov(lines[4:])
else:
return Markov()
def load_gen(path):
open_file = open(path, 'r')
gen = parse_file(open_file.read())
open_file.close()
return gen
def wake(bot):
global generator
directory = os.fsencode(LOG_DIR)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(LOG_EXT):
gen = load_gen(LOG_DIR + filename)
generator.fuse_with(gen)
def start(bot, update):
update.message.reply_text('WHADDUP NERD')
def help(bot, update):
update.message.reply_text("""I ANSWER TO
/start - HELLO
/about - MY BIOGRAPHY
/help - THIS
/freq - HOW LONG I WAIT TO SPEAK
/speak - I SPEAK
""")
def about(bot, update):
update.message.reply_text('I AM LIKE @velascobot BUT STRONGER. THE TRUE SELF')
def echo(bot, update):
text = update.message.text.split(None, 2)
if len(text) > 1:
text = text[1]
update.message.reply_text(text)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def read(bot, update):
global generator
if not "group" in update.message.chat.type:
update.message.reply_text("I ONLY TALK IN GROUPS")
return
generator.add_text(update.message.text + TAIL)
chat = update.message.chat
ident = str(chat.id)
if not ident in chatfreqs:
chatfreqs[ident] = (DEFAULT_FREQ, 0)
freq, count = chatfreqs[ident]
if count%freq == 0:
msg = generator.generate_markov_text()
try:
bot.sendMessage(ident, msg)
count = 0
except TimedOut:
chatfreqs[ident] = (freq + CHAT_INC, count)
print("Increased freq for chat " + chat.title + " [" + ident + "]")
chatfreqs[ident] = (freq, count+1)
def speak(bot, update):
global generator
if not "group" in update.message.chat.type:
update.message.reply_text("I ONLY TALK IN GROUPS")
return
chat = update.message.chat
ident = str(chat.id)
if not ident in chatfreqs:
chatfreqs[ident] = (DEFAULT_FREQ, 0)
msg = generator.generate_markov_text()
update.message.reply_text(msg)
def get_chatlogs(bot, update):
if str(update.message.chat.id) == GUILLERMO_ID:
bot.sendMessage(GUILLERMO_ID, "HECK YOU")
def set_freq(bot, update):
ident = str(update.message.chat.id)
if not ident in chatfreqs:
chatfreqs[ident] = (DEFAULT_FREQ, 0)
freq, count = chatfreqs[ident]
if not len(update.message.text.split()) > 1:
reply = "I WAIT FOR " + str(freq) + " MESSAGES"
else:
try:
value = update.message.text.split()[1]
value = int(value)
chatfreqs[ident] = (value, count)
reply = "I NOW WAIT FOR " + str(value) + " MESSAGES"
if value > freq:
reply += "\nYOU WILL NOT SILENCE ME"
except:
reply = "WHAT THE HECK. IMMA STILL WAIT FOR " + str(freq) + " MESSAGES"
update.message.reply_text(reply)
def stop(bot, update):
chat = update.message.chat
ident = str(chat.id)
del chatfreqs[ident]
def main():
parser = argparse.ArgumentParser(description='A Telegram markovbot.')
parser.add_argument('token', metavar='TOKEN', help='The Bot Token to work with the Telegram Bot API')
args = parser.parse_args()
# Create the EventHandler and pass it your bot's token.
updater = Updater(args.token)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("about", about))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("freq", set_freq))
dp.add_handler(CommandHandler("list", get_chatlogs))
dp.add_handler(CommandHandler("stop", stop))
dp.add_handler(CommandHandler("speak", speak))
# on noncommand i.e message - echo the message on Telegram
# dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_handler(MessageHandler(Filters.text, read))
# log all errors
dp.add_error_handler(error)
wake(updater.bot)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()

View file

@ -19,14 +19,16 @@ disabled = {}
GUILLERMO_ID = "8379173"
CHAT_INC = 5
CHAT_SAVE = 15
LOG_DIR = "chatlogs/"
LOG_EXT = ".txt"
def wake(bot):
directory = os.fsencode("chatlogs/")
directory = os.fsencode(LOG_DIR)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
chat = loadchat("chatlogs/" + filename)
if filename.endswith(LOG_EXT):
chat = loadchat(LOG_DIR + filename)
chatlogs[chat.id] = chat
print("loaded chat " + chat.title + " [" + chat.id + "]")
continue
@ -45,7 +47,7 @@ def start(bot, update):
update.message.reply_text('cowabunga')
def savechat(chatlog):
open_file = open('chatlogs/' + chatlog.id + '.txt', 'w')
open_file = open(LOG_DIR + chatlog.id + LOG_EXT, 'w')
open_file.write(chatlog.to_txt())
open_file.close()
@ -70,7 +72,11 @@ def about(bot, update):
update.message.reply_text('I am yet another Markov Bot experiment. I read everything you type to me and then spit back nonsensical messages that look like yours')
def echo(bot, update):
update.message.reply_text(update.message.text)
text = update.message.text.split(None, 2)
if len(text) > 1:
text = text[1]
chatlog.add_msg(text)
update.message.reply_text(text)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
@ -168,7 +174,7 @@ def set_freq(bot, update):
def stop(bot, update):
chatlog = chatlogs[update.message.chat.id]
del chatlogs[chatlog.id]
os.remove("chatlogs/" + chatlog.id + ".txt")
os.remove(LOG_DIR + chatlog.id + LOG_EXT)
print("I got blocked. Removed user " + chatlog.id)
def main():
@ -197,7 +203,7 @@ def main():
# dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_handler(MessageHandler(Filters.text, read))
# chatlog all errors
# log all errors
dp.add_error_handler(error)
wake(updater.bot)