basic reworked bot
This commit is contained in:
parent
ec8983eb57
commit
39ae1ec038
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python: Aktuelle Datei",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -1,5 +1,2 @@
|
|||||||
# Placeholder
|
# Placeholder
|
||||||
from . import clock
|
from . import clock, help, weather, status, zvv, lists, alias, plaintext
|
||||||
from . import help
|
|
||||||
from . import weather
|
|
||||||
from . import status
|
|
||||||
|
65
bot2/commands/alias.py
Normal file
65
bot2/commands/alias.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
from .template import *
|
||||||
|
|
||||||
|
FIRST = range(1)
|
||||||
|
class Alias(BotFunc):
|
||||||
|
"""create a new command for command-paths you often use"""
|
||||||
|
|
||||||
|
def __init__(self, dispatcher, prst):
|
||||||
|
super().__init__(prst)
|
||||||
|
self.dispatcher = dispatcher
|
||||||
|
# do not interact with him yet!
|
||||||
|
|
||||||
|
def create_handler(self):
|
||||||
|
conv_handler = ConversationHandler(
|
||||||
|
entry_points=[CommandHandler('alias', self.entry_point)],
|
||||||
|
states={
|
||||||
|
FIRST: [
|
||||||
|
CallbackQueryHandler(self.print_all, pattern="^all$"),
|
||||||
|
CallbackQueryHandler(self.create_alias, pattern="^new$"),
|
||||||
|
CallbackQueryHandler(self.delete_alias, pattern='^delete$'),
|
||||||
|
]
|
||||||
|
},
|
||||||
|
fallbacks=[CommandHandler('alias', self.entry_point)],
|
||||||
|
)
|
||||||
|
return conv_handler
|
||||||
|
|
||||||
|
|
||||||
|
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
super().entry_point()
|
||||||
|
test = self.dispatcher
|
||||||
|
print(self.dispatcher.handlers[0])
|
||||||
|
keyboard = [
|
||||||
|
[InlineKeyboardButton("All aliases", callback_data="all")],
|
||||||
|
[InlineKeyboardButton("Create new alias", callback_data="new")],
|
||||||
|
[InlineKeyboardButton("Delete alias", callback_data="delete")],
|
||||||
|
]
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
update.message.reply_text("What exactly do you want?", reply_markup=reply_markup)
|
||||||
|
return FIRST
|
||||||
|
|
||||||
|
|
||||||
|
def print_all(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
|
||||||
|
all_alias = ""
|
||||||
|
for k in self.persistence["bot"]["aliases"]:
|
||||||
|
all_alias += k + " - " + self.persistence["bot"]["aliases"] +"\n"
|
||||||
|
|
||||||
|
query.edit_message_text(text="List of all commands:\n" + all_alias)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
|
||||||
|
def create_alias(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
|
||||||
|
all_alias = ""
|
||||||
|
for k in self.persistence["bot"]["aliases"]:
|
||||||
|
all_alias += k + " - " + self.persistence["bot"]["aliases"] +"\n"
|
||||||
|
|
||||||
|
query.edit_message_text(text="List of all commands:\n" + all_alias)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
def delete_alias(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
return ConversationHandler.END
|
@ -0,0 +1,23 @@
|
|||||||
|
from .template import *
|
||||||
|
|
||||||
|
CHOOSE, ARGS = range(2)
|
||||||
|
|
||||||
|
class Clock(BotFunc):
|
||||||
|
"""pass on commands to clock-module"""
|
||||||
|
def __init__(self, prst, hw_commands):
|
||||||
|
super().__init__(prst)
|
||||||
|
self.hw_commands = hw_commands
|
||||||
|
|
||||||
|
def create_handler(self):
|
||||||
|
handler = ConversationHandler(
|
||||||
|
entry_points=[CommandHandler("clock", self.entry_point)],
|
||||||
|
states={
|
||||||
|
CHOOSE : [],
|
||||||
|
ARGS : []
|
||||||
|
},
|
||||||
|
fallbacks=[CommandHandler('clock', self.entry_point)],
|
||||||
|
)
|
||||||
|
return handler
|
||||||
|
|
||||||
|
def entry_point(self):
|
||||||
|
super().entry_point()
|
@ -1,19 +1,13 @@
|
|||||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
from .template import *
|
||||||
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
|
|
||||||
from telegram.ext import (
|
|
||||||
Updater,
|
|
||||||
CommandHandler,
|
|
||||||
CallbackQueryHandler,
|
|
||||||
ConversationHandler,
|
|
||||||
CallbackContext,
|
|
||||||
)
|
|
||||||
|
|
||||||
FIRST = 1
|
FIRST = 1
|
||||||
|
|
||||||
class Help():
|
|
||||||
|
class Help(BotFunc):
|
||||||
"""Shows the functions and their usage"""
|
"""Shows the functions and their usage"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, prst):
|
||||||
|
super().__init__(prst)
|
||||||
self.available_commands = {}
|
self.available_commands = {}
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +19,7 @@ class Help():
|
|||||||
CallbackQueryHandler(self.print_all, pattern="^all$"),
|
CallbackQueryHandler(self.print_all, pattern="^all$"),
|
||||||
CallbackQueryHandler(self.choose_specific, pattern="^specific$"),
|
CallbackQueryHandler(self.choose_specific, pattern="^specific$"),
|
||||||
CallbackQueryHandler(self.print_one, pattern='func-'),
|
CallbackQueryHandler(self.print_one, pattern='func-'),
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
fallbacks=[CommandHandler('help', self.entry_point)],
|
fallbacks=[CommandHandler('help', self.entry_point)],
|
||||||
)
|
)
|
||||||
@ -34,9 +28,13 @@ class Help():
|
|||||||
def add_commands(self, commands):
|
def add_commands(self, commands):
|
||||||
# commands is a dict {"name": class}
|
# commands is a dict {"name": class}
|
||||||
for k in commands:
|
for k in commands:
|
||||||
self.available_commands[k] = commands[k].__doc__
|
if k != "plaintext":
|
||||||
|
self.available_commands[k] = commands[k].__doc__
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
super().entry_point()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[
|
[
|
||||||
InlineKeyboardButton("All commands", callback_data="all"),
|
InlineKeyboardButton("All commands", callback_data="all"),
|
||||||
@ -51,12 +49,11 @@ class Help():
|
|||||||
def print_all(self, update: Update, context: CallbackContext) -> None:
|
def print_all(self, update: Update, context: CallbackContext) -> None:
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
query.answer()
|
query.answer()
|
||||||
|
|
||||||
all_cmd = ""
|
all_cmd = ""
|
||||||
for k in self.available_commands:
|
for h in self.available_commands:
|
||||||
all_cmd += k + " - " + self.available_commands[k] +"\n"
|
all_cmd += h + " - `" + self.available_commands[h] + "`\n"
|
||||||
|
|
||||||
query.edit_message_text(text="List of all commands:\n" + all_cmd)
|
query.edit_message_text(text="List of all commands:\n" + all_cmd, parse_mode = ParseMode.MARKDOWN)
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
|
||||||
@ -66,8 +63,8 @@ class Help():
|
|||||||
|
|
||||||
|
|
||||||
keyboard = [[InlineKeyboardButton(k, callback_data="func-" + k)] for k in self.available_commands]
|
keyboard = [[InlineKeyboardButton(k, callback_data="func-" + k)] for k in self.available_commands]
|
||||||
|
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
|
||||||
query.edit_message_text(
|
query.edit_message_text(
|
||||||
text="What command should be printed?", reply_markup=reply_markup
|
text="What command should be printed?", reply_markup=reply_markup
|
||||||
)
|
)
|
||||||
@ -77,11 +74,12 @@ class Help():
|
|||||||
def print_one(self, update: Update, context: CallbackContext) -> None:
|
def print_one(self, update: Update, context: CallbackContext) -> None:
|
||||||
"""Show new choice of buttons"""
|
"""Show new choice of buttons"""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
data = query.data.replace("func-", "")
|
name = query.data.replace("func-", "")
|
||||||
|
|
||||||
query.answer()
|
query.answer()
|
||||||
message = self.available_commands[data]
|
|
||||||
|
message = name + ": `" + self.available_commands[name] + "`"
|
||||||
query.edit_message_text(
|
query.edit_message_text(
|
||||||
text= message
|
text= message,
|
||||||
|
parse_mode = ParseMode.MARKDOWN_V2
|
||||||
)
|
)
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
|
177
bot2/commands/lists.py
Normal file
177
bot2/commands/lists.py
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
from .template import *
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import requests
|
||||||
|
|
||||||
|
NAME, NEW, ACTION, ITEMADD, ITEMREMOVE = range(5)
|
||||||
|
|
||||||
|
|
||||||
|
class Lists(BotFunc):
|
||||||
|
"""Create and edit lists"""
|
||||||
|
|
||||||
|
def __init__(self, prst):
|
||||||
|
super().__init__(prst)
|
||||||
|
self.current_name = ""
|
||||||
|
|
||||||
|
|
||||||
|
def create_handler(self):
|
||||||
|
conv_handler = ConversationHandler(
|
||||||
|
entry_points=[CommandHandler('list', self.entry_point)],
|
||||||
|
states={
|
||||||
|
NAME: [
|
||||||
|
CallbackQueryHandler(self.choose_list, pattern="^list-"),
|
||||||
|
CallbackQueryHandler(self.new_list, pattern="^new$"),
|
||||||
|
],
|
||||||
|
NEW : [MessageHandler(Filters.text, callback=self.new_listname)],
|
||||||
|
ACTION: [
|
||||||
|
CallbackQueryHandler(self.list_add, pattern="^add$"),
|
||||||
|
CallbackQueryHandler(self.list_remove, pattern="^remove$"),
|
||||||
|
CallbackQueryHandler(self.list_clear, pattern="^clear$"),
|
||||||
|
CallbackQueryHandler(self.list_delete, pattern="^delete$"),
|
||||||
|
CallbackQueryHandler(self.list_print, pattern="^print$"),
|
||||||
|
CallbackQueryHandler(self.list_menu, pattern="^overview$"),
|
||||||
|
],
|
||||||
|
ITEMADD : [MessageHandler(Filters.text, callback=self.list_add_item)],
|
||||||
|
ITEMREMOVE : [CallbackQueryHandler(self.list_remove_index)]
|
||||||
|
},
|
||||||
|
fallbacks=[CommandHandler('list', self.entry_point)],
|
||||||
|
)
|
||||||
|
return conv_handler
|
||||||
|
|
||||||
|
|
||||||
|
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")]]
|
||||||
|
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
update.message.reply_text(text="Here are the existing lists. You can also create a new one:", reply_markup=reply_markup)
|
||||||
|
return NAME
|
||||||
|
|
||||||
|
|
||||||
|
def choose_list(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
data = query.data
|
||||||
|
name = data.replace("list-","")
|
||||||
|
query.answer()
|
||||||
|
self.current_name = name
|
||||||
|
|
||||||
|
keyboard = [
|
||||||
|
[InlineKeyboardButton("Add item", callback_data="add")],
|
||||||
|
[InlineKeyboardButton("Remove item", callback_data="remove")],
|
||||||
|
[InlineKeyboardButton("Clear list", callback_data="clear")],
|
||||||
|
[InlineKeyboardButton("Print list", callback_data="print")],
|
||||||
|
[InlineKeyboardButton("Delete list", callback_data="delete")],
|
||||||
|
]
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
|
||||||
|
query.edit_message_text("Very well. For " + name + " the following actions are available:", reply_markup=reply_markup)
|
||||||
|
return ACTION
|
||||||
|
|
||||||
|
|
||||||
|
def list_menu(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
|
||||||
|
keyboard = [
|
||||||
|
[InlineKeyboardButton("Add item", callback_data="add")],
|
||||||
|
[InlineKeyboardButton("Remove item", callback_data="remove")],
|
||||||
|
[InlineKeyboardButton("Clear list", callback_data="clear")],
|
||||||
|
[InlineKeyboardButton("Print list", callback_data="print")],
|
||||||
|
[InlineKeyboardButton("Delete list", callback_data="delete")],
|
||||||
|
]
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
|
||||||
|
query.edit_message_text("Very well. For " + self.current_name + " the following actions are available:", reply_markup=reply_markup)
|
||||||
|
return ACTION
|
||||||
|
|
||||||
|
|
||||||
|
def new_list(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
query.edit_message_text("What's the name of the new list?")
|
||||||
|
return NEW
|
||||||
|
|
||||||
|
|
||||||
|
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] = []
|
||||||
|
|
||||||
|
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")
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
|
||||||
|
def list_add(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
query.edit_message_text("What would you like to add?")
|
||||||
|
return ITEMADD
|
||||||
|
|
||||||
|
|
||||||
|
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])]
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
|
||||||
|
query.edit_message_text("Which item would you like to remove?", reply_markup = reply_markup)
|
||||||
|
return ITEMREMOVE
|
||||||
|
|
||||||
|
|
||||||
|
def list_clear(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
self.persistence["global"]["lists"][self.current_name] = []
|
||||||
|
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)
|
||||||
|
return ACTION
|
||||||
|
|
||||||
|
|
||||||
|
def list_delete(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
query.answer()
|
||||||
|
self.persistence["global"]["lists"].pop(self.current_name, None)
|
||||||
|
query.edit_message_text("List " + self.current_name + " deleted")
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
|
||||||
|
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])
|
||||||
|
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)
|
||||||
|
return ACTION
|
||||||
|
|
||||||
|
|
||||||
|
def list_add_item(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
name = update.message.text
|
||||||
|
self.persistence["global"]["lists"][self.current_name] += [name]
|
||||||
|
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)
|
||||||
|
return ACTION
|
||||||
|
|
||||||
|
|
||||||
|
def list_remove_index(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
query = update.callback_query
|
||||||
|
ind = int(query.data)
|
||||||
|
query.answer()
|
||||||
|
|
||||||
|
old = self.persistence["global"]["lists"][self.current_name]
|
||||||
|
name = old.pop(ind)
|
||||||
|
self.persistence["global"]["lists"][self.current_name] = old
|
||||||
|
|
||||||
|
keyboard = [[InlineKeyboardButton("Remove another", callback_data="remove"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
|
||||||
|
query.edit_message_text("Removed " + name, reply_markup=reply_markup)
|
||||||
|
return ACTION
|
15
bot2/commands/plaintext.py
Normal file
15
bot2/commands/plaintext.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from .template import *
|
||||||
|
|
||||||
|
|
||||||
|
class Plain(BotFunc):
|
||||||
|
"""Not a command: just keeps logs and usage_data"""
|
||||||
|
def __init__(self, prst):
|
||||||
|
super().__init__(prst)
|
||||||
|
|
||||||
|
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")
|
@ -1,35 +1,29 @@
|
|||||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
from .template import *
|
||||||
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
|
|
||||||
from telegram.ext import (
|
|
||||||
Updater,
|
|
||||||
CommandHandler,
|
|
||||||
CallbackQueryHandler,
|
|
||||||
ConversationHandler,
|
|
||||||
CallbackContext,
|
|
||||||
)
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import requests
|
import requests
|
||||||
import socket
|
import socket
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
FIRST = 1
|
FIRST = 1
|
||||||
|
|
||||||
class Status():
|
class Status(BotFunc):
|
||||||
"""Shows a short status of the program."""
|
"""Shows a short status of the program."""
|
||||||
|
|
||||||
def __init__(self, name, version, prst, logger):
|
def __init__(self, name, version, prst):
|
||||||
|
super().__init__(prst)
|
||||||
self.start_time = datetime.datetime.now()
|
self.start_time = datetime.datetime.now()
|
||||||
self.name = name
|
self.name = name
|
||||||
self.version = version
|
self.version = version
|
||||||
self.persistence = prst
|
|
||||||
self.logger = logger
|
|
||||||
|
|
||||||
def create_handler(self):
|
def create_handler(self):
|
||||||
conv_handler = ConversationHandler(
|
conv_handler = ConversationHandler(
|
||||||
entry_points=[CommandHandler('status', self.entry_point)],
|
entry_points=[CommandHandler('status', self.entry_point)],
|
||||||
states={
|
states={
|
||||||
FIRST: [
|
FIRST: [
|
||||||
CallbackQueryHandler(self.print_status, pattern="^status-"),
|
CallbackQueryHandler(self.send_log, pattern="^full$"),
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
fallbacks=[CommandHandler('status', self.entry_point)],
|
fallbacks=[CommandHandler('status', self.entry_point)],
|
||||||
@ -38,22 +32,14 @@ class Status():
|
|||||||
|
|
||||||
|
|
||||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
super().entry_point()
|
||||||
user = update.message.from_user
|
user = update.message.from_user
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[
|
[
|
||||||
InlineKeyboardButton("Status", callback_data="status-simple"),
|
InlineKeyboardButton("And the log?", callback_data="full"),
|
||||||
InlineKeyboardButton("With log", callback_data="status-full"),
|
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
update.message.reply_text("What exactly do you want?", reply_markup=reply_markup)
|
|
||||||
return FIRST
|
|
||||||
|
|
||||||
|
|
||||||
def print_status(self, update: Update, context: CallbackContext) -> None:
|
|
||||||
query = update.callback_query
|
|
||||||
wanted = query.data.replace("status-","")
|
|
||||||
query.answer()
|
|
||||||
|
|
||||||
delta = str(datetime.datetime.now() - self.start_time)
|
delta = str(datetime.datetime.now() - self.start_time)
|
||||||
message = "BeebBop, this is " + self.name + " (V." + self.version + ")\n"
|
message = "BeebBop, this is " + self.name + " (V." + self.version + ")\n"
|
||||||
@ -68,25 +54,30 @@ class Status():
|
|||||||
ip = "not fetchable"
|
ip = "not fetchable"
|
||||||
local_ips = "not fetchable"
|
local_ips = "not fetchable"
|
||||||
|
|
||||||
message += "<pre>Status: Running 🟢\n"
|
message += "Status: Running 🟢\n"
|
||||||
message += "Uptime: " + delta[:delta.rfind(".")] + "\n"
|
message += "Uptime: `" + delta[:delta.rfind(".")] + "`\n"
|
||||||
message += "Reboots: " + str(self.persistence["global"]["reboots"]) + "\n"
|
message += "Reboots: `" + str(self.persistence["global"]["reboots"]) + "`\n"
|
||||||
message += "IP (public): " + ip + "\n"
|
message += "IP (public): `" + ip + "`\n"
|
||||||
message += "IP (private): " + str(local_ips) + "\n"
|
message += "IP (private): `" + str(local_ips) + "`\n"
|
||||||
|
|
||||||
tot_r = np.array(self.persistence["bot"]["receive_activity"]["count"]).sum()
|
tot_r = np.array(self.persistence["bot"]["receive_activity"]["count"]).sum()
|
||||||
message += "Total messages read: " + str(tot_r) + "\n"
|
message += "Total messages read: `" + str(tot_r) + "`\n"
|
||||||
|
|
||||||
tot_s = np.array(self.persistence["bot"]["send_activity"]["count"]).sum()
|
tot_s = np.array(self.persistence["bot"]["send_activity"]["count"]).sum()
|
||||||
message += "Total messages sent: " + str(tot_s) + "\n"
|
message += "Total messages sent: `" + str(tot_s) + "`\n"
|
||||||
|
|
||||||
tot_e = np.array(self.persistence["bot"]["execute_activity"]["count"]).sum()
|
tot_e = np.array(self.persistence["bot"]["execute_activity"]["count"]).sum()
|
||||||
message += "Commands executed " + str(tot_e) + "</pre>\n"
|
message += "Commands executed `" + str(tot_e) + "`\n"
|
||||||
|
|
||||||
if wanted == "full":
|
update.message.reply_text(message, reply_markup=reply_markup)
|
||||||
message += str(dir(self.logger))
|
return FIRST
|
||||||
|
|
||||||
query.edit_message_text(
|
|
||||||
text= message
|
def send_log(self, update: Update, context: CallbackContext) -> None:
|
||||||
)
|
query = update.callback_query
|
||||||
|
wanted = query.data.replace("status-","")
|
||||||
|
query.answer()
|
||||||
|
with open("persistence/complete.log") as l:
|
||||||
|
query.message.reply_document(l)
|
||||||
|
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
33
bot2/commands/template.py
Normal file
33
bot2/commands/template.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import logging
|
||||||
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ParseMode
|
||||||
|
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters
|
||||||
|
from telegram.ext import (
|
||||||
|
Updater,
|
||||||
|
CommandHandler,
|
||||||
|
CallbackQueryHandler,
|
||||||
|
ConversationHandler,
|
||||||
|
CallbackContext,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class BotFunc():
|
||||||
|
"""Base class for a specific bot-functionality"""
|
||||||
|
def __init__(self, prst):
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
self.persistence = prst
|
||||||
|
|
||||||
|
|
||||||
|
def entry_point(self):
|
||||||
|
self.increase_counter("execute_activity")
|
||||||
|
|
||||||
|
|
||||||
|
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
|
@ -1,23 +1,18 @@
|
|||||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
from .template import *
|
||||||
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
|
|
||||||
from telegram.ext import (
|
|
||||||
Updater,
|
|
||||||
CommandHandler,
|
|
||||||
CallbackQueryHandler,
|
|
||||||
ConversationHandler,
|
|
||||||
CallbackContext,
|
|
||||||
)
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
FIRST = 1
|
FIRST = 1
|
||||||
|
|
||||||
class Weather():
|
class Weather(BotFunc):
|
||||||
"""Shows a weatherforecast for a given location"""
|
"""Shows a weatherforecast for a given location"""
|
||||||
def __init__(self, api):
|
def __init__(self, api, prst):
|
||||||
"""initialize api and persistence"""
|
"""initialize api and persistence"""
|
||||||
|
super().__init__(prst)
|
||||||
self.api = api
|
self.api = api
|
||||||
self.city = ""
|
self.city = ""
|
||||||
|
|
||||||
|
|
||||||
def create_handler(self):
|
def create_handler(self):
|
||||||
"""returns the handlers with button-logic"""
|
"""returns the handlers with button-logic"""
|
||||||
conv_handler = ConversationHandler(
|
conv_handler = ConversationHandler(
|
||||||
@ -36,6 +31,7 @@ class Weather():
|
|||||||
|
|
||||||
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||||
"""Reacts the call of the command. Prints the first buttons"""
|
"""Reacts the call of the command. Prints the first buttons"""
|
||||||
|
super().entry_point()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[
|
[
|
||||||
InlineKeyboardButton("Zürich", callback_data="city-zurich"),
|
InlineKeyboardButton("Zürich", callback_data="city-zurich"),
|
||||||
@ -44,7 +40,10 @@ class Weather():
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
update.message.reply_text("Which city?", reply_markup=reply_markup)
|
if update.message:
|
||||||
|
update.message.reply_text("Which city?", reply_markup=reply_markup)
|
||||||
|
else:
|
||||||
|
update.callback_query.edit_message_text("Which city", reply_markup=reply_markup)
|
||||||
return FIRST
|
return FIRST
|
||||||
|
|
||||||
|
|
||||||
|
86
bot2/commands/zvv.py
Normal file
86
bot2/commands/zvv.py
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
from .template import *
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import requests
|
||||||
|
|
||||||
|
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)
|
||||||
|
self.start = ""
|
||||||
|
self.dest = ""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def create_handler(self):
|
||||||
|
conv_handler = ConversationHandler(
|
||||||
|
entry_points=[CommandHandler('zvv', self.entry_point)],
|
||||||
|
states={
|
||||||
|
START: [MessageHandler(Filters.text, callback=self.get_start)],
|
||||||
|
DEST: [MessageHandler(Filters.text, callback=self.get_dest)]
|
||||||
|
},
|
||||||
|
fallbacks=[CommandHandler('zvv', self.entry_point)],
|
||||||
|
)
|
||||||
|
return conv_handler
|
||||||
|
|
||||||
|
|
||||||
|
def entry_point(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
super().entry_point()
|
||||||
|
update.message.reply_text("What is the start point?")
|
||||||
|
return START
|
||||||
|
|
||||||
|
|
||||||
|
def get_start(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
loc = update.message.text
|
||||||
|
self.start = loc
|
||||||
|
update.message.reply_text("Ok. Going from " + loc + ", what is the destination?")
|
||||||
|
return DEST
|
||||||
|
|
||||||
|
|
||||||
|
def get_dest(self, update: Update, context: CallbackContext) -> None:
|
||||||
|
loc = update.message.text
|
||||||
|
self.dest = loc
|
||||||
|
route = self.get_result()
|
||||||
|
update.message.reply_text("Her are the routes I've got:\n" + route)
|
||||||
|
return ConversationHandler.END
|
||||||
|
|
||||||
|
|
||||||
|
def get_result(self):
|
||||||
|
url = "http://transport.opendata.ch/v1/connections"
|
||||||
|
|
||||||
|
start = self.start
|
||||||
|
dest = self.dest
|
||||||
|
|
||||||
|
data = {"from" : start, "to" : dest, "limit" : 2}
|
||||||
|
try:
|
||||||
|
routes = requests.get(url, params=data).json()
|
||||||
|
result = routes["connections"]
|
||||||
|
text = result[0]["from"]["station"]["name"] + " ⏩ " + result[0]["to"]["station"]["name"] + "\n\n"
|
||||||
|
for con in result:
|
||||||
|
text += "Start: " + datetime.datetime.fromtimestamp(int(con["from"]["departureTimestamp"])).strftime("%d/%m - %H:%M") + "\n"
|
||||||
|
text += "🏁 " + datetime.datetime.fromtimestamp(int(con["to"]["arrivalTimestamp"])).strftime("%d/%m - %H:%M") + "\n"
|
||||||
|
text += "⏳ " + con["duration"] + "\n"
|
||||||
|
text += "🗺️ Route:\n"
|
||||||
|
|
||||||
|
for step in con["sections"]:
|
||||||
|
if step["journey"] != None:
|
||||||
|
text += step["journey"]["passList"][0]["station"]["name"] + " (" + datetime.datetime.fromtimestamp(int(step["journey"]["passList"][0]["departureTimestamp"])).strftime("%H:%M") + ")\n"
|
||||||
|
|
||||||
|
text += "➡️ Linie " + self.number_to_emoji(step["journey"]["number"]) + "\n"
|
||||||
|
|
||||||
|
text += step["journey"]["passList"][-1]["station"]["name"] + " (" + datetime.datetime.fromtimestamp(int(step["journey"]["passList"][-1]["arrivalTimestamp"])).strftime("%H:%M") +")\n"
|
||||||
|
else:
|
||||||
|
text += "Walk."
|
||||||
|
text += "\n"
|
||||||
|
return text
|
||||||
|
except:
|
||||||
|
return "Invalid api call."
|
||||||
|
|
||||||
|
def number_to_emoji(self, number):
|
||||||
|
out = ""
|
||||||
|
numbers = ["0️⃣","1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣","8️⃣","9️⃣"]
|
||||||
|
for i in str(number):
|
||||||
|
out += numbers[int(i)]
|
||||||
|
return str(out)
|
52
bot2/main.py
52
bot2/main.py
@ -1,16 +1,14 @@
|
|||||||
# TODO remove in the end
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
|
||||||
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
|
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
|
||||||
|
|
||||||
#from .commands import *
|
|
||||||
from . import api, commands
|
from . import api, commands
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class ChatBot():
|
class ChatBot():
|
||||||
"""better framwork - unites all functions"""
|
"""better framwork - unites all functions"""
|
||||||
|
|
||||||
def __init__(self, name, version, hw_commands, prst, logger):
|
def __init__(self, name, version, hw_commands, prst):
|
||||||
"""Inits the Bot with a few conf. vars
|
"""Inits the Bot with a few conf. vars
|
||||||
Args: -> name:str - Name of the bot
|
Args: -> name:str - Name of the bot
|
||||||
-> version:str - Version number
|
-> version:str - Version number
|
||||||
@ -24,42 +22,40 @@ class ChatBot():
|
|||||||
# self.reddit_api = api.reddit.RedditFetch()
|
# self.reddit_api = api.reddit.RedditFetch()
|
||||||
# and so on
|
# and so on
|
||||||
|
|
||||||
# Mark them as available
|
self.telegram = Updater(api.keys.telegram_api, use_context=True)
|
||||||
self.help_module = commands.help.Help()
|
self.dispatcher = self.telegram.dispatcher
|
||||||
self.sub_modules = {
|
|
||||||
"weather": commands.weather.Weather(self.api_weather),
|
|
||||||
"help" : self.help_module,
|
|
||||||
"status" : commands.status.Status(name, version, prst, logger),
|
|
||||||
|
|
||||||
|
# Mark them as available
|
||||||
|
self.help_module = commands.help.Help(prst)
|
||||||
|
self.sub_modules = {
|
||||||
|
"weather": commands.weather.Weather(self.api_weather, prst),
|
||||||
|
"help" : self.help_module,
|
||||||
|
"status" : commands.status.Status(name, version, prst),
|
||||||
|
"zvv" : commands.zvv.Zvv(prst),
|
||||||
|
"list" : commands.lists.Lists(prst),
|
||||||
|
#"alias" : commands.alias.Alias(self.dispatcher, prst),
|
||||||
|
"clock" : commands.clock.Clock(prst, hw_commands),
|
||||||
|
"plaintext" : commands.plaintext.Plain(prst) # for handling non-command messages that should simply contribute to statistics
|
||||||
}
|
}
|
||||||
# "log" : self.bot_print_log,
|
|
||||||
# "lorem" : self.bot_print_lorem,
|
|
||||||
# "weather" : self.bot_show_weather,
|
|
||||||
# "google" : self.bot_google_search,
|
|
||||||
# "events" : self.bot_print_events,
|
# "events" : self.bot_print_events,
|
||||||
# "wikipedia" : self.bot_show_wikipedia,
|
# "wikipedia" : self.bot_show_wikipedia,
|
||||||
# "zvv" : self.bot_zvv,
|
|
||||||
# "cronjob" : self.bot_cronjob,
|
# "cronjob" : self.bot_cronjob,
|
||||||
# "joke" : self.bot_tell_joke,
|
# "joke" : self.bot_tell_joke,
|
||||||
# "meme" : self.bot_send_meme,
|
# "meme" : self.bot_send_meme,
|
||||||
# "news" : self.bot_send_news,
|
# "news" : self.bot_send_news,
|
||||||
# "list" : self.bot_list,
|
# }
|
||||||
# "alias" : self.bot_save_alias,
|
|
||||||
# }, **hw_commands)
|
|
||||||
# concat bot_commands + hw-commands
|
|
||||||
# must be a class that has a method create_handler
|
# must be a class that has a method create_handler
|
||||||
|
|
||||||
self.telegram = Updater(api.keys.telegram_api, use_context=True)
|
|
||||||
self.dispatcher = self.telegram.dispatcher
|
|
||||||
|
|
||||||
self.add_commands()
|
self.add_commands()
|
||||||
self.telegram.start_polling()
|
|
||||||
self.telegram.idle()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def add_commands(self):
|
def add_commands(self):
|
||||||
self.help_module.add_commands(self.sub_modules)
|
|
||||||
for k in self.sub_modules:
|
for k in self.sub_modules:
|
||||||
self.dispatcher.add_handler(self.sub_modules[k].create_handler())
|
self.dispatcher.add_handler(self.sub_modules[k].create_handler())
|
||||||
|
|
||||||
|
self.help_module.add_commands(self.sub_modules)
|
||||||
|
|
||||||
|
def START(self):
|
||||||
|
self.telegram.start_polling()
|
||||||
|
# self.telegram.idle()
|
@ -52,7 +52,7 @@ class DashBoard():
|
|||||||
return kids
|
return kids
|
||||||
|
|
||||||
|
|
||||||
def launch_dashboard(self):
|
def START(self):
|
||||||
self.app.run_server(host=self.host_ip, port=80)#, debug=True)
|
self.app.run_server(host=self.host_ip, port=80)#, debug=True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,4 +2,4 @@
|
|||||||
|
|
||||||
sleep 30
|
sleep 30
|
||||||
cd /home/pi/AIO
|
cd /home/pi/AIO
|
||||||
sudo python3 launcher.py > persistence/log.txt
|
sudo python3 launcher.py
|
||||||
|
13
launcher.py
13
launcher.py
@ -1,5 +1,6 @@
|
|||||||
# functionality
|
# functionality
|
||||||
import bot.main
|
import bot.main
|
||||||
|
import bot2.main
|
||||||
import clock.main
|
import clock.main
|
||||||
import dashboard.main
|
import dashboard.main
|
||||||
# wrapper
|
# wrapper
|
||||||
@ -28,15 +29,18 @@ class Launcher():
|
|||||||
self.persistence["global"]["reboots"] += 1
|
self.persistence["global"]["reboots"] += 1
|
||||||
|
|
||||||
self.clock_module = clock.main.ClockFace(prst=self.persistence)
|
self.clock_module = clock.main.ClockFace(prst=self.persistence)
|
||||||
self.bot_module = bot.main.ChatBot(name="ChatterBot", version="2.3", prst=self.persistence, hw_commands=self.clock_module.commands)
|
self.bot_module = bot2.main.ChatBot(name="Norbit", version="3.0a", prst=self.persistence, hw_commands=self.clock_module.commands)
|
||||||
self.dashboard_module = dashboard.main.DashBoard(host_ip="0.0.0.0", prst=self.persistence)
|
self.dashboard_module = dashboard.main.DashBoard(host_ip="0.0.0.0", prst=self.persistence)
|
||||||
|
|
||||||
self.threads = []
|
self.threads = []
|
||||||
self.threads.append(Thread(target=self.chatbot))
|
#self.threads.append(Thread(target=self.chatbot))
|
||||||
|
|
||||||
self.threads.append(Thread(target=self.clock))
|
self.threads.append(Thread(target=self.clock))
|
||||||
self.threads.append(Thread(target=self.dashboard))
|
self.threads.append(Thread(target=self.dashboard))
|
||||||
|
|
||||||
for i in self.threads:
|
for i in self.threads:
|
||||||
i.start()
|
i.start()
|
||||||
|
self.chatbot()
|
||||||
|
|
||||||
|
|
||||||
def clock(self):
|
def clock(self):
|
||||||
@ -50,7 +54,7 @@ class Launcher():
|
|||||||
|
|
||||||
|
|
||||||
def init_persistence(self):
|
def init_persistence(self):
|
||||||
self.logger.warn("New Persistence created")
|
self.logger.warn("No persistence found, created a new one")
|
||||||
|
|
||||||
self.persistence["bot"] = {
|
self.persistence["bot"] = {
|
||||||
"send_activity" : {"hour":[], "count":[]},
|
"send_activity" : {"hour":[], "count":[]},
|
||||||
@ -70,7 +74,4 @@ class Launcher():
|
|||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
## Aand liftoff!
|
## Aand liftoff!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Launcher()
|
Launcher()
|
@ -72,3 +72,8 @@ class HookedDict(dict):
|
|||||||
if type(ret_val) == dict:
|
if type(ret_val) == dict:
|
||||||
ret_val = HookedDict(key, self, ret_val)
|
ret_val = HookedDict(key, self, ret_val)
|
||||||
return ret_val
|
return ret_val
|
||||||
|
|
||||||
|
def pop(self, k, d=None):
|
||||||
|
retvalue = super().pop(k, d)
|
||||||
|
self.parent.__setitem__(self.name, self)
|
||||||
|
return retvalue
|
||||||
|
4
t.py
4
t.py
@ -3,7 +3,9 @@ import persistence.main
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
|
level=logging.INFO,
|
||||||
|
#filename='persistence/complete.log',
|
||||||
)
|
)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
prst = persistence.main.PersistentDict("persistence/prst.json")
|
prst = persistence.main.PersistentDict("persistence/prst.json")
|
||||||
|
128
wrapper.py
128
wrapper.py
@ -1,24 +1,23 @@
|
|||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Wrapper():
|
class Wrapper():
|
||||||
"""Wrapper skeleton for the modules (bot, clock dashboard...)"""
|
"""Wrapper skeleton for the modules (bot, clock, dashboard ... maybe more to come?)"""
|
||||||
|
|
||||||
def __init__(self, own_module, *other_modules):
|
def __init__(self, own_module, *other_modules):
|
||||||
self.own = own_module
|
self.own = own_module
|
||||||
self.others = other_modules
|
self.others = other_modules
|
||||||
print("Starting " + self.__class__.__name__ + " functionality")
|
|
||||||
|
logger.debug("Starting " + self.own.__class__.__name__ + " through wrapper.")
|
||||||
|
|
||||||
|
|
||||||
|
def external_action(self, func, *args, **kwargs):
|
||||||
def mainloop(self, sleep_delta, action):
|
"""do a special action initiated by other modules"""
|
||||||
"""sleep_delta in seconds sets the sleep period of the loop
|
logger.info("External request to " + self.own.__class__.__name__ + ".")
|
||||||
action is a function that is performed every * seconds"""
|
|
||||||
print("Launching " + self.__class__.__name__ + " mainloop")
|
|
||||||
while True:
|
|
||||||
action()
|
|
||||||
time.sleep(sleep_delta)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -30,46 +29,46 @@ class ClockWrapper(Wrapper):
|
|||||||
super().__init__(own_module, *other_modules)
|
super().__init__(own_module, *other_modules)
|
||||||
self.weather = {"weather":"", "high":"", "low":"", "show":"temps"}
|
self.weather = {"weather":"", "high":"", "low":"", "show":"temps"}
|
||||||
self.weather_raw = {}
|
self.weather_raw = {}
|
||||||
self.mainloop(15)
|
self.START()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def mainloop(self, sleep_delta):
|
def START(self): # I prefer the name tick_tack
|
||||||
"""Runs the showing of the clock-face periodically (better way?)"""
|
"""Runs the showing of the clock-face periodically: update every minute"""
|
||||||
|
|
||||||
self.prev_time = "0"
|
|
||||||
|
|
||||||
def perform_loop():
|
def perform_loop():
|
||||||
if self.prev_time != datetime.datetime.now().strftime("%H%M"):
|
logger.warning("NEW TIME")
|
||||||
|
t = int(datetime.datetime.now().strftime("%H%M"))
|
||||||
|
|
||||||
if int(self.prev_time) % 5 == 0:
|
if t % 5 == 0:
|
||||||
weather = self.others[0].weather.show_weather([47.3769, 8.5417]) # zürich
|
# switch secondary face every 5 minutes
|
||||||
|
weather = self.others[0].api_weather.show_weather([47.3769, 8.5417]) # zürich
|
||||||
|
|
||||||
if weather != self.weather_raw and len(weather) != 0:
|
if weather != self.weather_raw and len(weather) != 0:
|
||||||
td = weather[1]
|
td = weather[1]
|
||||||
|
|
||||||
low = td["temps"][0]
|
low = td["temps"][0]
|
||||||
high = td["temps"][1]
|
high = td["temps"][1]
|
||||||
self.weather["weather"] = td["short"]
|
self.weather["weather"] = td["short"]
|
||||||
self.weather["high"] = high
|
self.weather["high"] = high
|
||||||
self.weather["low"] = low
|
self.weather["low"] = low
|
||||||
elif len(weather) == 0:
|
elif len(weather) == 0:
|
||||||
self.weather["weather"] = "error"
|
self.weather["weather"] = "error"
|
||||||
self.weather["high"] = "error"
|
self.weather["high"] = "error"
|
||||||
self.weather["low"] = "error"
|
self.weather["low"] = "error"
|
||||||
# if weather == self.weather.raw do nothing
|
# if weather == self.weather.raw do nothing
|
||||||
|
|
||||||
if self.weather["show"] == "weather":
|
if self.weather["show"] == "weather":
|
||||||
next = "temps"
|
next = "temps"
|
||||||
else:
|
else:
|
||||||
next = "weather"
|
next = "weather"
|
||||||
self.weather["show"] = next
|
self.weather["show"] = next
|
||||||
|
|
||||||
self.prev_time = datetime.datetime.now().strftime("%H%M")
|
self.prev_time = datetime.datetime.now().strftime("%H%M")
|
||||||
|
|
||||||
self.own.set_face(self.weather)
|
self.own.set_face(self.weather)
|
||||||
|
|
||||||
|
RepeatedTimer(60, perform_loop)
|
||||||
|
|
||||||
super().mainloop(sleep_delta,perform_loop)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -78,23 +77,7 @@ class BotWrapper(Wrapper):
|
|||||||
def __init__(self, own_module, *other_modules):
|
def __init__(self, own_module, *other_modules):
|
||||||
""""""
|
""""""
|
||||||
super().__init__(own_module, *other_modules)
|
super().__init__(own_module, *other_modules)
|
||||||
|
self.own.START()
|
||||||
self.bot = own_module
|
|
||||||
self.clock = other_modules[0]
|
|
||||||
|
|
||||||
self.mainloop(10)
|
|
||||||
|
|
||||||
|
|
||||||
def mainloop(self, sleep_delta):
|
|
||||||
"""Calls the telegram entity regularly to check for activity"""
|
|
||||||
def perform_loop():
|
|
||||||
self.bot.react_chats()
|
|
||||||
# num = self.bot.telegram.fetch_updates()
|
|
||||||
# for message in range(num):
|
|
||||||
# command, params = self.bot.react_command() # returns None if handled internally
|
|
||||||
# if command != None:
|
|
||||||
# self.clock.external_action(command, params)
|
|
||||||
super().mainloop(sleep_delta, perform_loop)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -104,5 +87,34 @@ class DashBoardWrapper(Wrapper):
|
|||||||
super().__init__(own_module, other_modules)
|
super().__init__(own_module, other_modules)
|
||||||
# self.mainloop(1 * 3600) # 1 hour refresh-cycle
|
# self.mainloop(1 * 3600) # 1 hour refresh-cycle
|
||||||
# cannot get called through mainloop, will use the included callback-functionality of Dash
|
# cannot get called through mainloop, will use the included callback-functionality of Dash
|
||||||
own_module.bot = other_modules[0]
|
self.own.bot = other_modules[0]
|
||||||
own_module.launch_dashboard()
|
self.own.START()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class RepeatedTimer(object):
|
||||||
|
def __init__(self, interval, function, *args, **kwargs):
|
||||||
|
self._timer = None
|
||||||
|
self.interval = interval
|
||||||
|
self.function = function
|
||||||
|
self.args = args
|
||||||
|
self.kwargs = kwargs
|
||||||
|
self.is_running = False
|
||||||
|
self.next_call = time.time()
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
def _run(self):
|
||||||
|
self.is_running = False
|
||||||
|
self.start()
|
||||||
|
self.function(*self.args, **self.kwargs)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
if not self.is_running:
|
||||||
|
self.next_call += self.interval
|
||||||
|
self._timer = threading.Timer(self.next_call - time.time(), self._run)
|
||||||
|
self._timer.start()
|
||||||
|
self.is_running = True
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self._timer.cancel()
|
||||||
|
self.is_running = False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user