![見出し画像](https://assets.st-note.com/production/uploads/images/171350567/rectangle_large_type_2_6487056b6700f31f1ac3c375c4581030.png?width=1200)
Line Notify終了を受け、通知手段をDiscordに移行
概要
何個かの自作アプリの通知手段として長らくLine Notifyを利用していましたが、2025年3月31日に終了することが決定したため、他の通知手段を検討。
Lineの提案ではMessaging APIの利用が推されていましたが、その条件が200通/日(時間あたり7通弱)と自分にとっては厳しく、有料プランもしかたないと思ったのですが、ひとつ上のプランは5,000円/月(5,000通/日)と個人の趣味で使うには大きすぎる金額のため却下・・・
軽くDiscordを調べたところ、無料(5リクエスト/秒)かつプログラムからシンプルにメッセージを送信することが可能だったため、移行することにしました。
送信手順
(1) Webhook URLの取得
DiscordのWebページから「チャンネル設定」「連携サービス」「ウェブフック」「新しいウェブフック」から作成し、ウェブフックURLをコピーします。
![](https://assets.st-note.com/img/1737653663-pFjcmR7VA8ugoI69OsbeSaZX.png?width=1200)
(2) PythonからAPIをコールしメッセージを送信
基本的にはWebhook URLにJSON形式でメッセージをPostするだけ。
とてもシンプルです。
requests.post(URL, {"content": "メッセージ"})
もちろん画像も送信可能。
サンプルでテキスト、画像(1つのみ)に対応したClassを作ってみました。
(例外処理、バリデーションチェックは入れていません)
import requests
class DiscordSend:
def __init__(self, url):
self._url = url
def send(self, text, image_file=None, username='Bot'):
payload = {}
files = None
if text:
payload['content'] = text
if username:
payload['username'] = username
if image_file:
with open(image_file, 'rb') as f:
files = {'file': ('image.png', f, 'image/png')}
res = requests.post(self._url, data=payload, files=files)
else:
res = requests.post(self._url, json=payload)
return res
webhook_url = 'https://discord.com/api/webhooks/--------------------'
discord_send = DiscordSend(webhook_url)
discord_send.send('01 test message\n02 ---')
discord_send.send('test image', 'test1.jpg')
discord_send.send('', 'test2.jpg')
あとがき
Discordという素晴らしい移行先があって助かりました。
Line Notifyには今までお世話になり、ありがとうございました。
いいなと思ったら応援しよう!
![R-Y-O](https://assets.st-note.com/production/uploads/images/55212934/profile_7a6381aeff3ebe51b3fd0d85ee2c2849.png?width=600&crop=1:1,smart)