mirror of
https://gitlab.com/vylion/velascobot.git
synced 2025-04-19 21:46:35 +02:00
velasco 2.1
- Added the possibility of answering a message in the interval since the last Velasco message when chatting because of freq
This commit is contained in:
parent
f485a715a6
commit
2d93cad2e6
2 changed files with 44 additions and 15 deletions
14
chatlog.py
14
chatlog.py
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import random
|
||||||
from markov import *
|
from markov import *
|
||||||
|
|
||||||
def parse_line(l):
|
def parse_line(l):
|
||||||
|
@ -25,6 +26,7 @@ class Chatlog(object):
|
||||||
self.count = len(text)
|
self.count = len(text)
|
||||||
else:
|
else:
|
||||||
self.count = 0
|
self.count = 0
|
||||||
|
self.replyables = []
|
||||||
self.answer = answer
|
self.answer = answer
|
||||||
self.gen = Markov(text)
|
self.gen = Markov(text)
|
||||||
|
|
||||||
|
@ -69,6 +71,18 @@ class Chatlog(object):
|
||||||
return False
|
return False
|
||||||
return rand <= self.answer
|
return rand <= self.answer
|
||||||
|
|
||||||
|
def add_replyable(self, msg_id):
|
||||||
|
self.replyables.append(msg_id)
|
||||||
|
|
||||||
|
def restart_replyables(self, msg_id):
|
||||||
|
if msg_id is not None:
|
||||||
|
self.replyables = [msg_id]
|
||||||
|
else:
|
||||||
|
self.replyables = []
|
||||||
|
|
||||||
|
def get_replyable(self):
|
||||||
|
random.choice(self.replyables)
|
||||||
|
|
||||||
def to_txt(self):
|
def to_txt(self):
|
||||||
lines = ["DICT=v2"]
|
lines = ["DICT=v2"]
|
||||||
lines.append("CHAT_ID=" + self.id)
|
lines.append("CHAT_ID=" + self.id)
|
||||||
|
|
45
velasco.py
45
velasco.py
|
@ -17,6 +17,7 @@ logger = logging.getLogger(__name__)
|
||||||
chatlogs = {}
|
chatlogs = {}
|
||||||
|
|
||||||
ADMIN_ID = 0
|
ADMIN_ID = 0
|
||||||
|
WAKEUP = False
|
||||||
CHAT_INC = 5
|
CHAT_INC = 5
|
||||||
CHAT_SAVE = 15
|
CHAT_SAVE = 15
|
||||||
LOG_DIR = "chatlogs/"
|
LOG_DIR = "chatlogs/"
|
||||||
|
@ -37,17 +38,17 @@ def wake(bot):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
"""
|
|
||||||
for c in chatlogs:
|
for c in chatlogs:
|
||||||
try:
|
try:
|
||||||
send_message(bot, update, "Good morning. I just woke up", False)
|
if WAKEUP:
|
||||||
|
send_message(bot, update, "Good morning. I just woke up")
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
#del chatlogs[c]
|
#del chatlogs[c]
|
||||||
"""
|
|
||||||
|
|
||||||
def start(bot, update):
|
def start(bot, update):
|
||||||
update.message.reply_text()
|
update.message.reply_text("Hello there! Ask me for /help to see an overview of the available commands.")
|
||||||
|
|
||||||
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')
|
||||||
|
@ -120,27 +121,37 @@ def read(bot, update):
|
||||||
elif update.message.sticker is not None:
|
elif update.message.sticker is not None:
|
||||||
chatlog.add_sticker(update.message.sticker.file_id)
|
chatlog.add_sticker(update.message.sticker.file_id)
|
||||||
|
|
||||||
|
if chatlog.get_count()%chatlog.freq == 1:
|
||||||
|
chatlog.restart_replyables(update.message.message_id)
|
||||||
|
else:
|
||||||
|
chatlog.add_replyable(update.message.message_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()):
|
reply_text = update.message.text.casefold()
|
||||||
|
to_reply = ((replied is not None) and (replied.from_user.name == "@velascobot")) or ("@velascobot" in reply_text) or ("velasco" in reply_text and "@velasco" not in reply_text)
|
||||||
|
|
||||||
|
if to_reply 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()
|
||||||
send_message(bot, update, msg, True)
|
send_message(bot, update, msg, update.message.message_id)
|
||||||
|
|
||||||
if random.random() <= REPT_CHANCE:
|
if random.random() <= REPT_CHANCE:
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
send_message(bot, update, msg, False)
|
send_message(bot, update, msg)
|
||||||
|
|
||||||
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")
|
||||||
send_message(bot, update, msg, True)
|
send_message(bot, update, msg, chatlog.get_replyable())
|
||||||
else:
|
else:
|
||||||
print("I sent a message")
|
print("I sent a message")
|
||||||
send_message(bot, update, msg, False)
|
send_message(bot, update, msg)
|
||||||
if random.random() <= REPT_CHANCE:
|
if random.random() <= REPT_CHANCE:
|
||||||
print("And a followup")
|
print("And a followup")
|
||||||
msg = chatlog.speak()
|
msg = chatlog.speak()
|
||||||
send_message(bot, update, msg, False)
|
send_message(bot, update, msg)
|
||||||
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 + "]")
|
||||||
|
@ -164,19 +175,19 @@ 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()
|
||||||
send_message(bot, update, msg, True)
|
send_message(bot, update, msg, update.message.message_id)
|
||||||
savechat(chatlog)
|
savechat(chatlog)
|
||||||
chatlogs[chatlog.id] = chatlog
|
chatlogs[chatlog.id] = chatlog
|
||||||
|
|
||||||
def send_message(bot, update, msg, is_reply):
|
def send_message(bot, update, msg, reply_id):
|
||||||
words = msg.split()
|
words = msg.split()
|
||||||
if words[0] == STICKER_TAG:
|
if words[0] == STICKER_TAG:
|
||||||
if is_reply:
|
if is_reply:
|
||||||
update.message.reply_sticker(words[1])
|
update.message.reply_sticker(words[1])
|
||||||
else:
|
else:
|
||||||
bot.sendSticker(update.message.chat_id, words[1])
|
bot.sendSticker(update.message.chat_id, words[1])
|
||||||
elif is_reply:
|
elif reply_id is not None:
|
||||||
update.message.reply_text(msg)
|
bot.sendMessage(update.message.chat.id, msg, reply_to_message_id=reply_id)
|
||||||
else:
|
else:
|
||||||
bot.sendMessage(update.message.chat.id, msg)
|
bot.sendMessage(update.message.chat.id, msg)
|
||||||
|
|
||||||
|
@ -184,7 +195,7 @@ 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
|
||||||
send_message(bot, update, msg, True)
|
send_message(bot, update, msg, update.message.message_id)
|
||||||
|
|
||||||
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))
|
||||||
|
@ -253,12 +264,15 @@ def main():
|
||||||
parser = argparse.ArgumentParser(description='A Telegram markov bot.')
|
parser = argparse.ArgumentParser(description='A Telegram markov bot.')
|
||||||
parser.add_argument('token', metavar='TOKEN', help='The Bot Token to work with the Telegram Bot API')
|
parser.add_argument('token', metavar='TOKEN', help='The Bot Token to work with the Telegram Bot API')
|
||||||
parser.add_argument('admin_id', metavar='ADMIN_ID', type=int, help='The ID of the Telegram user that manages this bot')
|
parser.add_argument('admin_id', metavar='ADMIN_ID', type=int, help='The ID of the Telegram user that manages this bot')
|
||||||
|
parser.add_argument('-w', '--wakeup', metavar='WAKEUP_MSG', action='store_true', help='Flag that makes the bot send a first message to all chats during wake up.')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Create the EventHandler and pass it your bot's token.
|
# Create the EventHandler and pass it your bot's token.
|
||||||
updater = Updater(args.token)
|
updater = Updater(args.token)
|
||||||
ADMIN_ID = args.admin_id
|
ADMIN_ID = args.admin_id
|
||||||
|
if args.wakeup:
|
||||||
|
WAKEUP = True
|
||||||
|
|
||||||
# Get the dispatcher to register handlers
|
# Get the dispatcher to register handlers
|
||||||
dp = updater.dispatcher
|
dp = updater.dispatcher
|
||||||
|
@ -266,6 +280,7 @@ def main():
|
||||||
# on different commands - answer in Telegram
|
# on different commands - answer in Telegram
|
||||||
dp.add_handler(CommandHandler("start", start))
|
dp.add_handler(CommandHandler("start", start))
|
||||||
dp.add_handler(CommandHandler("about", about))
|
dp.add_handler(CommandHandler("about", about))
|
||||||
|
dp.add_handler(CommandHandler("explain", explain))
|
||||||
dp.add_handler(CommandHandler("help", help))
|
dp.add_handler(CommandHandler("help", help))
|
||||||
dp.add_handler(CommandHandler("count", get_count))
|
dp.add_handler(CommandHandler("count", get_count))
|
||||||
dp.add_handler(CommandHandler("freq", set_freq))
|
dp.add_handler(CommandHandler("freq", set_freq))
|
||||||
|
|
Loading…
Reference in a new issue