Skip to content

Worwae77 patch 2 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

// Weather Example
// See https://wit.ai/sungkim/weather/stories and https://wit.ai/docs/quickstart
const Wit = require('node-wit').Wit;
const FB = require('./facebook.js');
const Config = require('./const.js');

const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};

// Bot actions
const actions = {
say(sessionId, context, message, cb) {
console.log(message);
//console.log(sendGenericMessage(1234)); //test GenericMessage
// Bot testing mode, run cb() and return
if (require.main === module) {
cb();
return;
}

// Our bot has something to say!
// Let's retrieve the Facebook user whose session belongs to from context
// TODO: need to get Facebook user name
const recipientId = context._fbid_;
if (recipientId) {
// Yay, we found our recipient!
// Let's forward our bot response to her.
FB.fbMessage(recipientId, message, (err, data) => {
if (err) {
console.log(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err
);
}
// Let's give the wheel back to our bot
cb();
});
} else {
console.log('Oops! Couldn\'t find user in context:', context);
// Giving the wheel back to our bot
cb();
}
},
merge(sessionId, context, entities, message, cb) {
// Retrieve the location entity and store it into a context field
const loc = firstEntityValue(entities, 'location');
if (loc) {
context.loc = loc; // store it in context
}

cb(context);
},

error(sessionId, context, error) {
console.log(error.message);
},

// fetch-weather bot executes
['fetch-weather'](sessionId, context, cb) {
// Here should go the api call, e.g.:
// context.forecast = apiCall(context.loc)
context.forecast = 'sunny';
cb(context);
},
};

const getWit = () => {
return new Wit(Config.WIT_TOKEN, actions);
};

exports.getWit = getWit;

// bot testing mode
// http://stackoverflow.com/questions/6398196
if (require.main === module) {
console.log("Bot testing mode.");

const client = getWit();
client.interactive();
}
22 changes: 22 additions & 0 deletions const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

// Wit.ai parameters
//const WIT_TOKEN = process.env.WIT_TOKEN;
const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF";
if (!WIT_TOKEN) {
throw new Error('missing WIT_TOKEN');
}

// Messenger API parameters
const FB_PAGE_TOKEN = process.env.FB_PAGE_TOKEN;

var FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN;
if (!FB_VERIFY_TOKEN) {
FB_VERIFY_TOKEN = "just_do_it";
}

module.exports = {
WIT_TOKEN: WIT_TOKEN,
FB_PAGE_TOKEN: FB_PAGE_TOKEN,
FB_VERIFY_TOKEN: FB_VERIFY_TOKEN,
};
63 changes: 63 additions & 0 deletions facebook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

// See the Send API reference
// https://developers.facebook.com/docs/messenger-platform/send-api-reference
const request = require('request');
const Config = require('./const.js');

const fbReq = request.defaults({
uri: 'https://graph.facebook.com/me/messages',
method: 'POST',
json: true,
qs: {
access_token: Config.FB_PAGE_TOKEN
},
headers: {
'Content-Type': 'application/json'
},
});


const fbMessage = (recipientId, msg, cb) => {
const opts = {
form: {
recipient: {
id: recipientId,
},
message: {
text: msg,
},
},
};

fbReq(opts, (err, resp, data) => {
if (cb) {
cb(err || data.error && data.error.message, data);
}
});
};


// See the Webhook reference
// https://developers.facebook.com/docs/messenger-platform/webhook-reference
const getFirstMessagingEntry = (body) => {
const val = body.object === 'page' &&
body.entry &&
Array.isArray(body.entry) &&
body.entry.length > 0 &&
body.entry[0] &&
body.entry[0].messaging &&
Array.isArray(body.entry[0].messaging) &&
body.entry[0].messaging.length > 0 &&
body.entry[0].messaging[0];

return val || null;
};



module.exports = {
getFirstMessagingEntry: getFirstMessagingEntry,
fbMessage: fbMessage,
fbReq: fbReq
};
Loading