Added saperate sending and receiving

Je kan nu de server check muten. en ik heb een framework gemaakt voor het versturen naar verschilende personen.
This commit is contained in:
Bart Koolen 2025-02-28 15:30:21 +01:00
parent 094cb63d2e
commit 608a2c95d2
5 changed files with 75 additions and 23 deletions

View file

@ -0,0 +1 @@
,bart,bart-T420,28.02.2025 12:04,file:///home/bart/.config/libreoffice/4;

View file

@ -7,8 +7,8 @@ from collections import deque
##### Mqtt vars ##### ##### Mqtt vars #####
broker_address = "192.168.1.250" broker_address = "192.168.1.250"
broker_port = 1883 broker_port = 1883
recieve_topic = "telegrambot/in" recieve_topic = "telegrambot/in/glucose/gj"
send_topic = "telegrambot/out" send_topic = "telegrambot/out/gj"
brokercreds = ["hass","Bruk#5"] brokercreds = ["hass","Bruk#5"]
##### Variables ##### ##### Variables #####
@ -145,5 +145,6 @@ Mqtt.connect(broker_address, broker_port, 60) #Set Connection
Mqtt.loop_start() #Start connecting Mqtt.loop_start() #Start connecting
Mqtt.publish(send_topic, "Started Glucose Checker :-)")
if __name__ == "__main__": if __name__ == "__main__":
main(url, headers) main(url, headers)

View file

