diff --git a/stdin2discord.bash b/stdin2discord.bash new file mode 100644 index 0000000..695383c --- /dev/null +++ b/stdin2discord.bash @@ -0,0 +1,26 @@ +#!/bin/bash + +send() { + local _msg=$1 + if [[ -z $_msg ]]; then + return + fi + + curl -X POST \ + -H "Content-Type: application/json" \ + -d "{\"content\":\"$_msg\"}" \ + "$DISCORD_WEBHOOK" +} + +msg="" + +while IFS= read -r line; do + if [[ ${#msg}${#line} -ge 2000 ]]; then + send "$msg" + msg="" + else + msg+="$line" + fi +done + +send "$msg" diff --git a/stdin2discord.py b/stdin2discord.py deleted file mode 100644 index a55f99f..0000000 --- a/stdin2discord.py +++ /dev/null @@ -1,26 +0,0 @@ -import sys -import requests -import os - -def send(_msg: str): - if len(_msg) == 0: - return - - r = requests.post( - url=os.environ['DISCORD_WEBHOOK'], - data=f'content=```diff\n{requests.utils.quote(_msg)}```'.encode('utf-8'), - headers = {'Content-Type': 'application/x-www-form-urlencoded'} - ) - r.raise_for_status() - -msg = '' - -for line in sys.stdin: - if len(msg + line) >= 2000: - send(msg) - msg = '' - - else: - msg += line - -send(msg)