Created
May 26, 2017 05:03
-
-
Save JSpiner/447a225b8abb7d17ffb9d3535f46adfb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
from flask import jsonify | |
import os | |
import json | |
import requests | |
from slackclient import SlackClient | |
# Your app's Slack bot user token | |
SLACK_BOT_TOKEN = " | |
# 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 | |
@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/[email protected] 2x' /></a>" | |
"</html>" | |
) | |
return html | |
@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 "" | |
@app.route("/slash/reco", methods=["POST"]) | |
def slash_reco(): | |
print("reco") | |
print(request.form) | |
text = request.form['text'] | |
r = requests.get( | |
'https://api.api.ai/api/query?v=20150910&query='+text+'&lang=ko&sessionId=5f1781ab-9a1d-45ca-8331-027da391a2df&timezone=2017-05-26T13:10:25+0900', | |
headers = { | |
'Authorization': 'Bearer dfasdfasdfasdfasdf' | |
} | |
) | |
channelId = request.form['channel_id'] | |
data = json.loads(r.text) | |
print(data) | |
print(data['result']) | |
print(data['result']['fulfillment']) | |
print(data['result']['fulfillment']['messages']) | |
attach = r.text | |
if data['result']['metadata']['intentName'] == "reco": | |
attach = "http://movie.naver.com/movie/running/current.nhn" | |
jsonResult = { | |
"response_type": "in_channel", | |
"text": (data['result']['fulfillment']['messages'][0]['speech']), | |
"attachments": [ | |
{ | |
"text": attach | |
} | |
] | |
} | |
elif data['result']['metadata']['intentName'] == "similar": | |
attach = "추천" | |
res = requests.get( | |
"https://openapi.naver.com/v1/search/movie?query=" + data['result']['parameters']['moviename'], | |
headers = { | |
'X-Naver-Client-Id': 'asdfasdf', | |
'X-Naver-Client-Secret': 'asdfasdf' | |
} | |
) | |
print(res) | |
attach = json.loads(res.text)['items'][0]['link'] | |
jsonResult = { | |
"response_type": "in_channel", | |
"text": (data['result']['fulfillment']['messages'][0]['speech']) + "\n" + attach, | |
} | |
else: | |
jsonResult = { | |
"response_type": "in_channel", | |
"text": (data['result']['fulfillment']['messages'][0]['speech']), | |
"attachments": [ | |
{ | |
"text": attach | |
} | |
] | |
} | |
return jsonify(jsonResult) | |
@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) | |
result = { | |
"response_type": "in_channel", | |
"text": "It's 80 degrees right now.", | |
"attachments": [ | |
{ | |
"text":"Partly cloudy today and tomorrow" | |
} | |
] | |
} | |
return result | |
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