Skip to content

Instantly share code, notes, and snippets.

@yue82
Created July 8, 2016 16:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yue82/bd2c3eb7f5434da7a1537d24d7f645cd to your computer and use it in GitHub Desktop.
Save yue82/bd2c3eb7f5434da7a1537d24d7f645cd to your computer and use it in GitHub Desktop.
Slack Progress Checker with Interactive Button
# -*- coding: utf-8 -*-
import requests
import json
from datetime import date
def make_month_prog_msg(work):
grades = ['B4', 'M1', 'M2']
month = date.today().month
return '{}さん,{}の進捗どうですか?'.format(grades[month%3], work)
def post_progress_msg(work):
post_url = 'https://slack.com/api/chat.postMessage'
token = 'xoxp-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXX'
channel = 'your_channel'
username = '進捗チェックさん'
icon_emoji = ':checkered_flag:'
callback_id = 'lab_clean'
text = make_month_prog_msg(work)
attachments = [{
'fallback': text,
'text': text,
'callback_id': callback_id,
'color': '#EE2222',
'attachment_type': 'default',
'actions': [{
'name': 'done',
'text': '終わった!',
'type': 'button'
}]
}]
payload = {
'token': token,
'channel': channel,
'username': username,
'icon_emoji': icon_emoji,
'attachments': json.dumps(attachments)
}
res = requests.post(post_url, data=payload)
print res.status_code
if __name__ == '__main__':
post_progress_msg('掃除')
# -*- coding: utf-8 -*-
import os
from bottle import route, run, request
import requests
import json
def post_ok_msg(params):
post_url = 'https://slack.com/api/chat.postMessage'
token = 'xoxp-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXX'
callback_id = params['callback_id']
callback_id_done = callback_id + '_done'
channel = params['channel']['name']
username = params['original_message']['username']
icon_emoji = params['original_message']['icons']['emoji']
original_text = params['original_message']['attachments'][0]['text']
text = 'Done!'
if callback_id == 'lab_clean':
text = 'ありがとう!綺麗になったよ!'
attachments = [{
'fallback': text,
'text': text,
'callback_id': callback_id_done,
'color': '#22EE22',
'attachment_type': 'default'
}]
payload = {
'token': token,
'channel': channel,
'username': username,
'icon_emoji': icon_emoji,
'attachments': json.dumps(attachments)
}
res = requests.post(post_url, data=payload)
print res.status_code
return original_text
@route('/', method='POST')
def index():
params = json.loads(request.params.get('payload'))
text = post_ok_msg(params)
return text
run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment