1
1

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にアラートを飛ばす(2022新方式版)

Last updated at Posted at 2021-03-27

はじめに

現場ではこの手の「ログをslackに投げる」みたいなのはすでに出来上がっていることが多いが実際にやったことあるかないかでは大きく違うので、メモみたいなものだが書いておく

参考

アプリケーションのテーマ自体はこっち:point_down:だけどslackの設定の仕方が古いので、

設定の仕方はこっち:point_down:の新方式で

slack側準備

https://api.slack.com/apps/
image.png
image.png
image.png
image.png

Webhook URL をコピーしといてください
image.png

source

import json
import requests


def send_message(message: str):
    response = requests.post(
        # webhook urlを貼る 
        'https://hooks.slack.com/services/cool/slack/endpoint',
        data=json.dumps({
            "channel": "random",
            "text": message,
            "icon_emoji": ":mostly_sunny:",
            "username": "weather_bot"
        })
    )
    print(response)


if __name__ == '__main__':
    TODAY = 0
    TOKYO = 130010
    url = f'https://weather.tsukumijima.net/api/forecast/city/{TOKYO}'
    weather_data = requests.get(url).json()
    txt = weather_data["title"] + '\n'
    txt += weather_data["forecasts"][TODAY]["date"] + '\n'
    txt += weather_data["forecasts"][TODAY]["telop"] + '\n'
    send_message(txt)

確認

image.png

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?