0
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?

外部ツールからGoogleChartでメッセージ送信

Last updated at Posted at 2023-12-05

議事概要

会社・個人でGooleChatを使っている人向け。外部ツール等からMessageを投げる方法。

方法

Webhook取得後、URLに向けてPostでOK。

スクリプト

⇓Python

import json
import requests

webhook_url = 'https://chat.googleapis.com/v1/spaces/xxxxxxxxxxxx'

message = {
    'cards': [
        {
            'header': {
                'title': 'title',
                'subtitle': 'subtitle',
                # 'imageUrl': 'https://xxxxx.png',
                # 'imageStyle': 'IMAGE',
            },
            'sections': [
                {
                    'widgets': [
                        {
                            'keyValue': {
                                'topLabel': 'topLabel',
                                'content': 'Hello Wrold !',
                                'onClick': {
                                    'openLink': {
                                        'url': 'https://qiita.com/'
                                    }
                                },
                                'icon': 'DESCRIPTION',
                            }
                        }
                    ]
                }
            ],
        }
    ]
}

# message = {
#   'text': 'Hello World !'
# }
headers = {'Content-Type': 'application/json; charset=UTF-8'}
response = requests.post(webhook_url, headers=headers, json=message)
print(response.status_code)

⇓PowerShell

# Webhook URL
$webhookUrl = 'https://chat.googleapis.com/v1/spaces/xxxxxxxxxxxx'

# Message
$jsonData = '{
    "cards": [
        {
            "header": {
                "title": "title",
                "subtitle": "subtitle"
            },
            "sections": [
                {
                    "widgets": [
                        {
                            "keyValue": {
                                "topLabel": "topLabel",
                                "content": "Hello Wrold !",
                                "onClick": {
                                    "openLink": {
                                        "url": "https://qiita.com/"
                                    }
                                },
                                "icon": "DESCRIPTION"
                            }
                        }
                    ]
                }
            ]
        }
    ]
}'

# $jsonData = '{
#   "text": "Hello World"
# }'

# HTTP POSTリクエスト
Invoke-RestMethod -Uri $webhookUrl -Method Post -Body $jsonData -ContentType 'application/json; charset=UTF-8'

参考

⇓公式が分かりやすい。
https://developers.google.com/chat/concepts?hl=ja

使い方

上のスクリプトはカード型のサンプルなので、クリックした際のURLや画像を設定すれば色々活用ができる。
私自身はファイル監視のスクリプトと組み合わせて、ファイルがないorファイルの更新が一定期間されていない時にGoogleChatに通知するようなことをしていた。

0
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
0
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?