3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

facebook Graph API を使ってworkplaceにメッセージを通知する

Last updated at Posted at 2021-08-20

業務案件で、facebook Graph API を使ってworkplaceにメッセージを通知する事があったのでメモ。
※ちなみに送信できるメッセージは2000文字です。

import requests

def _send_message_workplace(message: str):
    """
    workplaceにメッセージを送信
    """
    # メッセージの上限を超えた部分は削除して送信する
    if len(message) > 2000:
        warn_msg = 'message is omitted to first 2000 characters.'
        print(warn_msg)
        message = message[:2000]

    # アクセストークンを事前に生成しておく必要がある
    token = 'xxxxxxxxx'
    
    # 通知先のグループチャットのID
    thread_key = 't_xxxxxxxxxxxxxxx'

    headers = {
        'Content-type': 'application/json',
        'Authorization': 'Bearer ' + token
    }

    data = {
        "recipient": {
            "thread_key": thread_key
        },
        "message": {
            "text": message
        }
    }

    post_url = 'https://graph.facebook.com/v10.0/me/messages'

    print("sending message...")
    response = requests.post(post_url, headers=headers, json=data)

参考
https://developers.facebook.com/docs/graph-api/

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?