Skip to content

Commit

Permalink
✨ webhookの例を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
toshi-pono committed Jul 5, 2024
1 parent 05ed590 commit 6648132
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
BOT_ACCESS_TOKEN=SuperSecretToken
BOT_VERIFICATION_TOKEN=SuperSecretVerificationToken
WEBHOOK_SECRET=SuperSecretWebhookSecret
WEBHOOK_ID=SuperSecretWebhookId
40 changes: 40 additions & 0 deletions examples/webhook.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6648132

Please sign in to comment.