mirror of
https://gitlab.com/vylion/velascobot.git
synced 2025-04-19 21:46:35 +02:00
Velasco 2.0
- Added sticker functionality - Refactored internal tags
This commit is contained in:
parent
7de67c0238
commit
1f282027fc
3 changed files with 51 additions and 23 deletions
21
chatlog.py
21
chatlog.py
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
from markov import *
|
from markov import *
|
||||||
|
|
||||||
|
def parse_line(l):
|
||||||
|
s = l.split('=')
|
||||||
|
if len(s) < 2:
|
||||||
|
return ""
|
||||||
|
else:
|
||||||
|
return s[1]
|
||||||
|
|
||||||
class Chatlog(object):
|
class Chatlog(object):
|
||||||
def __init__(self, ident, chattype, title, text=None, freq=None, answer=0.5):
|
def __init__(self, ident, chattype, title, text=None, freq=None, answer=0.5):
|
||||||
self.id = str(ident)
|
self.id = str(ident)
|
||||||
|
@ -42,7 +49,11 @@ class Chatlog(object):
|
||||||
return self.answer
|
return self.answer
|
||||||
|
|
||||||
def add_msg(self, message):
|
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
|
self.count += 1
|
||||||
|
|
||||||
def speak(self):
|
def speak(self):
|
||||||
|
@ -72,6 +83,8 @@ class Chatlog(object):
|
||||||
|
|
||||||
def from_txt(text):
|
def from_txt(text):
|
||||||
lines = text.splitlines()
|
lines = text.splitlines()
|
||||||
|
#print("Line 4=" + lines[4])
|
||||||
|
print("Line 0=" + parse_line(lines[0]))
|
||||||
if(parse_line(lines[0]) == "v2"):
|
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 = 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]))
|
new_log.count = int(parse_line(lines[6]))
|
||||||
|
@ -91,12 +104,6 @@ class Chatlog(object):
|
||||||
else:
|
else:
|
||||||
return Chatlog(lines[0], lines[1], lines[2], lines[4:], int(lines[3]))
|
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):
|
def fuse_with(chatlog):
|
||||||
self.count += chatlog.count
|
self.count += chatlog.count
|
||||||
self.gen.fuse_with(chatlog.gen)
|
self.gen.fuse_with(chatlog.gen)
|
||||||
|
|
|
@ -3,8 +3,9 @@
|
||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
|
|
||||||
HEAD = "\n!kvl"
|
HEAD = "\n^MESSAGE_SEPARATOR^"
|
||||||
TAIL = "!kvl"
|
TAIL = "^MESSAGE_SEPARATOR^"
|
||||||
|
STICKER_TAG = "^IS_STICKER^"
|
||||||
|
|
||||||
def trim_and_split(text):
|
def trim_and_split(text):
|
||||||
words = text.replace('\n', '\n ').split(' ')
|
words = text.replace('\n', '\n ').split(' ')
|
||||||
|
@ -100,6 +101,6 @@ class Markov(object):
|
||||||
count = 0
|
count = 0
|
||||||
for key in self.cache:
|
for key in self.cache:
|
||||||
for word in self.cache[key]:
|
for word in self.cache[key]:
|
||||||
if word == "!kvl":
|
if word == MESSAGE_SEPARATOR:
|
||||||
count += 1
|
count += 1
|
||||||
return count
|
return count
|
||||||
|
|
46
velasco.py
46
velasco.py
|
@ -31,22 +31,23 @@ def wake(bot):
|
||||||
filename = os.fsdecode(file)
|
filename = os.fsdecode(file)
|
||||||
if filename.endswith(LOG_EXT):
|
if filename.endswith(LOG_EXT):
|
||||||
chat = loadchat(LOG_DIR + filename)
|
chat = loadchat(LOG_DIR + filename)
|
||||||
chatlogs[chat.id] = chat
|
if chat is not None:
|
||||||
print("loaded chat " + chat.title + " [" + chat.id + "]")
|
chatlogs[chat.id] = chat
|
||||||
|
print("loaded chat " + chat.title + " [" + chat.id + "]")
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
"""
|
"""
|
||||||
for c in chatlogs:
|
for c in chatlogs:
|
||||||
try:
|
try:
|
||||||
bot.sendMessage(chatlogs[c].id, "Good morning. I just woke up")
|
send_message(bot, update, "Good morning. I just woke up", False)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
#del chatlogs[c]
|
#del chatlogs[c]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def start(bot, update):
|
def start(bot, update):
|
||||||
update.message.reply_text('cowabunga')
|
update.message.reply_text()
|
||||||
|
|
||||||
def savechat(chatlog):
|
def savechat(chatlog):
|
||||||
open_file = open(LOG_DIR + chatlog.id + LOG_EXT, 'w')
|
open_file = open(LOG_DIR + chatlog.id + LOG_EXT, 'w')
|
||||||
|
@ -54,7 +55,9 @@ def savechat(chatlog):
|
||||||
open_file.close()
|
open_file.close()
|
||||||
|
|
||||||
def loadchat(path):
|
def loadchat(path):
|
||||||
|
#print("Loading chat: " + path)
|
||||||
open_file = open(path, 'r')
|
open_file = open(path, 'r')
|
||||||
|
chat = None
|
||||||
try:
|
try:
|
||||||
chat = Chatlog.from_txt(open_file.read())
|
chat = Chatlog.from_txt(open_file.read())
|
||||||
except:
|
except:
|
||||||
|
@ -111,28 +114,33 @@ def read(bot, update):
|
||||||
chatlog = Chatlog(chat.id, chat.type, title)
|
chatlog = Chatlog(chat.id, chat.type, title)
|
||||||
else:
|
else:
|
||||||
chatlog = chatlogs[ident]
|
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
|
replied = update.message.reply_to_message
|
||||||
if (replied is not None) and (replied.from_user.name == "@velascobot") and chatlog.answering(random.random()):
|
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")
|
print("They're talking to me, I'm answering back")
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
update.message.reply_text(msg)
|
send_message(bot, update, msg, True)
|
||||||
if random.random() <= REPT_CHANCE:
|
if random.random() <= REPT_CHANCE:
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
bot.sendMessage(chatlog.id, msg)
|
send_message(bot, update, msg, False)
|
||||||
elif chatlog.get_count()%chatlog.freq == 0:
|
elif chatlog.get_count()%chatlog.freq == 0:
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
try:
|
try:
|
||||||
if random.random() <= REPL_CHANCE:
|
if random.random() <= REPL_CHANCE:
|
||||||
print("I made a reply")
|
print("I made a reply")
|
||||||
update.message.reply_text(msg)
|
send_message(bot, update, msg, True)
|
||||||
else:
|
else:
|
||||||
print("I sent a message")
|
print("I sent a message")
|
||||||
bot.sendMessage(chatlog.id, msg)
|
send_message(bot, update, msg, False)
|
||||||
if random.random() <= REPT_CHANCE:
|
if random.random() <= REPT_CHANCE:
|
||||||
print("And a followup")
|
print("And a followup")
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
bot.sendMessage(chatlog.id, msg)
|
send_message(bot, update, msg, False)
|
||||||
except TimedOut:
|
except TimedOut:
|
||||||
chatlog.set_freq(chatlog.freq + CHAT_INC)
|
chatlog.set_freq(chatlog.freq + CHAT_INC)
|
||||||
print("Increased freq for chat " + chatlog.title + " [" + chatlog.id + "]")
|
print("Increased freq for chat " + chatlog.title + " [" + chatlog.id + "]")
|
||||||
|
@ -156,15 +164,27 @@ def speak(bot, update):
|
||||||
if len(text) > 1:
|
if len(text) > 1:
|
||||||
chatlog.add_msg(' '.join(text[1:]))
|
chatlog.add_msg(' '.join(text[1:]))
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
update.message.reply_text(msg)
|
send_message(bot, update, msg, True)
|
||||||
savechat(chatlog)
|
savechat(chatlog)
|
||||||
chatlogs[chatlog.id] = 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):
|
def get_chatlogs(bot, update):
|
||||||
m = "I have these chatlogs:"
|
m = "I have these chatlogs:"
|
||||||
for c in chatlogs:
|
for c in chatlogs:
|
||||||
m += "\n" + chatlogs[c].id + " " + chatlogs[c].title
|
m += "\n" + chatlogs[c].id + " " + chatlogs[c].title
|
||||||
update.message.reply_text(m)
|
send_message(bot, update, msg, True)
|
||||||
|
|
||||||
def get_id(bot, update):
|
def get_id(bot, update):
|
||||||
update.message.reply_text("This chat's id is: " + str(update.message.chat.id))
|
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
|
# on noncommand i.e message - echo the message on Telegram
|
||||||
# dp.add_handler(MessageHandler(Filters.text, echo))
|
# 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
|
# log all errors
|
||||||
dp.add_error_handler(error)
|
dp.add_error_handler(error)
|
||||||
|
|
Loading…
Reference in a new issue