Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kotobuki/88ce21cbbfc3d69c67352f26fae22584 to your computer and use it in GitHub Desktop.
Save kotobuki/88ce21cbbfc3d69c67352f26fae22584 to your computer and use it in GitHub Desktop.
API.AIのFulfillmentにIFTTTのWebhooksサービスを使う ref: http://qiita.com/mayfair/items/d6440cec8104bc72c6c7
POST https://my-service.com/action
Headers:
Content-type: application/json
POST body:
{
"lang": "en",
"status": {
"errorType": "success",
"code": 200
},
"timestamp": "2017-02-09T16:06:01.908Z",
"sessionId": "1486656220806",
"result": {
...
"action": "greetings",
'use strict';
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/', (req, res) => {
// API.AIからのwebhookのヘッダに記載されたkeyをIFTTTのkeyとして使用
const KEY = req.headers['key'];
// API.AIからのwebhookのresult/actionをIFTTTのイベントのvalue1として使用
const ACTION = req.body['result']['action'];
// IFTTTのイベント名は「api_ai」で固定
var options = {
uri: 'https://maker.ifttt.com/trigger/api_ai/with/key/' + KEY,
form: {
value1: ACTION,
value2: '',
value3: ''
},
json: true
};
request.post(options, (error, response, body) => {
let responseToApiAi;
if (!error && response.statusCode === 200) {
console.log(body);
responseToApiAi = {
'speech': 'OK!',
'displayText': 'OK!'
};
} else {
console.log('error: ' + response.statusCode);
responseToApiAi = {
'speech': 'ERROR!',
'displayText': 'ERROR: ' + response.statusCode
};
}
res.json(responseToApiAi);
});
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
POST/GET https://maker.ifttt.com/trigger/{event}/with/key/*********************
Headers:
Content-Type: application/json
POST body:
{
"value1":"value1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment