Velasco 2.0

- Added sticker functionality
- Refactored internal tags
This commit is contained in:
vylion 2018-04-23 12:53:03 +02:00
parent 7de67c0238
commit 1f282027fc
3 changed files with 51 additions and 23 deletions

View file

@ -2,6 +2,13 @@
from markov import *
def parse_line(l):
s = l.split('=')
if len(s) < 2:
return ""
else:
return s[1]
class Chatlog(object):
def __init__(self, ident, chattype, title, text=None, freq=None, answer=0.5):
self.id = str(ident)
@ -42,7 +49,11 @@ class Chatlog(object):
return self.answer
def add_msg(self, message):
self.gen.add_text(message + " !kvl")
self.gen.add_text(message + ' ' + TAIL)
self.count += 1
def add_sticker(self, file_id):
self.gen.add_text(STICKER_TAG + ' ' + file_id + ' ' + TAIL)
self.count += 1
def speak(self):
@ -72,6 +83,8 @@ class Chatlog(object):
def from_txt(text):
lines = text.splitlines()
#print("Line 4=" + lines[4])
print("Line 0=" + parse_line(lines[0]))
if(parse_line(lines[0]) == "v2"):
new_log = Chatlog(parse_line(lines[1]), parse_line(lines[2]), parse_line(lines[3]), None, int(parse_line(lines[4])), float(parse_line(lines[5])))
new_log.count = int(parse_line(lines[6]))
@ -91,12 +104,6 @@ class Chatlog(object):
else:
return Chatlog(lines[0], lines[1], lines[2], lines[4:], int(lines[3]))
def parse_line(line):
s = line.split('=')
if len(s) < 2:
return ""
return s[1]
def fuse_with(chatlog):
self.count += chatlog.count
self.gen.fuse_with(chatlog.gen)

View file

@ -3,8 +3,9 @@
import random
import json
HEAD = "\n!kvl"
TAIL = "!kvl"
HEAD = "\n^MESSAGE_SEPARATOR^"
TAIL = "^MESSAGE_SEPARATOR^"
STICKER_TAG = "^IS_STICKER^"
def trim_and_split(text):
words = text.replace('\n', '\n ').split(' ')
@ -100,6 +101,6 @@ class Markov(object):
count = 0
for key in self.cache:
for word in self.cache[key]:
if word == "!kvl":
if word == MESSAGE_SEPARATOR:
count += 1
return count

View file

@ -31,22 +31,23 @@ def wake(bot):
filename = os.fsdecode(file)
if filename.endswith(LOG_EXT):
chat = loadchat(LOG_DIR + filename)
chatlogs[chat.id] = chat
print("loaded chat " + chat.title + " [" + chat.id + "]")
if chat is not None:
chatlogs[chat.id] = chat
print("loaded chat " + chat.title + " [" + chat.id + "]")
continue
else:
continue
"""
for c in chatlogs:
try:
bot.sendMessage(chatlogs[c].id, "Good morning. I just woke up")
send_message(bot, update, "Good morning. I just woke up", False)
except:
pass
#del chatlogs[c]
"""
def start(bot, update):
update.message.reply_text('cowabunga')
update.message.reply_text()
def savechat(chatlog):
open_file = open(LOG_DIR + chatlog.id + LOG_EXT, 'w')
@ -54,7 +55,9 @@ def savechat(chatlog):
open_file.close()
def loadchat(path):
#print("Loading chat: " + path)
open_file = open(path, 'r')
chat = None
try:
chat = Chatlog.from_txt(open_file.read())
except:
@ -111,28 +114,33 @@ def read(bot, update):
chatlog = Chatlog(chat.id, chat.type, title)
else:
chatlog = chatlogs[ident]
chatlog.add_msg(update.message.text)
if update.message.text is not None:
chatlog.add_msg(update.message.text)
elif update.message.sticker is not None:
chatlog.add_sticker(update.message.sticker.file_id)
replied = update.message.reply_to_message
if (replied is not None) and (replied.from_user.name == "@velascobot") and chatlog.answering(random.random()):
print("They're talking to me, I'm answering back")
msg = chatlog.speak()
update.message.reply_text(msg)
send_message(bot, update, msg, True)
if random.random() <= REPT_CHANCE:
msg = chatlog.speak()
bot.sendMessage(chatlog.id, msg)
send_message(bot, update, msg, False)
elif chatlog.get_count()%chatlog.freq == 0:
msg = chatlog.speak()
try:
if random.random() <= REPL_CHANCE:
print("I made a reply")
update.message.reply_text(msg)
send_message(bot, update, msg, True)
else:
print("I sent a message")
bot.sendMessage(chatlog.id, msg)
send_message(bot, update, msg, False)
if random.random() <= REPT_CHANCE:
print("And a followup")
msg = chatlog.speak()
bot.sendMessage(chatlog.id, msg)
send_message(bot, update, msg, False)
except TimedOut:
chatlog.set_freq(chatlog.freq + CHAT_INC)
print("Increased freq for chat " + chatlog.title + " [" + chatlog.id + "]")
@ -156,15 +164,27 @@ def speak(bot, update):
if len(text) > 1:
chatlog.add_msg(' '.join(text[1:]))
msg = chatlog.speak()
update.message.reply_text(msg)
send_message(bot, update, msg, True)
savechat(chatlog)
chatlogs[chatlog.id] = chatlog
def send_message(bot, update, msg, is_reply):
words = msg.split()
if words[0] == STICKER_TAG:
if is_reply:
update.message.reply_sticker(words[1])
else:
bot.sendSticker(update.message.chat_id, words[1])
elif is_reply:
update.message.reply_text(msg)
else:
bot.sendMessage(update.message.chat.id, msg)
def get_chatlogs(bot, update):
m = "I have these chatlogs:"
for c in chatlogs:
m += "\n" + chatlogs[c].id + " " + chatlogs[c].title
update.message.reply_text(m)
send_message(bot, update, msg, True)
def get_id(bot, update):
update.message.reply_text("This chat's id is: " + str(update.message.chat.id))
@ -258,7 +278,7 @@ def main():
# on noncommand i.e message - echo the message on Telegram
# dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_handler(MessageHandler(Filters.text, read))
dp.add_handler(MessageHandler((Filters.text | Filters.sticker), read))
# log all errors
dp.add_error_handler(error)