Multiple small fixes
This commit is contained in:
parent
5b5bb4b30c
commit
3b2170cf3d
@ -43,12 +43,8 @@ class TelegramIO():
|
||||
def process_message(self):
|
||||
"""Inspects the first message from self.message_queue and reacts accordingly."""
|
||||
message_data = self.message_queue.pop(0)
|
||||
current_hour = int(datetime.datetime.now().timestamp() // 3600)
|
||||
if len(self.persistence["bot"]["receive_activity"]["hour"]) == 0 or current_hour != self.persistence["bot"]["receive_activity"]["hour"][-1]:
|
||||
self.persistence["bot"]["receive_activity"]["hour"].append(current_hour)
|
||||
self.persistence["bot"]["receive_activity"]["count"].append(1)
|
||||
else:
|
||||
self.persistence["bot"]["receive_activity"]["count"][-1] += 1
|
||||
|
||||
self.increase_counter("receive_activity")
|
||||
|
||||
self.offset = message_data["update_id"] + 1
|
||||
|
||||
@ -59,8 +55,8 @@ class TelegramIO():
|
||||
self.message_id = message["message_id"]
|
||||
self.chat_id = message["chat"]["id"]
|
||||
author = message["from"]
|
||||
|
||||
if author["id"] not in self.persistence["bot"]["chat_members"]:
|
||||
|
||||
if str(author["id"]) not in self.persistence["bot"]["chat_members"]:
|
||||
name = ""
|
||||
if "first_name" in author:
|
||||
name += author["first_name"] + " "
|
||||
@ -68,7 +64,7 @@ class TelegramIO():
|
||||
name += author["last_name"]
|
||||
if len(name) == 0:
|
||||
name = "anonymous"
|
||||
self.persistence["bot"]["chat_members"][author["id"]] = name
|
||||
self.persistence["bot"]["chat_members"][str(author["id"])] = name # seems like the conversion to string is handled implicitly, but it got me really confused
|
||||
self.send_message("Welcome to this chat " + name + "!")
|
||||
|
||||
if "text" in message:
|
||||
@ -118,12 +114,7 @@ class TelegramIO():
|
||||
if (r.status_code != 200):
|
||||
raise Exception
|
||||
|
||||
current_hour = int(datetime.datetime.now().timestamp() // 3600)
|
||||
if len(self.persistence["bot"]["send_activity"]["hour"]) == 0 or current_hour != self.persistence["bot"]["send_activity"]["hour"][-1]:
|
||||
self.persistence["bot"]["send_activity"]["hour"].append(current_hour)
|
||||
self.persistence["bot"]["send_activity"]["count"].append(1)
|
||||
else:
|
||||
self.persistence["bot"]["send_activity"]["count"][-1] += 1
|
||||
self.increase_counter("send_activity")
|
||||
except:
|
||||
out = datetime.datetime.now().strftime("%d.%m.%y - %H:%M")
|
||||
out += " @ " + "telegram.send_message"
|
||||
@ -143,9 +134,17 @@ class TelegramIO():
|
||||
send_url = self.base_url + "sendPhoto"
|
||||
try:
|
||||
r = requests.post(send_url, data=data)
|
||||
self.persistence["bot"]["photos_sent"] += 1
|
||||
self.increase_counter("send_activity")
|
||||
except:
|
||||
out = datetime.datetime.now().strftime("%d.%m.%y - %H:%M")
|
||||
out += " @ " + "telegram.send_photo"
|
||||
out += " --> " + "did not send:\n" + url
|
||||
self.persistence["bot"]["log"] += [out]
|
||||
|
||||
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
|
@ -88,12 +88,7 @@ class BotFramework():
|
||||
# *params means the list is unpacked and handed over as separate arguments.
|
||||
self.telegram.send_message(result)
|
||||
|
||||
current_hour = int(datetime.datetime.now().timestamp() // 3600)
|
||||
if len(self.persistence["bot"]["execute_activity"]["hour"]) == 0 or current_hour != self.persistence["bot"]["execute_activity"]["hour"][-1]:
|
||||
self.persistence["bot"]["execute_activity"]["hour"].append(current_hour)
|
||||
self.persistence["bot"]["execute_activity"]["count"].append(1)
|
||||
else:
|
||||
self.persistence["bot"]["execute_activity"]["count"][-1] += 1
|
||||
self.telegram.increase_counter("execute_activity")
|
||||
|
||||
if self.is_command(cmd): # first word
|
||||
call_command(cmd, params)
|
||||
|
11
bot/main.py
11
bot/main.py
@ -17,7 +17,7 @@ class ChatBot(FW.BotFramework):
|
||||
"""Inits the Bot with a few conf. vars
|
||||
Args: -> name:str - Name of the bot
|
||||
-> version:str - Version number
|
||||
-> prst:shelveObj - persistence
|
||||
-> prst:dict - persistence (overloaded dict that writes to json file)
|
||||
"""
|
||||
super().__init__(name, version, prst)
|
||||
|
||||
@ -64,15 +64,20 @@ class ChatBot(FW.BotFramework):
|
||||
message = "BeebBop, this is " + self.name + " (V." + self.version + ")\n"
|
||||
try:
|
||||
ip = requests.get('https://api.ipify.org').text
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.connect(('8.8.8.8', 80))
|
||||
(addr, port) = s.getsockname()
|
||||
local_ips = addr
|
||||
except:
|
||||
ip = "not fetchable"
|
||||
local_ips = "not fetchable"
|
||||
local_ips = [i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)]
|
||||
|
||||
message += "<pre>Status: Running :green_circle:\n"
|
||||
message += "Uptime: " + delta[:delta.rfind(".")] + "\n"
|
||||
message += "Reboots: " + str(self.persistence["global"]["reboots"]) + "\n"
|
||||
message += "IP-Adress (public): " + ip + "\n"
|
||||
message += "IP-Adress (private): " + str(local_ips) + "\n"
|
||||
message += "IP (public): " + ip + "\n"
|
||||
message += "IP (private): " + str(local_ips) + "\n"
|
||||
tot_r = np.array(self.persistence["bot"]["receive_activity"]["count"]).sum()
|
||||
message += "Total messages read: " + str(tot_r) + "\n"
|
||||
|
||||
|
@ -54,7 +54,7 @@ class DashBoard():
|
||||
|
||||
|
||||
def launch_dashboard(self):
|
||||
self.app.run_server(host=self.host_ip, port=8080)#, debug=True)
|
||||
self.app.run_server(host=self.host_ip, port=80)#, debug=True)
|
||||
|
||||
|
||||
def card_header(self):
|
||||
|
@ -47,7 +47,6 @@ class Launcher():
|
||||
"send_activity" : {"hour":[], "count":[]},
|
||||
"receive_activity" : {"hour":[], "count":[]},
|
||||
"execute_activity" : {"hour":[], "count":[]},
|
||||
"photos_sent": 0,
|
||||
"log": [],
|
||||
"chat_members": {},
|
||||
"aliases" : {}
|
||||
|
@ -32,7 +32,7 @@ class PersistentDict(dict):
|
||||
def __setitem__(self, key, value):
|
||||
if self.last_action != "r":
|
||||
self.read_dict()
|
||||
|
||||
# not sure if the step to read is necessary, but I'll keep it for safety
|
||||
super().__setitem__(key,value)
|
||||
self.write_dict() # writes 'self' to a json file.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user