LoginSignup
0
0

More than 1 year has passed since last update.

標準入力で入ってきたテキストをGoogle Chatに飛ばすってPython script書いたよ、って記事

Posted at

やってること

標準入力で入ってきたテキストを
Google Chatに飛ばすってだけ。

用途

時間がかかるコマンドが終わったときに
手元に通知が来るのが欲しかったんで、
そんな感じでの用途を考えてます。

なので、

$ ls | python3 stdin2chat.py

って感じで、出力された内容をそのまま渡せば、
あらかじめ設定したチャットに
メッセージとして飛んできます。

既存のテキストファイルでも

$ cat exsample.txt | python3 stdin2chat.py

って感じで飛ばすことができます。

用意するもの

Google chatのWebhook

設定

webhookの値をご自身の環境のものに書き換えれば
使えるんじゃないかと

コード

'''
	input
		STDIN
	output
		Google Chat

	sample
		$ ls | python3 stdin2chat.py
'''
import sys
import textwrap
import requests
import json

# google chat Webhook
Webhook  = 'https://chat.googleapis.com/v1/spaces/xxxxxxxxxxxx/messages?key=xxxxxxxxxxxxxxxxxxxxxxxxxxx'

ChatText = textwrap.dedent('''
	```
	{StdinText}
	```
''').format(
	StdinText = sys.stdin.read().strip(), 
	).strip()

# Send Webhook
response = requests.post(
    Webhook,
    json={"text": ChatText}
)

これから

とりあえず動いてる状態での公開。
手元ではオプションで送信先をSlackや
別のサービスとかに飛ばせるようにしてるのがあるので、
きれいに書き直す時間ができたらそちらも公開しようかと。

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