You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reporthook.py 946B

1234567891011121314151617181920212223242526272829303132333435
  1. import httpx
  2. import sbenv
  3. async def send_report(msg):
  4. hook_url = sbenv.get_report_webhook()
  5. if hook_url is None:
  6. raise RuntimeError('webhook envvar not specified')
  7. chunks = chunk_message(msg, wrap_chunks='```')
  8. chunks.insert(0, '**New Report:**')
  9. async with httpx.AsyncClient() as client:
  10. for c in chunks:
  11. r = await client.post(hook_url, data={'content': c})
  12. r.raise_for_status()
  13. MAX_MSG_SIZE = 1950
  14. def chunk_message(msg, wrap_chunks=None):
  15. lines = msg.splitlines()
  16. startwrap = wrap_chunks + '\n' if wrap_chunks is not None else ''
  17. endwrap = '\n' + wrap_chunks if wrap_chunks is not None else ''
  18. msgs = []
  19. cur_line = startwrap
  20. for l in lines:
  21. if len(cur_line) + len(l) >= MAX_MSG_SIZE:
  22. msgs.append(cur_line + endwrap)
  23. cur_line = startwrap
  24. cur_line += '\n' + l
  25. msgs.append(cur_line + endwrap)
  26. return msgs