DiscordのWebhookの使い方
discordのwebhookの使い方のメモ
curlを使ってdiscordに投稿を行う方法をまとめる
テキストチャンネルに co2: 1234ppm という文字列を投稿する
code:sh
printf '{"content": "co2: 1234ppm"}\n' | curl -v -H "Content-Type: application/json" -d @- "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX"
テキストチャンネルに co2: XXXXppm という文字列を投稿する(XXXXの部分はランダムな4桁の数字)
code:sh
while :; do printf '{"content": "co2: %sppm"}\n' "$(cat /dev/urandom | LC_CTYPE=C tr -dc '0-9' | fold -w 4 | head -n 1)" | curl -v -H "Content-Type: application/json" -d @- "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX"; sleep 10; done
テキストチャンネルに photo.jpg というファイル名の画像を投稿する
code:sh
curl -v -X POST -F "file=@photo.jpg" "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX"
-F あるなら -X POST なくてもいいかも
USB温湿度計の測定結果をテキストチャンネルに投稿する
pi@cm27:~ $ cat /dev/ttyACM0 | head -n 1
temp=2696;hum=6897;status=0
という感じでデータを取得できるUSB接続の温湿度計があったとすると、
code:sh
while :; do cat /dev/ttyACM0 | head -n 1| sed 's/^\(temp=\)\(....\)\(;hum=\)\(....\)\(;status=.\)$/\2 \4/' | awk '{printf("温度: %.2f°C, 湿度: %.2f%%\n", $1/100, $2/100)}' | awk '{printf("{\"content\": \"%s\"}\n", $0)}' | curl -v -w '\n' -H "Content-Type: application/json" -d @- https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXX; sleep 300; done
チャンネル名をチャンネルへのリンクにする
code:json
{"content": "<#406357894427312151> <#686801602086043674> <#616918719444025354> <#799600883892486144> <#811511454444421151> <#811513050112196658> はすべて雑談チャンネルです。好きな話題を投げ込みましょう!"}
参考:
https://qiita.com/Eai/items/1165d08dce9f183eac74