view.py 1017 B

123456789101112131415161718192021222324252627282930
  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)