1
0

nagbot.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import requests
  2. import json
  3. import sys
  4. import time
  5. import config
  6. import logging
  7. import requests
  8. logging.basicConfig(filename=config.logfile, level=logging.INFO)
  9. def get_now(): # Create a timestamp for logging
  10. now = str(time.strftime("%Y/%m/%d %H:%M:%S"))
  11. return now
  12. def live_now(): # Check if the stream is live
  13. response = requests.get(f'{config.owncast_url}/api/status')
  14. response = response.json()
  15. return response['online']
  16. def send_msg(msg):
  17. if live_now() is True:
  18. logging.info(f'{get_now()} - Sending \"{nag}\"...')
  19. new_status = {'body': msg}
  20. headers = {'Authorization': f'Bearer {config.auth_bearer}'}
  21. response = requests.post(f'{config.owncast_url}/api/integrations/chat/send', data=json.dumps(new_status), headers=headers)
  22. return response.json()
  23. else:
  24. logging.info(f'{get_now()} - Owncast stream is not live, so not sending messages.')
  25. def main():
  26. logging.info(f'{get_now()} - Starting nagbot...')
  27. for nag in config.nags:
  28. send_msg(nag)
  29. time.sleep(config.interval) # Wait between messages
  30. try:
  31. while True:
  32. main()
  33. except KeyboardInterrupt:
  34. logging.info(f'{get_now()} - Ctrl-alt-delete caught. Exiting program...')
  35. print('Ctrl-alt-delete caught. Exiting program...')
  36. sys.exit()
  37. except Exception as error:
  38. logging.info(f'{get_now()} - \n{error}\n')
  39. sys.exit()