Skip to content

Instantly share code, notes, and snippets.

@safx
Created December 17, 2014 11:39
Show Gist options
  • Save safx/b2cfc0ac6595867b23e3 to your computer and use it in GitHub Desktop.
Save safx/b2cfc0ac6595867b23e3 to your computer and use it in GitHub Desktop.
NuLabのTypetalk上でNTTドコモの雑談対話APIを利用するサンプルです (Google Apps Script)。 http://safx-dev.blogspot.jp/2014/12/typetalkwebhooknttapigoogle-apps-script.html
var dialogueUrl = 'https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
var typetalkUrl = 'https://typetalk.in/api/v1/topics/xxxx?typetalkToken=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
var botName = 'foobar';
function doPost(e) {
var jsonString = e.postData.getDataAsString();
var post = JSON.parse(jsonString).post;
var user = '@' + post.account.name
var message = getMessage(post.message);
var responseMessage = getDialogueMessage(post.account.id, message)
postMessage(user, responseMessage)
}
function init() {
var props = PropertiesService.getScriptProperties();
props.deleteAllProperties()
}
function postMessage(user, message) {
var options = {
'method': 'POST',
'payload': { 'message': user + ' ' + message }
};
UrlFetchApp.fetch(typetalkUrl, options);
}
function getMessage(mes) {
var re = new RegExp('@' + botName + '\\+', 'i');
return mes.replace(re, '');
}
function getDialogueMessage(userId, mes) {
var contextId = 'context' + userId;
var dialogue_options = {
'utt': mes
}
var props = PropertiesService.getScriptProperties();
var context = props.getProperty(contextId);
if (context) {
dialogue_options.context = context
}
var options = {
'method': 'POST',
'contentType': 'text/json',
'payload': JSON.stringify(dialogue_options)
};
var response = UrlFetchApp.fetch(dialogueUrl, options);
var content = JSON.parse(response.getContentText());
props.setProperty(contextId, content.context);
return content.utt;
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment