Forráskód Böngészése

Set up bot to get API information from the config file, based on the sender's address.

DeadTOm 2 napja
szülő
commit
bb701636a3
1 módosított fájl, 16 hozzáadás és 7 törlés
  1. 16 7
      owncastcom.py

+ 16 - 7
owncastcom.py

@@ -12,15 +12,21 @@ import os
 logging.basicConfig(filename=config.log_file, level=logging.INFO)
 
 
-def get_now():  # This creates and returns a time stamp
+sender_jids = []
+for sender in config.senders:  # Put together list of sender JIDs
+    sender_jids.append(sender)
+
+
+def get_now():  # This creates and returns a time stamp for the log
     now = str(time.strftime("%Y/%m/%d %H:%M:%S"))
     return now
 
 
-def send_ownchat(msg):
+def send_ownchat(msg, auth_bearer, api_url):
+    logging.info(f'Sending message to Owncast server.')
     msg_body = {'body': msg}
-    headers = {'Authorization': config.auth_bearer}
-    response = requests.post(f'{config.api_url}/api/integrations/chat/send', data=json.dumps(msg_body),
+    headers = {'Authorization': auth_bearer}
+    response = requests.post(f'{api_url}/api/integrations/chat/send', data=json.dumps(msg_body),
                              headers=headers)
     return response.json()
 
@@ -52,12 +58,15 @@ class CommandBot(ClientXMPP):
             self.sender = str(self.sender)  # Convert to string so split() can work with it
             self.sender = self.sender.split('/')[0]  # Only save the information before the forward slash
             time.sleep(1)
-            if self.sender in config.approved_senders:  # Check if the sender has permission to send commands
-                self.send = send_ownchat(self.msg['body'])
+            if self.sender in sender_jids:  # Check if the sender is on the list
+                logging.info(f'Recieved message from  \"{self.sender}\".')
+                self.auth_bearer = config.senders[sender][0]
+                self.api_url = config.senders[sender][1]
+                self.send = send_ownchat(self.msg['body'], self.auth_bearer, self.api_url)
                 self.result = self.send
                 logging.info(f'Recieved response \"{self.result}\" from Owncast server.')
             else:  # If not, say so
-                logging.info(f'{self.sender} sent a command, but is not in the approved senders list.')
+                logging.info(f'{self.sender} sent a command, but is not in the senders list.')
                 self.msg.reply('Access denied. You are not on the list.').send()