view.py 969 B

1234567891011121314151617181920212223242526272829
  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. this_json = {'content': status_text}
  17. pprint(this_json)
  18. for webhook in webhooks:
  19. url = base_url+webhook
  20. response = requests.post(url, json=this_json)
  21. if response.status_code in [200, 204]:
  22. print("Webhook executed")
  23. else:
  24. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  25. if response.status_code == int(429):
  26. sys.exit(1)