util.py 925 B

1234567891011121314151617181920212223242526272829303132333435
  1. import requests
  2. from requests.exceptions import ConnectionError
  3. def post_message(msg, bot_id):
  4. requests.post(
  5. "https://api.groupme.com/v3/bots/post",
  6. json={
  7. "bot_id": bot_id,
  8. "text": msg
  9. },
  10. timeout=10
  11. )
  12. def upload_image(key, content):
  13. try:
  14. return True, requests.post(
  15. "https://image.groupme.com/pictures",
  16. headers={
  17. "Content-Type": "image/png",
  18. "X-Access-Token": key,
  19. },
  20. data=content
  21. ).json()["payload"]["url"]
  22. except ConnectionError as ce:
  23. return False, {
  24. "explain": "Could not reach GroupMe image service.",
  25. "exception": ce
  26. }
  27. except (KeyError, JSONDecodeError) as e:
  28. return False, {
  29. "explain": "Could not parse response from GroupMe image service.",
  30. "exception": e
  31. }