@ -8,22 +8,34 @@ from time import sleep
##### Mqtt vars ##### ##### Mqtt vars #####
broker_address = "192.168.1.250" broker_address = "192.168.1.250"
broker_port = 1883 broker_port = 1883
recieve_topic = "telegrambot/in" recieve_topic = "telegrambot/in/server/#"
send_topic = "telegrambot/out" send_topic = "telegrambot/out/"
brokercreds = ["hass","Bruk#5"] brokercreds = ["hass","Bruk#5"]
####################### #######################
# Setup For Variables # # Setup For Variables #
####################### #######################
class User: # Custom classes for my dictionaries
def __init__(self, name, lastName, userID, mute):
self.name = name
self.lastName = lastName
self.userID = userID
self.mute = mute
User = {
"7570193598": User(name="Bart", lastName="Koolen", userID="7570193598", mute=0),
"7909366331": User(name="GJ", lastName="Koolen", userID="7909366331", mute=0),}
mute_time = 86400 #This is in seconds, it defines how long the program /mute command lasts
interval_offline_message = 300
class Site: #Custom classes for my dictionaries class Site: #Custom classes for my dictionaries
def __init__(self, url, typeVar): def __init__(self, url, typeVar):
self.url = url self.url = url
self.typeVar = typeVar self.typeVar = typeVar
webServers = {"Google" : Site(url="https://google.com", typeVar="URL"), #List with all the servers urls and names and types webServers = {"Google" : Site(url="https://google.com", typeVar="URL"), #List with all the servers urls and names and types
"GameServer" : Site(url="http://192.168.0.251", typeVar="URL"), "GameServer" : Site(url="http://192.168.1.251", typeVar="URL"),
"SpiritBody" : Site(url="https://spiritbodyhealing.org", typeVar="URL"), "SpiritBody" : Site(url="https://spiritbodyhealing.org", typeVar="URL"),
"HeresYourSign": Site(url="https://heresyoursign.nl", typeVar="URL"), "HeresYourSign": Site(url="https://heresyoursign.nl", typeVar="URL"),
#"SMB_server": Site(url="192.168.1.250", typeVar="SMB"), #"SMB_server": Site(url="192.168.1.250", typeVar="SMB"),
@ -130,8 +142,24 @@ def on_connect(client, userdata, flags, reason_code, properties): #This runs o
def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved
checkWebServer(webServers) #Update list of websites status checkWebServer(webServers) #Update list of websites status
messageStatus = statusCheck(True) #True returns every server and state messageStatus = statusCheck(True) #True returns every server and state
print(messageStatus) user_topic = msg.topic.split("/")[-1]
Mqtt.publish(send_topic, messageStatus) args = msg.payload.decode()
print(user_topic)
keys = list(User.keys())
if not args:
return
elif args[0] == "m":
for i in keys:
if User[i].name.lower() == user_topic:
print(f"right: {user_topic}: {i}")
if User[i].mute > 0:
User[keys[0]].mute = 0
User[keys[1]].mute = 0
else:
User[i].mute = mute_time//interval_offline_message #This is defined in the setup variables
elif args[0] == "s":
print("s")
Mqtt.publish(send_topic + user_topic , messageStatus)
Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Creates a mqqt object Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Creates a mqqt object
Mqtt.on_connect = on_connect #Create callback on connect Mqtt.on_connect = on_connect #Create callback on connect
@ -143,11 +171,21 @@ Mqtt.connect(broker_address, broker_port, 60) #Set Connectio
Mqtt.loop_start() #Start connecting Mqtt.loop_start() #Start connecting
def main(): #This is the main loop def main(): #This is the main loop
keys = list(User.keys())
while True: while True:
checkWebServer(webServers) #Update list of websites status checkWebServer(webServers) #Update list of websites status
messageStatus = statusCheck(False) #False is only on state change messageStatus = statusCheck(False) #False is only on state change
Mqtt.publish(send_topic, messageStatus) if User[keys[0]].mute == 0 | User[keys[1]].mute == 0:
Mqtt.publish(send_topic + "broadcast", messageStatus)
sleep(30) sleep(30)
if User[keys[0]].mute > 0:
User[keys[0]].mute = User[keys[0]].mute - 1
if User[keys[1]].mute > 0:
User[keys[1]].mute = User[keys[1]].mute - 1
print(User[keys[0]].mute)
print(User[keys[1]].mute)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -9,12 +9,14 @@ from time import sleep
##### Mqtt vars ##### ##### Mqtt vars #####
broker_address = "192.168.1.250" broker_address = "192.168.1.250"
broker_port = 1883 broker_port = 1883
recieve_topic = "telegrambot/out" recieve_topic = "telegrambot/out/#"
send_topic = "telegrambot/in" send_topic_server = "telegrambot/in/server/"
send_topic_glucose = "telegrambot/in/glucose/"
brokercreds = ["hass","Bruk#5"] brokercreds = ["hass","Bruk#5"]
x = "AS" x = "AS"
flag = False flag = " "
##### Bot Variabels ##### ##### Bot Variabels #####
BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE'
class User: # Custom classes for my dictionaries class User: # Custom classes for my dictionaries
def __init__(self, name, lastName, userID, mute): def __init__(self, name, lastName, userID, mute):
@ -22,7 +24,6 @@ class User: # Custom classes for my dictionaries
self.lastName = lastName self.lastName = lastName
self.userID = userID self.userID = userID
self.mute = mute self.mute = mute
BotToken = '7690417088:AAGcmBegbLHBDXMBKiE6MEgCp3rnLz5vPCE'
User = { User = {
"7570193598": User(name="Bart", lastName="Koolen", userID="7570193598", mute=0), "7570193598": User(name="Bart", lastName="Koolen", userID="7570193598", mute=0),
@ -31,13 +32,15 @@ User = {
##### Main ##### ##### Main #####
async def subscribe(update: Update, context: ContextTypes.DEFAULT_TYPE): #This command can be run to test setting up a database for users async def subscribe(update: Update, context: ContextTypes.DEFAULT_TYPE): #This command can be run to test setting up a database for users
await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.chat.id) await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.chat.id)
Mqtt.publish(send_topic,1) #Mqtt.publish(send_topic,1)
async def gluc_command(update: Update, context: CallbackContext): #This command can be run to test setting up a database for users async def gluc_command(update: Update, context: CallbackContext): #This command can be run to test setting up a database for users
args = ' '.join(context.args) args = ' '.join(context.args)
print(args) current_user = User[str(update.effective_chat.id)].name
#await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.chat.id) Mqtt.publish(send_topic_glucose + current_user.lower(), args)
Mqtt.publish(send_topic, args) async def server_command(update: Update, context: CallbackContext):
args = ' '.join(context.args)
current_user = User[str(update.effective_chat.id)].name
Mqtt.publish(send_topic_server + current_user.lower(), args)
def on_connect(client, userdata, flags, reason_code, properties): def on_connect(client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}") print(f"Connected with result code {reason_code}")
@ -52,17 +55,26 @@ def on_connect(client, userdata, flags, reason_code, properties):
def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved def on_message(client, userdata, msg): #This runs everytime a mqtt message is recieved
global x global x
global flag global flag
print(msg.topic)
x = msg.payload.decode() x = msg.payload.decode()
flag = True flag = msg.topic.split("/")[-1]
async def Cat(context: ContextTypes.DEFAULT_TYPE): #This runs evert second to check for if a mqtt message is recieved async def Cat(context: ContextTypes.DEFAULT_TYPE): #This runs evert second to check for if a mqtt message is recieved
global x global x
global flag global flag
keys = list(User.keys()) keys = list(User.keys())
print(flag) print(flag)
if flag == True: if flag == "broadcast":
#await context.bot.send_message(chat_id=keys[0], text=x) await context.bot.send_message(chat_id=keys[0], text=x)
await context.bot.send_message(chat_id=keys[1], text=x) await context.bot.send_message(chat_id=keys[1], text=x)
flag = False flag = " "
else:
for i in keys:
if User[i].name.lower() == flag:
#await context.bot.send_message(chat_id=keys[0], text=x)
await context.bot.send_message(chat_id=i, text=x)
flag = " "
flag = " "
Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
Mqtt.on_connect = on_connect Mqtt.on_connect = on_connect
@ -76,13 +88,13 @@ def main():#This is the main loop
application = ApplicationBuilder().token(BotToken).build() application = ApplicationBuilder().token(BotToken).build()
gluc_handler = CommandHandler('g', gluc_command) gluc_handler = CommandHandler('g', gluc_command)
subscribe_handler = CommandHandler('subscribe', subscribe) subscribe_handler = CommandHandler('subscribe', subscribe)
#mute_handler = CommandHandler('mute', mute) server_handler = CommandHandler('s', server_command)
job_queue = application.job_queue job_queue = application.job_queue
job_minute = job_queue.run_repeating(Cat , interval=1, first=1) job_minute = job_queue.run_repeating(Cat , interval=1, first=1)
#job_offline = job_queue.run_repeating(callback_offline , interval=interval_offline_message) #job_offline = job_queue.run_repeating(callback_offline , interval=interval_offline_message)
application.add_handler(gluc_handler) application.add_handler(gluc_handler)
application.add_handler(subscribe_handler) application.add_handler(subscribe_handler)
#application.add_handler(mute_handler) application.add_handler(server_handler)
application.run_polling() application.run_polling()
if __name__ == '__main__': if __name__ == '__main__':