view.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. import sys
  3. from pprint import pprint
  4. import json
  5. from dotenv import load_dotenv
  6. import roman
  7. import requests
  8. def send_message(status):
  9. base_url = 'https://discordapp.com/api/webhooks/'
  10. load_dotenv()
  11. webhooks_json = os.environ["DISCORD_TOKENS"]
  12. webhooks = json.loads(webhooks_json)
  13. name = status['name']
  14. its_id = status['id']
  15. status_text = f"[{name} \[{its_id}\]](https://www.torn.com/profiles.php?XID={its_id}) → Loot level changed to "+roman.toRoman(status['integer'])
  16. status_text += "\n" + status['time_string']
  17. this_json = {'content': status_text}
  18. pprint(this_json)
  19. for webhook in webhooks:
  20. url = base_url+webhook
  21. response = requests.post(url, json=this_json)
  22. if response.status_code in [200, 204]:
  23. print("Webhook executed")
  24. else:
  25. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  26. if response.status_code == int(429):
  27. sys.exit(1)
  28. def send_alert(this_alert):
  29. base_url = 'https://discordapp.com/api/webhooks/'
  30. load_dotenv()
  31. webhooks_json = os.environ["DISCORD_TOKENS"]
  32. webhooks = json.loads(webhooks_json)
  33. this_json = {'content': this_alert}
  34. for webhook in webhooks:
  35. url = base_url+webhook
  36. response = requests.post(url, json=this_json)
  37. if response.status_code in [200, 204]:
  38. print("Webhook executed")
  39. else:
  40. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  41. if response.status_code == int(429):
  42. sys.exit(1)