以前ラズパイで作った店の呼び出しボタンだけどLine Nofityがサービス終了になるという事でDiscordの通知を追加した。該当部のソース
#!/usr/bin/env python3
# coding: utf-8
import os
import requests
import json
def send_discord_message(message_content):
"""
DiscordのWebhookにメッセージを送信する関数
PCでログインしているとandroidのポップアップ通知出ない模様
エラーハンドリングしていない
:param message_content: 送信するメッセージ
"""
headers = {"Content-Type": "application/json"}
payload = {"content": message_content}
webhook_url = os.environ['DISCORD_WEBHOOK']
response = requests.post(webhook_url, data=json.dumps(payload), headers=headers)
if __name__ == '__main__':
send_discord_message( '2018/11/12 固定' )
その他追加箇所
--- a/control.py
+++ b/control.py
@@ -11,6 +11,8 @@ import RPi.GPIO as GPIO
from mail import visitmail
from line_notify import line_notify
+from discord_notify import send_discord_message
+
#from voice import make_voice
def handler(signal, frame):
@@ -52,6 +54,7 @@ def callBuzzer(channel):
str_current_time = current_time.strftime("%Y/%m/%d %H:%M:%S")
visitmail(str_current_time)
line_notify(str_current_time)
+ send_discord_message(str_current_time)
#make_voice(str_current_time)
webhook_url = os.environ[‘DISCORD_WEBHOOK’]のwebhook_url はこちらを参考に取得。
os.environはserviceのEnvironmentFileから仕込んであるけどwebhook_url べた書きでも多分困らない。
なんとなく作業中の注意点メモ。下からのツリー