big changes
This commit is contained in:
@@ -4,8 +4,8 @@ FIRST = range(1)
|
||||
class Alias(BotFunc):
|
||||
"""create a new command for command-paths you often use"""
|
||||
|
||||
def __init__(self, dispatcher, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, dispatcher, db):
|
||||
super().__init__(db)
|
||||
self.dispatcher = dispatcher
|
||||
# do not interact with him yet!
|
||||
|
||||
@@ -25,7 +25,6 @@ class Alias(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
test = self.dispatcher
|
||||
print(self.dispatcher.handlers[0])
|
||||
keyboard = [
|
||||
@@ -34,6 +33,7 @@ class Alias(BotFunc):
|
||||
[InlineKeyboardButton("Delete alias", callback_data="delete")],
|
||||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
super().log_activity(receive=True, execute=False, send=True)
|
||||
update.message.reply_text("What exactly do you want?", reply_markup=reply_markup)
|
||||
return FIRST
|
||||
|
||||
|
@@ -37,7 +37,6 @@ class Clock(BotFunc):
|
||||
return handler
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
keyboard = [
|
||||
[InlineKeyboardButton("Make a wake-light", callback_data="wake")],
|
||||
[InlineKeyboardButton("Blink as alarm", callback_data="alarm")],
|
||||
@@ -208,4 +207,12 @@ class Clock(BotFunc):
|
||||
|
||||
update.message.reply_text("Ok. Showing art for the next "+ str(duration) + " hours.")
|
||||
self.clock.run(output,(number, duration))
|
||||
return ConversationHandler.END
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# TODO FIx this to work with the new backend
|
@@ -6,8 +6,8 @@ FIRST, EXECUTE = range(2)
|
||||
class Help(BotFunc):
|
||||
"""Shows the functions and their usage"""
|
||||
|
||||
def __init__(self, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, db):
|
||||
super().__init__(db)
|
||||
self.available_commands = {}
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ class Help(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton("All commands", callback_data="all"),
|
||||
@@ -47,6 +46,7 @@ class Help(BotFunc):
|
||||
]
|
||||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
super().log_activity(read=True, execute=True, send=True) # at this point every step has been fulfilled
|
||||
if update.message:
|
||||
update.message.reply_text("What exactly do you want?", reply_markup=reply_markup)
|
||||
else:
|
||||
|
@@ -9,8 +9,8 @@ NAME, NEW, ACTION, ITEMADD, ITEMREMOVE = range(5)
|
||||
class Lists(BotFunc):
|
||||
"""Create and edit lists"""
|
||||
|
||||
def __init__(self, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, db):
|
||||
super().__init__(db)
|
||||
self.current_name = ""
|
||||
|
||||
|
||||
@@ -40,10 +40,12 @@ class Lists(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
keyboard = [[InlineKeyboardButton(k, callback_data="list-"+k)] for k in self.persistence["global"]["lists"]] + [[InlineKeyboardButton("New list", callback_data="new")]]
|
||||
lists = self.db.lists.select()
|
||||
sl = [l.name for l in lists]
|
||||
keyboard = [[InlineKeyboardButton(k, callback_data="list-"+k)] for k in sl] + [[InlineKeyboardButton("New list", callback_data="new")]]
|
||||
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
super().log_activity(read=True, execute=False, send=True)
|
||||
update.message.reply_text(text="Here are the existing lists. You can also create a new one:", reply_markup=reply_markup)
|
||||
return NAME
|
||||
|
||||
@@ -94,16 +96,16 @@ class Lists(BotFunc):
|
||||
|
||||
def new_listname(self, update: Update, context: CallbackContext) -> None:
|
||||
name = update.message.text
|
||||
if name not in self.persistence["global"]["lists"]:
|
||||
self.persistence["global"]["lists"][name] = []
|
||||
|
||||
try:
|
||||
data = self.db.lists(name=name, content="")
|
||||
data.save()
|
||||
keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
self.current_name = name
|
||||
update.message.reply_text("Thanks. List " + name + " was successfully created.", reply_markup=reply_markup)
|
||||
return ACTION
|
||||
else:
|
||||
update.message.reply_text("Oh no! That list already exists")
|
||||
except Exception as e:
|
||||
update.message.reply_text("Oh no! Encountered exception: {}".format(e))
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
@@ -117,7 +119,10 @@ class Lists(BotFunc):
|
||||
def list_remove(self, update: Update, context: CallbackContext) -> None:
|
||||
query = update.callback_query
|
||||
query.answer()
|
||||
keyboard = [[InlineKeyboardButton(k, callback_data=i)] for i,k in enumerate(self.persistence["global"]["lists"][self.current_name])]
|
||||
it = self.db.lists.get(self.db.lists.name == self.current_name)
|
||||
sl = it.content.split("<-->")
|
||||
|
||||
keyboard = [[InlineKeyboardButton(k, callback_data=i)] for i,k in enumerate(sl)]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
query.edit_message_text("Which item would you like to remove?", reply_markup = reply_markup)
|
||||
@@ -127,7 +132,7 @@ class Lists(BotFunc):
|
||||
def list_clear(self, update: Update, context: CallbackContext) -> None:
|
||||
query = update.callback_query
|
||||
query.answer()
|
||||
self.persistence["global"]["lists"][self.current_name] = []
|
||||
self.db.lists.update(content="").where(self.db.lists.name == self.current_name).execute()
|
||||
keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
query.edit_message_text("List " + self.current_name + " cleared", reply_markup=reply_markup)
|
||||
@@ -137,7 +142,7 @@ class Lists(BotFunc):
|
||||
def list_delete(self, update: Update, context: CallbackContext) -> None:
|
||||
query = update.callback_query
|
||||
query.answer()
|
||||
self.persistence["global"]["lists"].pop(self.current_name, None)
|
||||
self.db.lists.delete().where(self.db.lists.name == self.current_name).execute()
|
||||
query.edit_message_text("List " + self.current_name + " deleted")
|
||||
return ConversationHandler.END
|
||||
|
||||
@@ -145,7 +150,9 @@ class Lists(BotFunc):
|
||||
def list_print(self, update: Update, context: CallbackContext) -> None:
|
||||
query = update.callback_query
|
||||
query.answer()
|
||||
content = "\n".join(self.persistence["global"]["lists"][self.current_name])
|
||||
it = self.db.lists.get(self.db.lists.name == self.current_name)
|
||||
content = it.content.split("<-->")
|
||||
content = "\n".join(content)
|
||||
keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
query.edit_message_text("Content of " + self.current_name + ":\n" + content, reply_markup=reply_markup)
|
||||
@@ -153,11 +160,16 @@ class Lists(BotFunc):
|
||||
|
||||
|
||||
def list_add_item(self, update: Update, context: CallbackContext) -> None:
|
||||
name = update.message.text
|
||||
self.persistence["global"]["lists"][self.current_name] += [name]
|
||||
item = update.message.text
|
||||
it = self.db.lists.get(self.db.lists.name == self.current_name)
|
||||
sl = it.content
|
||||
print("SLSLLLL: {} -- {}".format(sl, type(sl)))
|
||||
sl += item + "<-->"
|
||||
self.db.lists.update(content=sl).where(self.db.lists.name == self.current_name).execute()
|
||||
|
||||
keyboard = [[InlineKeyboardButton("Add some more", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
update.message.reply_text("Added " + name, reply_markup=reply_markup)
|
||||
update.message.reply_text("Added " + item, reply_markup=reply_markup)
|
||||
return ACTION
|
||||
|
||||
|
||||
@@ -166,9 +178,13 @@ class Lists(BotFunc):
|
||||
ind = int(query.data)
|
||||
query.answer()
|
||||
|
||||
old = self.persistence["global"]["lists"][self.current_name]
|
||||
it = self.db.lists.get(self.db.lists.name == self.current_name)
|
||||
old = it.content.split("<-->")
|
||||
# todo make better
|
||||
|
||||
name = old.pop(ind)
|
||||
self.persistence["global"]["lists"][self.current_name] = old
|
||||
new = "<-->".join(old)
|
||||
self.db.lists.update(content=new).where(self.db.lists.name == self.current_name).execute()
|
||||
|
||||
keyboard = [[InlineKeyboardButton("Remove another", callback_data="remove"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
@@ -3,13 +3,16 @@ from .template import *
|
||||
|
||||
class Plain(BotFunc):
|
||||
"""Not a command: just keeps logs and usage_data"""
|
||||
def __init__(self, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, db):
|
||||
super().__init__(db)
|
||||
|
||||
def create_handler(self):
|
||||
h = MessageHandler(Filters.text, callback=self.add_to_log)
|
||||
return h
|
||||
|
||||
def add_to_log(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
super().increase_counter("receive_activity")
|
||||
super().log_activity(
|
||||
read = True,
|
||||
send = False,
|
||||
execute = False
|
||||
)
|
||||
|
@@ -24,9 +24,10 @@ class Joke(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
|
||||
keyboard = [[InlineKeyboardButton(str(i), callback_data=str(i)) for i in range(1,11)]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
super().log_activity(read=True, execute=True, send=True) # at this point every step has been fulfilled
|
||||
update.message.reply_text("How many jokes?", reply_markup=reply_markup)
|
||||
return CHOOSE_NUM
|
||||
|
||||
@@ -71,7 +72,6 @@ class Meme(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
|
||||
keyboard = [
|
||||
[InlineKeyboardButton("General", callback_data="memes"),],
|
||||
@@ -81,6 +81,7 @@ class Meme(BotFunc):
|
||||
[InlineKeyboardButton("Biology", callback_data="biologymemes"),],
|
||||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
super().log_activity(read=True, execute=True, send=True) # at this point every step has been fulfilled
|
||||
update.message.reply_text("What kind of memes?", reply_markup=reply_markup)
|
||||
return CHOOSE_TOPIC
|
||||
|
||||
@@ -105,6 +106,7 @@ class Meme(BotFunc):
|
||||
memes = self.api.get_random_rising(data[0], int(data[1]), "photo")
|
||||
if len(memes) != 0:
|
||||
for m in memes:
|
||||
super().log_activity(read=False, execute=False, send=True) # we just sent an additional message
|
||||
update.effective_chat.send_photo(photo = m["image"],caption = m["caption"])
|
||||
else:
|
||||
update.effective_chat.send_message("Sorry, the meme won't yeet.")
|
||||
|
@@ -5,8 +5,8 @@ SEARCH, MORE = range(2)
|
||||
class Search(BotFunc):
|
||||
"""Browse the web for a topic."""
|
||||
|
||||
def __init__(self, api, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, api, db):
|
||||
super().__init__(db)
|
||||
self.available_commands = {}
|
||||
self.api = api
|
||||
|
||||
@@ -25,7 +25,6 @@ class Search(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
|
||||
update.message.reply_text("What are we searching?")
|
||||
return SEARCH
|
||||
@@ -43,6 +42,7 @@ class Search(BotFunc):
|
||||
message = first["text"] + "\n(" + first["url"] + ")\n\n"
|
||||
|
||||
update.message.reply_text(text = message, reply_markup=reply_markup)
|
||||
super().log_activity(read = True, execute = True, send = True)
|
||||
return MORE
|
||||
|
||||
|
||||
@@ -55,4 +55,5 @@ class Search(BotFunc):
|
||||
message += r["text"] + "\n(" + r["url"] + ")\n\n"
|
||||
|
||||
query.edit_message_text(message)
|
||||
super().log_activity(read = False, execute = False, send = True)
|
||||
return ConversationHandler.END
|
@@ -13,8 +13,8 @@ FIRST = 1
|
||||
class Status(BotFunc):
|
||||
"""Shows a short status of the program."""
|
||||
|
||||
def __init__(self, name, version, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, name, version, db):
|
||||
super().__init__(db)
|
||||
self.start_time = datetime.datetime.now()
|
||||
self.name = name
|
||||
self.version = version
|
||||
@@ -33,7 +33,6 @@ class Status(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton("And the log?", callback_data="full"),
|
||||
@@ -75,6 +74,8 @@ class Status(BotFunc):
|
||||
update.message.reply_text(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
|
||||
else:
|
||||
update._effective_chat.send_message(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
|
||||
|
||||
super().log_activity(read = True, execute = True, send = True)
|
||||
return FIRST
|
||||
|
||||
|
||||
@@ -85,6 +86,7 @@ class Status(BotFunc):
|
||||
with open("persistence/complete.log") as l:
|
||||
query.message.reply_document(l)
|
||||
|
||||
super().log_activity(read = False, execute = False, send = True)
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
import datetime
|
||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ParseMode
|
||||
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters
|
||||
from telegram.ext import (
|
||||
@@ -15,19 +16,21 @@ import datetime
|
||||
|
||||
class BotFunc():
|
||||
"""Base class for a specific bot-functionality"""
|
||||
def __init__(self, prst):
|
||||
def __init__(self, db):
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self.persistence = prst
|
||||
self.db = db
|
||||
|
||||
|
||||
def entry_point(self):
|
||||
self.increase_counter("execute_activity")
|
||||
def log_activity(self, **kwargs):
|
||||
# mark that a new command has been executed
|
||||
data = self.db.chats(
|
||||
time=datetime.datetime.now(),
|
||||
**kwargs
|
||||
)
|
||||
# kwargs can look like
|
||||
# receive=True,
|
||||
# execute=True,
|
||||
# send=False,
|
||||
data.save()
|
||||
|
||||
|
||||
def increase_counter(self, counter_name):
|
||||
current_hour = int(datetime.datetime.now().timestamp() // 3600)
|
||||
if len(self.persistence["bot"][counter_name]["hour"]) == 0 or current_hour != self.persistence["bot"][counter_name]["hour"][-1]:
|
||||
self.persistence["bot"][counter_name]["hour"].append(current_hour)
|
||||
self.persistence["bot"][counter_name]["count"].append(1)
|
||||
else:
|
||||
self.persistence["bot"][counter_name]["count"][-1] += 1
|
@@ -6,9 +6,9 @@ FIRST = 1
|
||||
|
||||
class Weather(BotFunc):
|
||||
"""Shows a weatherforecast for a given location"""
|
||||
def __init__(self, api, prst):
|
||||
def __init__(self, api, db):
|
||||
"""initialize api and persistence"""
|
||||
super().__init__(prst)
|
||||
super().__init__(db)
|
||||
self.api = api
|
||||
self.city = ""
|
||||
|
||||
@@ -31,7 +31,6 @@ class Weather(BotFunc):
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
"""Reacts the call of the command. Prints the first buttons"""
|
||||
super().entry_point()
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton("Zürich", callback_data="city-zurich"),
|
||||
@@ -78,6 +77,7 @@ class Weather(BotFunc):
|
||||
text = "Weather: \n\n" + weather,
|
||||
parse_mode = ParseMode.HTML
|
||||
)
|
||||
super().log_activity(read = True, execute = True, send = True)
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
|
@@ -8,8 +8,8 @@ START, DEST = range(2)
|
||||
class Zvv(BotFunc):
|
||||
"""Connects to the swiss travel-api to get public transport routes"""
|
||||
|
||||
def __init__(self, prst):
|
||||
super().__init__(prst)
|
||||
def __init__(self, db):
|
||||
super().__init__(db)
|
||||
self.start = ""
|
||||
self.dest = ""
|
||||
pass
|
||||
@@ -27,7 +27,6 @@ class Zvv(BotFunc):
|
||||
|
||||
|
||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||
super().entry_point()
|
||||
update.message.reply_text("What is the start point?")
|
||||
return START
|
||||
|
||||
@@ -43,7 +42,8 @@ class Zvv(BotFunc):
|
||||
loc = update.message.text
|
||||
self.dest = loc
|
||||
route = self.get_result()
|
||||
update.message.reply_text("Her are the routes I've got:\n" + route)
|
||||
update.message.reply_text("Here are the routes I've got:\n" + route)
|
||||
super().log_activity(read=True, execute=True, send=True)
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
|
28
bot/main.py
28
bot/main.py
@@ -16,7 +16,7 @@ class ChatBot():
|
||||
-> prst:dict - persistence (overloaded dict that writes to json file)
|
||||
-> logger - logging object to unify log messages
|
||||
"""
|
||||
# added by the launcher, we have self.modules (dict) and persistence
|
||||
# added by the launcher, we have self.modules (dict) and persistence and db
|
||||
|
||||
self.name = name
|
||||
self.version = version
|
||||
@@ -36,22 +36,22 @@ class ChatBot():
|
||||
|
||||
def add_commands(self):
|
||||
# Mark modules as available
|
||||
prst = self.persistence
|
||||
self.help_module = self.commands.help.Help(prst)
|
||||
db = self.db
|
||||
self.help_module = self.commands.help.Help(db)
|
||||
self.sub_modules = {
|
||||
"weather": self.commands.weather.Weather(self.api_weather, prst),
|
||||
"weather": self.commands.weather.Weather(self.api_weather, db),
|
||||
"help" : self.help_module,
|
||||
"status" : self.commands.status.Status(self.name, self.version, prst),
|
||||
"zvv" : self.commands.zvv.Zvv(prst),
|
||||
"list" : self.commands.lists.Lists(prst),
|
||||
# "alias" : commands.alias.Alias(self.dispatcher, prst),
|
||||
"joke" : self.commands.reddit.Joke(self.api_reddit, prst),
|
||||
"meme" : self.commands.reddit.Meme(self.api_reddit, prst),
|
||||
# "news" : self.commands.reddit.News(self.api_reddit, prst),
|
||||
"search" : self.commands.search.Search(self.api_search, prst),
|
||||
"status" : self.commands.status.Status(self.name, self.version, db),
|
||||
"zvv" : self.commands.zvv.Zvv(db),
|
||||
"list" : self.commands.lists.Lists(db),
|
||||
# "alias" : commands.alias.Alias(self.dispatcher, db),
|
||||
"joke" : self.commands.reddit.Joke(self.api_reddit, db),
|
||||
"meme" : self.commands.reddit.Meme(self.api_reddit, db),
|
||||
# "news" : self.commands.reddit.News(self.api_reddit, db),
|
||||
"search" : self.commands.search.Search(self.api_search, db),
|
||||
# ...
|
||||
|
||||
"plaintext" : self.commands.plaintext.Plain(prst) # for handling non-command messages that should simply contribute to statistics
|
||||
"plaintext" : self.commands.plaintext.Plain(db) # for handling non-command messages that should simply contribute to statistics
|
||||
}
|
||||
# must be a class that has a method create_handler
|
||||
|
||||
@@ -61,7 +61,7 @@ class ChatBot():
|
||||
self.help_module.add_commands(self.sub_modules)
|
||||
|
||||
def start(self):
|
||||
self.sub_modules = {"clock" : self.commands.clock.Clock(self.persistence, self.modules["clock"], self.api_art)}
|
||||
self.sub_modules = {"clock" : self.commands.clock.Clock(self.db, self.modules["clock"], self.api_art)}
|
||||
self.add_commands()
|
||||
self.telegram.start_polling()
|
||||
# self.telegram.idle()
|
||||
|
Reference in New Issue
Block a user