botframework
Advanced tools
Comparing version 0.5.0 to 0.6.0
import { IBotSettings } from '../interfaces'; | ||
import { IFbResponse } from './interfaces'; | ||
import * as Promise from 'bluebird'; | ||
export interface IFacebookProfile { | ||
first_name?: string; | ||
last_name?: string; | ||
profile_pic?: string; | ||
locale?: string; | ||
timezone?: number; | ||
gender?: string; | ||
} | ||
export declare class FacebookApi { | ||
@@ -7,3 +16,4 @@ private settings; | ||
sendMessage(msg: IFbResponse): void; | ||
getUserDetails(userId: number): Promise<IFacebookProfile>; | ||
subscribe(): void; | ||
} |
"use strict"; | ||
var request = require('request'); | ||
var Promise = require('bluebird'); | ||
var BASE_API = 'https://graph.facebook.com/v2.6'; | ||
@@ -14,21 +15,46 @@ var FacebookApi = (function () { | ||
if (err) { | ||
console.log('could not send msg to fb', JSON.stringify(err, null, 2)); | ||
console.log('facebook: could not send msg to fb', JSON.stringify(err, null, 2)); | ||
throw err; | ||
} | ||
if (res.statusCode >= 400) { | ||
throw new Error("Error sending to fb " + JSON.stringify(res) + " " + JSON.stringify(msg)); | ||
throw new Error("facebook: Error sending to fb " + JSON.stringify(res) + " " + JSON.stringify(msg)); | ||
} | ||
console.log('reply sent'); | ||
console.log('facebook: reply sent'); | ||
}); | ||
}; | ||
FacebookApi.prototype.getUserDetails = function (userId) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
request.get({ | ||
url: BASE_API + "/" + userId + "?fields=first_name,last_name,profile_pic,locale,timezone,genderaccess_token=" + _this.settings.fb.access_token | ||
}, function (err, res, body) { | ||
if (err) { | ||
console.log('facebook: could not get user from facebook', JSON.stringify(err, null, 2)); | ||
reject(err); | ||
} | ||
if (res.statusCode >= 400) { | ||
console.log('facebook: could not get user from facebook', JSON.stringify(body, null, 2)); | ||
reject(new Error("facebook: Error getting profile for user " + JSON.stringify(body) + " " + JSON.stringify(userId))); | ||
} | ||
var obj = {}; | ||
try { | ||
obj = JSON.parse(body); | ||
} | ||
catch (e) { | ||
obj = body; | ||
} | ||
return resolve(obj); | ||
}); | ||
}); | ||
}; | ||
FacebookApi.prototype.subscribe = function () { | ||
request.post(BASE_API + "/me/subscribed_apps?access_token=" + this.settings.fb.access_token, function (err, res, body) { | ||
if (err) { | ||
console.log('could not subscribe to fb', JSON.stringify(err, null, 2)); | ||
console.log('facebook: could not subscribe to fb', JSON.stringify(err, null, 2)); | ||
throw err; | ||
} | ||
if (res.statusCode >= 400) { | ||
throw new Error("Error connecting to fb " + JSON.stringify(res)); | ||
throw new Error("facebook: Error connecting to fb " + JSON.stringify(res)); | ||
} | ||
console.log('subscribed to fb'); | ||
console.log('facebook: subscribed to fb'); | ||
}); | ||
@@ -35,0 +61,0 @@ }; |
import { IBotController, IBotSettings, IBotReply, IBotReplyListItem } from '../interfaces'; | ||
import { IFbCallback, IFbMessaging } from './interfaces'; | ||
import { FacebookApi } from './api'; | ||
import * as Promise from 'bluebird'; | ||
export declare class FacebookReply implements IBotReply { | ||
@@ -14,9 +15,14 @@ private recipientId; | ||
private botController; | ||
profiles: Object; | ||
fbApi: FacebookApi; | ||
constructor(settings: IBotSettings, botController: IBotController); | ||
receiveMessage(fbMessage: IFbCallback): boolean; | ||
dispatchSingleMessage(messaging: IFbMessaging): void; | ||
private dispatchAttachmentMessage(messaging); | ||
dispatchSingleMessage(messaging: IFbMessaging): Promise<void>; | ||
private dispatchAttachmentMessage(messaging, user); | ||
/** | ||
* add passed email in authentication callback | ||
*/ | ||
private getNewUserFromMessage(messaging); | ||
private getUserProfile(userId); | ||
private getUserFromMessage(messaging); | ||
} |
"use strict"; | ||
var interfaces_1 = require('./interfaces'); | ||
var api_1 = require('./api'); | ||
var Promise = require('bluebird'); | ||
var FacebookReply = (function () { | ||
@@ -42,2 +43,3 @@ function FacebookReply(recipientId, fbApi) { | ||
this.botController = botController; | ||
this.profiles = {}; | ||
this.fbApi = null; | ||
@@ -66,25 +68,30 @@ this.fbApi = new api_1.FacebookApi(settings); | ||
FacebookBot.prototype.dispatchSingleMessage = function (messaging) { | ||
var _this = this; | ||
var reply = new FacebookReply(messaging.sender.id, this.fbApi); | ||
// console.log('dispatching..'); | ||
if (messaging.optin) { | ||
var user = this.getNewUserFromMessage(messaging); | ||
return this.botController.newUser(user, reply); | ||
return this.getNewUserFromMessage(messaging) | ||
.then(function (user) { | ||
return _this.botController.newUser(user, reply); | ||
}); | ||
} | ||
if (messaging.message) { | ||
if (messaging.message.text) { | ||
var textMessage = { | ||
user: this.getUserFromMessage(messaging), | ||
text: messaging.message.text | ||
}; | ||
console.log(JSON.stringify(textMessage)); | ||
return (this.botController.textMessage) ? this.botController.textMessage(textMessage, reply) : null; | ||
return this.getUserFromMessage(messaging) | ||
.then(function (user) { | ||
if (messaging.message) { | ||
if (messaging.message.text) { | ||
var textMessage = { | ||
user: user, | ||
text: messaging.message.text | ||
}; | ||
console.log(JSON.stringify(textMessage)); | ||
return (_this.botController.textMessage) ? _this.botController.textMessage(textMessage, reply) : null; | ||
} | ||
if (messaging.message.attachments) { | ||
return _this.dispatchAttachmentMessage(messaging, user); | ||
} | ||
} | ||
if (messaging.message.attachments) { | ||
return this.dispatchAttachmentMessage(messaging); | ||
} | ||
} | ||
return (this.botController.catchAll) ? this.botController.catchAll(this.getUserFromMessage(messaging), messaging, reply) : null; | ||
return (_this.botController.catchAll) ? _this.botController.catchAll(user, messaging, reply) : null; | ||
}); | ||
}; | ||
FacebookBot.prototype.dispatchAttachmentMessage = function (messaging) { | ||
var user = this.getUserFromMessage(messaging); | ||
FacebookBot.prototype.dispatchAttachmentMessage = function (messaging, user) { | ||
var reply = new FacebookReply(messaging.sender.id, this.fbApi); | ||
@@ -118,15 +125,39 @@ for (var _i = 0, _a = messaging.message.attachments; _i < _a.length; _i++) { | ||
}; | ||
/** | ||
* add passed email in authentication callback | ||
*/ | ||
FacebookBot.prototype.getNewUserFromMessage = function (messaging) { | ||
// get user details from fb api | ||
// graph.facebook.com/v2.6/message.sender.id | ||
//then | ||
return { | ||
id: messaging.sender.id.toString(), | ||
email: messaging.optin.ref | ||
}; | ||
return this.getUserFromMessage(messaging) | ||
.then(function (profile) { | ||
profile.email = messaging.optin.ref; | ||
return profile; | ||
}); | ||
}; | ||
FacebookBot.prototype.getUserProfile = function (userId) { | ||
var _this = this; | ||
if (this.profiles[userId]) { | ||
return Promise.resolve(this.profiles[userId]); | ||
} | ||
return this.fbApi.getUserDetails(userId) | ||
.then(function (profile) { | ||
_this.profiles[userId] = profile; | ||
return profile; | ||
}) | ||
.catch(function (err) { | ||
console.log('facebook: could not get profile, returning empty'); | ||
return {}; | ||
}); | ||
}; | ||
FacebookBot.prototype.getUserFromMessage = function (messaging) { | ||
return { | ||
id: messaging.sender.id.toString() | ||
}; | ||
return this.getUserProfile(messaging.sender.id) | ||
.then(function (profile) { | ||
return { | ||
id: messaging.sender.id.toString(), | ||
firstname: profile.first_name || null, | ||
lastname: profile.last_name || null, | ||
avatar: profile.profile_pic || null, | ||
gender: profile.gender || null, | ||
timezone: profile.timezone || null, | ||
}; | ||
}); | ||
}; | ||
@@ -133,0 +164,0 @@ return FacebookBot; |
@@ -7,2 +7,4 @@ export interface IBotUser { | ||
avatar?: string; | ||
gender?: string; | ||
timezone?: number; | ||
} | ||
@@ -9,0 +11,0 @@ export interface ITextMessage { |
{ | ||
"name": "botframework", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Framework for messaging bots", | ||
@@ -39,2 +39,3 @@ "main": "./dist/es5/src/bot.js", | ||
"dependencies": { | ||
"bluebird": "^3.3.5", | ||
"hapi": "^13.4.0", | ||
@@ -41,0 +42,0 @@ "request": "^2.72.0" |
import * as request from 'request'; | ||
import {IBotSettings} from '../interfaces'; | ||
import {IFbResponse} from './interfaces'; | ||
import * as Promise from 'bluebird'; | ||
const BASE_API = 'https://graph.facebook.com/v2.6'; | ||
export interface IFacebookProfile { | ||
first_name?: string; | ||
last_name?: string; | ||
profile_pic?: string; | ||
locale?: string; | ||
timezone?: number; | ||
gender?: string; | ||
} | ||
export class FacebookApi { | ||
@@ -17,24 +28,49 @@ | ||
if (err) { | ||
console.log('could not send msg to fb', JSON.stringify(err, null, 2)); | ||
console.log('facebook: could not send msg to fb', JSON.stringify(err, null, 2)); | ||
throw err; | ||
} | ||
if (res.statusCode >= 400) { | ||
throw new Error(`Error sending to fb ${JSON.stringify(res)} ${JSON.stringify(msg)}`); | ||
throw new Error(`facebook: Error sending to fb ${JSON.stringify(res)} ${JSON.stringify(msg)}`); | ||
} | ||
console.log('reply sent'); | ||
console.log('facebook: reply sent'); | ||
}); | ||
} | ||
public getUserDetails(userId: number): Promise<IFacebookProfile> { | ||
return new Promise( (resolve: Function, reject: Function) => { | ||
request.get({ | ||
url: `${BASE_API}/${userId}?fields=first_name,last_name,profile_pic,locale,timezone,genderaccess_token=${this.settings.fb.access_token}` | ||
}, (err, res, body) => { | ||
if (err) { | ||
console.log('facebook: could not get user from facebook', JSON.stringify(err, null, 2)); | ||
reject(err); | ||
} | ||
if (res.statusCode >= 400) { | ||
console.log('facebook: could not get user from facebook', JSON.stringify(body, null, 2)); | ||
reject(new Error(`facebook: Error getting profile for user ${JSON.stringify(body)} ${JSON.stringify(userId)}`)); | ||
} | ||
var obj = {}; | ||
try { | ||
obj = JSON.parse(body); | ||
} catch (e) { | ||
obj = body; | ||
} | ||
return resolve(<any> obj); | ||
}); | ||
}); | ||
} | ||
public subscribe() { | ||
request.post(`${BASE_API}/me/subscribed_apps?access_token=${this.settings.fb.access_token}`, (err, res, body) => { | ||
if (err) { | ||
console.log('could not subscribe to fb', JSON.stringify(err, null, 2)); | ||
console.log('facebook: could not subscribe to fb', JSON.stringify(err, null, 2)); | ||
throw err; | ||
} | ||
if (res.statusCode >= 400) { | ||
throw new Error(`Error connecting to fb ${JSON.stringify(res)}`); | ||
throw new Error(`facebook: Error connecting to fb ${JSON.stringify(res)}`); | ||
} | ||
console.log('subscribed to fb'); | ||
console.log('facebook: subscribed to fb'); | ||
}); | ||
} | ||
} |
import {IBotController, IBotSettings, IBotUser, ITextMessage, IBotReply, IBotReplyListItem} from '../interfaces' | ||
import {IFbResponse, IFbCallback, FB_RESPONSE_ATTACHMENT_PAYLOAD_TYPE, FB_RESPONSE_ATTACHMENT_TYPE, IFbMessaging, FB_ATTACHMENT_TYPE, FB_MESSAGE_TYPE} from './interfaces'; | ||
import {FacebookApi} from './api'; | ||
import {FacebookApi, IFacebookProfile} from './api'; | ||
import * as Promise from 'bluebird'; | ||
@@ -36,2 +37,3 @@ export class FacebookReply implements IBotReply { | ||
export class FacebookBot { | ||
profiles: Object = {}; | ||
fbApi: FacebookApi = null; | ||
@@ -63,25 +65,28 @@ constructor(private settings: IBotSettings, private botController: IBotController) { | ||
if (messaging.optin) { | ||
let user = this.getNewUserFromMessage(messaging); | ||
return this.botController.newUser(user, reply); | ||
return this.getNewUserFromMessage(messaging) | ||
.then( (user: IBotUser) => { | ||
return this.botController.newUser(user, reply); | ||
}); | ||
} | ||
if (messaging.message) { | ||
if (messaging.message.text) { | ||
let textMessage: ITextMessage = { | ||
user: this.getUserFromMessage(messaging), | ||
text: messaging.message.text | ||
return this.getUserFromMessage(messaging) | ||
.then( (user: IBotUser) => { | ||
if (messaging.message) { | ||
if (messaging.message.text) { | ||
let textMessage: ITextMessage = { | ||
user, | ||
text: messaging.message.text | ||
} | ||
console.log(JSON.stringify(textMessage)); | ||
return (this.botController.textMessage) ? this.botController.textMessage(textMessage, reply) : null; | ||
} | ||
console.log(JSON.stringify(textMessage)); | ||
return (this.botController.textMessage) ? this.botController.textMessage(textMessage, reply) : null; | ||
if (messaging.message.attachments) { | ||
return this.dispatchAttachmentMessage(messaging, user); | ||
} | ||
} | ||
if (messaging.message.attachments) { | ||
return this.dispatchAttachmentMessage(messaging); | ||
} | ||
} | ||
return (this.botController.catchAll) ? this.botController.catchAll(this.getUserFromMessage(messaging), messaging, reply) : null; | ||
return (this.botController.catchAll) ? this.botController.catchAll(user, messaging, reply) : null; | ||
}); | ||
} | ||
private dispatchAttachmentMessage(messaging: IFbMessaging) { | ||
let user = this.getUserFromMessage(messaging); | ||
private dispatchAttachmentMessage(messaging: IFbMessaging, user: IBotUser) { | ||
let reply: FacebookReply = new FacebookReply(messaging.sender.id, this.fbApi); | ||
@@ -115,17 +120,42 @@ for ( var attachment of messaging.message.attachments) { | ||
private getNewUserFromMessage(messaging: IFbMessaging): IBotUser { | ||
// get user details from fb api | ||
// graph.facebook.com/v2.6/message.sender.id | ||
//then | ||
return { | ||
id: messaging.sender.id.toString(), | ||
email: messaging.optin.ref | ||
}; | ||
/** | ||
* add passed email in authentication callback | ||
*/ | ||
private getNewUserFromMessage(messaging: IFbMessaging): Promise<IBotUser> { | ||
return this.getUserFromMessage(messaging) | ||
.then( (profile: IBotUser) => { | ||
profile.email = messaging.optin.ref; | ||
return profile; | ||
}); | ||
} | ||
private getUserFromMessage(messaging: IFbMessaging): IBotUser { | ||
return { | ||
id: messaging.sender.id.toString() | ||
}; | ||
private getUserProfile(userId: number): Promise<IFacebookProfile> { | ||
if (this.profiles[userId]) { | ||
return Promise.resolve(this.profiles[userId]); | ||
} | ||
return this.fbApi.getUserDetails(userId) | ||
.then( (profile: IFacebookProfile) => { | ||
this.profiles[userId] = profile; | ||
return profile; | ||
}) | ||
.catch( (err: any) => { | ||
console.log('facebook: could not get profile, returning empty'); | ||
return {}; | ||
}) | ||
} | ||
private getUserFromMessage(messaging: IFbMessaging): Promise<IBotUser> { | ||
return this.getUserProfile(messaging.sender.id) | ||
.then( (profile: IFacebookProfile) => { | ||
return { | ||
id: messaging.sender.id.toString(), | ||
firstname: profile.first_name || null, | ||
lastname: profile.last_name || null, | ||
avatar: profile.profile_pic || null, | ||
gender: profile.gender || null, | ||
timezone: profile.timezone || null, | ||
}; | ||
}); | ||
} | ||
} | ||
@@ -132,0 +162,0 @@ |
@@ -7,3 +7,5 @@ | ||
lastname?: string; | ||
avatar?: string; | ||
avatar?: string; //url | ||
gender?: string; | ||
timezone?: number; | ||
} | ||
@@ -10,0 +12,0 @@ |
@@ -22,2 +22,3 @@ { | ||
"./typings/main/definitions/sinon/index.d.ts", | ||
"./typings/main/definitions/bluebird/index.d.ts", | ||
"./typings/main/ambient/mocha/index.d.ts", | ||
@@ -24,0 +25,0 @@ "./src/bot.ts", |
{ | ||
"dependencies": { | ||
"bluebird": "registry:npm/bluebird#3.3.4+20160515010139", | ||
"request": "registry:npm/request#2.69.0+20160428223725", | ||
@@ -4,0 +5,0 @@ "sinon": "registry:npm/sinon#1.16.0+20160427193336" |
/// <reference path="browser/ambient/hapi/index.d.ts" /> | ||
/// <reference path="browser/ambient/mocha/index.d.ts" /> | ||
/// <reference path="browser/ambient/node/index.d.ts" /> | ||
/// <reference path="browser/definitions/bluebird/index.d.ts" /> | ||
/// <reference path="browser/definitions/chai/index.d.ts" /> | ||
/// <reference path="browser/definitions/request/index.d.ts" /> | ||
/// <reference path="browser/definitions/sinon/index.d.ts" /> |
/// <reference path="main/ambient/hapi/index.d.ts" /> | ||
/// <reference path="main/ambient/mocha/index.d.ts" /> | ||
/// <reference path="main/ambient/node/index.d.ts" /> | ||
/// <reference path="main/definitions/bluebird/index.d.ts" /> | ||
/// <reference path="main/definitions/chai/index.d.ts" /> | ||
/// <reference path="main/definitions/request/index.d.ts" /> | ||
/// <reference path="main/definitions/sinon/index.d.ts" /> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
771389
64
15550
3
+ Addedbluebird@^3.3.5
+ Addedbluebird@3.7.2(transitive)