2
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 1 year has passed since last update.

PythonでSlackにメッセージを通知(Webhook 機能)

Last updated at Posted at 2023-09-13

slackで適当なチャンネルを作成

 image.png

slack で Incoming Webhook の設定

slackでIncoming Webhooks の設定をすることで、
Pythonから対象チャンネルに通知を飛ばすことができるようになります。
設定方法
① Incoming Webhookの設定ページへアクセス
https://my.slack.com/services/new/incoming-webhook/
 image.png
先ほど作成したチェンネル"python通知"を選択。
「Incoming Webhook インテグレーションの追加」をクリック
遷移先のページでWebhook URLが発行されるので、メモっておく
一番下までスクロールして、「設定を保存」をクリック
image.png

方法1 ”Requests”というHTTPライブラリを使用

pip install requests
SlackRequests.py
import requests
import json

web_hook_url = 'https://hooks.slack.com/services/先のWebhook URL'

requests.post(web_hook_url, data=json.dumps({
    #メッセージ
    "text" : "Pythonから投稿テスト",
}))

image.png

方法2 ”slackweb”というライブラリを使用

pip install slackweb
post_slack.py
import slackweb
web_hook_url = 'https://hooks.slack.com/services/先のWebhook URL'
slack = slackweb.Slack(url=web_hook_url)
slack.notify(text="This is a test.")

image.png

方法3 Slack SDKを使用

 以下記事を参照してください。

2
0
1

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