1234567891011121314151617181920212223242526272829303132333435 |
- import httpx
-
- import sbenv
-
- async def send_report(msg):
- hook_url = sbenv.get_report_webhook()
- if hook_url is None:
- raise RuntimeError('webhook envvar not specified')
-
- chunks = chunk_message(msg, wrap_chunks='```')
- chunks.insert(0, '**New Report:**')
-
- async with httpx.AsyncClient() as client:
- for c in chunks:
- r = await client.post(hook_url, data={'content': c})
- r.raise_for_status()
-
- MAX_MSG_SIZE = 1950
-
- def chunk_message(msg, wrap_chunks=None):
- lines = msg.splitlines()
-
- startwrap = wrap_chunks + '\n' if wrap_chunks is not None else ''
- endwrap = '\n' + wrap_chunks if wrap_chunks is not None else ''
-
- msgs = []
- cur_line = startwrap
- for l in lines:
- if len(cur_line) + len(l) >= MAX_MSG_SIZE:
- msgs.append(cur_line + endwrap)
- cur_line = startwrap
- cur_line += '\n' + l
-
- msgs.append(cur_line + endwrap)
- return msgs
|