Skip to content

Instantly share code, notes, and snippets.

@hidsh
Last active January 23, 2022 00:23
Show Gist options
  • Save hidsh/c6898c3fb12289b8b6d67e69fec45497 to your computer and use it in GitHub Desktop.
Save hidsh/c6898c3fb12289b8b6d67e69fec45497 to your computer and use it in GitHub Desktop.
Python: CUIでGitHubの草画像をSlackに投稿する

slack-post-github-grass

CUIでSlackに草画像を投稿します。

投稿先のワークスペースはこちら → 招待リンク

Install

$ cd /usr/local/bin   # とか PATH の通ったところに移動
$ curl -O https://gist.githubusercontent.com/hidsh/c6898c3fb12289b8b6d67e69fec45497/raw/slack-post-github-grass.py
$ chmod +x ./slack-post-github-grass.py

Usage

$ slack-post-github-grass [GITHUB_ID]

Thanks

#!/usr/bin/env python
import sys
import requests
import json
import datetime
# slack setting
# See https://slack.com/intl/ja-jp/help/articles/115005265063-Slack-%E3%81%A7%E3%81%AE-Incoming-Webhook-%E3%81%AE%E5%88%A9%E7%94%A8
SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/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'
# 招待リンク
SLACK_LINK = 'https://w1642772442-p87414758.slack.com/'
def get_emoji():
morning = datetime.time(4, 00, 00)
night = datetime.time(21, 00, 00)
start = datetime.time(9, 00, 00)
noon = datetime.time(12, 00, 00)
now = datetime.datetime.now().time()
if now < morning or now > night:
return ':octocat:' # 深夜
elif now < start:
return ':medal:' # 早朝
elif now < noon:
return ':sunny:' # AM
else:
return ':slightly_smiling_face:' # PM
if __name__ == "__main__":
if(len(sys.argv) != 2):
print(f'Usage: python3 {sys.argv[0]} [GITHUB_ID]')
else:
# github setting
github_id = sys.argv[1]
post_json = {
'text': f'{github_id}が草を生やしました{get_emoji()}',
'attachments': [{
'fields': [{
# 'title': 'title',
# 'value': 'value',
}],
'image_url': f'https://grass-graph.appspot.com/images/{github_id}.png'
}]
}
try:
requests.post(SLACK_WEBHOOK_URL, data = json.dumps(post_json))
except Exception as e:
print(e)
else:
print(f'Done. See {SLACK_LINK}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment