From 66481324ca45682b6dde827f58f8df29572fea4b Mon Sep 17 00:00:00 2001 From: toshi-pono <66683209+toshi-pono@users.noreply.github.com> Date: Fri, 5 Jul 2024 13:40:48 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20webhook=E3=81=AE=E4=BE=8B=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/.env.example | 2 ++ examples/webhook.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 examples/webhook.py diff --git a/examples/.env.example b/examples/.env.example index 5a57962..29edd26 100644 --- a/examples/.env.example +++ b/examples/.env.example @@ -1,2 +1,4 @@ BOT_ACCESS_TOKEN=SuperSecretToken BOT_VERIFICATION_TOKEN=SuperSecretVerificationToken +WEBHOOK_SECRET=SuperSecretWebhookSecret +WEBHOOK_ID=SuperSecretWebhookId diff --git a/examples/webhook.py b/examples/webhook.py new file mode 100644 index 0000000..e3bc5cd --- /dev/null +++ b/examples/webhook.py @@ -0,0 +1,40 @@ +import os +from os.path import join, dirname +from dotenv import load_dotenv +from aiotraq import Client +from aiotraq.api.webhook import post_webhook +import hashlib +import hmac + + +dotenv_path = join(dirname(__file__), ".env") +load_dotenv(dotenv_path) + +WEBHOOK_ID = os.getenv("WEBHOOK_ID") +WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET") + + +def main() -> None: + client = Client(base_url="https://q.trap.jp/api/v3") + + with client as client: + if WEBHOOK_ID is None or WEBHOOK_SECRET is None: + print("WEBHOOK_ID or WEBHOOK_SECRET is not set.") + return + + message = "Hello World!" + # メッセージ本文をWebhookシークレットでHMAC-SHA1でハッシュ化した結果をhex形式で表した文字列 + # https://bot-console.trap.jp/docs/webhook/send + secret = hmac.new(WEBHOOK_SECRET.encode(), message.encode(), hashlib.sha1).hexdigest() + + result = post_webhook.sync_detailed( + client=client, + webhook_id=WEBHOOK_ID, + body=message, + x_traq_signature=secret, + ) + print(result) + + +if __name__ == "__main__": + main()