공공 인공지능 오픈 API·DATA 서비스 포털의 위키백과 QA API를 aws 람다로 node.js 를 이용해 slack에서 받을 수 있도록 처리했습니다.
전체적인 내용은 https://blog.aliencube.org/ko/2016/05/15/slack-github-integration-with-aws-lambda/ 를 참고하였습니다.
http://www.usefulparadigm.com/2016/04/06/creating-a-slack-bot-with-aws-lambda-and-api-gateway/
https://gist.github.com/sjoonk/20ae13e5cd8be88e9824e3bad11b2859
http://aiopen.etri.re.kr/service_list.php
exports.handler = (event, context) => {
// Sets request options
var options = {
host: 'aiopen.etri.re.kr',
port: '8000',
path: '/WikiQA',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var access_key = '발급받은키';
var type = 'irqa';
// Sets the request body
var data = {
'access_key': access_key,
'argument': {
'question': event.text,
'type': type
}
};
var request = require('http');
var req = request.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
body = JSON.parse(body);
console.log(body.result);
if(body.result==0){
var text = body.return_object.WiKiInfo.IRInfo[0].sent;
var ans = body.return_object.WiKiInfo.AnswerInfo[0].answer;
}else{
var text = "없음";
var ans = event.text;
}
context.succeed({
"response_type": "in_channel",
"text": "ETRI 위키백과 QA API 결과 : " + ans,
"attachments": [
{
"text": "https://ko.wikipedia.org/wiki/" + encodeURI(ans) + "\n" + text,
"color": "#7CD197"
}
]
});
});
});
req.write(JSON.stringify(data));
req.end();
};
aws gateway 에서 매핑템플릿에 application/x-www-form-urlencoded 를 추가하지 않으면, json 요청 값을 처리할 수 없으니, 꼭 추가하자.
(event undefined)
## convert HTML POST data to JSON ## get the raw post data from the AWS built-in variable and give it a nicer name #set($rawAPIData = $input.path('$')) ## first we get the number of "&" in the string, this tells us if there is more than one key value pair #set($countAmpersands = $rawAPIData.length() - $rawAPIData.replace("&", "").length()) ## if there are no "&" at all then we have only one key value pair. ## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs. ## the "empty" kv pair to the right of the ampersand will be ignored anyway. #if ($countAmpersands == 0) #set($rawPostData = $rawAPIData + "&") #end ## now we tokenise using the ampersand(s) #set($tokenisedAmpersand = $rawAPIData.split("&")) ## we set up a variable to hold the valid key value pairs #set($tokenisedEquals = []) ## now we set up a loop to find the valid key value pairs, which must contain only one "=" #foreach( $kvPair in $tokenisedAmpersand ) #set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length()) #if ($countEquals == 1) #set($kvTokenised = $kvPair.split("=")) #if ($kvTokenised[0].length() > 0) ## we found a valid key value pair. add it to the list. #set($devNull = $tokenisedEquals.add($kvPair)) #end #end #end ## next we set up our loop inside the output structure "{" and "}" { #foreach( $kvPair in $tokenisedEquals ) ## finally we output the JSON for this pair and append a comma if this isn't the last pair #set($kvTokenised = $kvPair.split("=")) "$util.urlDecode($kvTokenised[0])" : #if($kvTokenised[1].length() > 0)"$util.urlDecode($kvTokenised[1])"#{else}""#end#if( $foreach.hasNext ),#end #end }
댓글 달기