Instantly share code, notes, and snippets.
Created
May 26, 2017 02:37
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save JSpiner/2524a6f5eaff35ef957b27bb24f24b06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
from flask import Flask, request, make_response, Response | |
import os | |
import json | |
import requests | |
from slackclient import SlackClient | |
# Your app's Slack bot user token | |
SLACK_BOT_TOKEN = "xoxb-" | |
# Slack client for Web API requests | |
slack_client = SlackClient(SLACK_BOT_TOKEN) | |
client_id = "" | |
client_secret = "" | |
# Flask webserver for incoming traffic from Slack | |
app = Flask(__name__) | |
# Post a message to a channel, asking users if they want to play a game | |
#앱 설치 버튼이 나오는 url | |
@app.route("/", methods=["GET"]) | |
def home(): | |
html = ( | |
"<html>" | |
"<a href='https://slack.com/oauth/authorize?scope=channels:write+commands+bot+chat:write:bot+users:read+channels:read+im:read+groups:read+groups:write&client_id="+client_id+"'><img alt='Add to Slack' " | |
"height='40' width='139' src='https://platform.slack-edge.com/img/add_to_slack.png' " | |
"srcset='https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x' /></a>" | |
"</html>" | |
) | |
return html | |
#oauth | |
@app.route("/oauth", methods=["GET"]) | |
def oauth(): | |
code = request.args.get('code') | |
r = requests.post("https://slack.com/api/oauth.access", | |
data = { | |
'client_id' : client_id, | |
'client_secret' : client_secret, | |
'code' : code | |
} | |
) | |
print(r.text) | |
return "welcome : " + str(r.text) | |
#버튼 클릭시 오느느 콜백 | |
@app.route("/interactive_callback", methods=["POST"]) | |
def interactive_callback(): | |
print("interaction callback") | |
print request.form['payload'] | |
payload = json.loads(request.form['payload']) | |
channelId = payload['channel']['id'] | |
print(channelId) | |
slack_client.api_call( | |
"chat.postMessage", | |
channel=payload['channel']['id'], | |
text=payload['actions'][0]['value'] | |
) | |
return "" | |
# slash commend 입력시 오는 | |
@app.route("/slash/hack-test", methods=["POST"]) | |
def slask_hack_test(): | |
print("slash commend") | |
# Parse the request payload | |
print(request.form) | |
print(request.form['user_id']) | |
channelId = request.form['channel_id'] | |
response = slack_client.api_call( | |
"chat.postMessage", | |
channel=channelId, | |
text="버튼 테스트 ", | |
attachments= [ | |
{ | |
"text": "Choose a game to play", | |
"fallback": "You are unable to choose a game", | |
"callback_id": "wopr_game", | |
"color": "#3AA3E3", | |
"attachment_type": "default", | |
"actions": [ | |
{ | |
"name": "game", | |
"text": "Chess", | |
"type": "button", | |
"value": "chess" | |
}, | |
{ | |
"name": "game", | |
"text": "Falken's Maze", | |
"type": "button", | |
"value": "maze" | |
}, | |
{ | |
"name": "game", | |
"text": "Thermonuclear War", | |
"style": "danger", | |
"type": "button", | |
"value": "war", | |
"confirm": { | |
"title": "Are you sure?", | |
"text": "Wouldn't you prefer a good game of chess?", | |
"ok_text": "Yes", | |
"dismiss_text": "No" | |
} | |
} | |
] | |
} | |
] | |
) | |
print(response) | |
return "" | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment