Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

twilio sdk upgrade from 2.x.x to 3.x.x #1140

Merged
merged 1 commit into from
Feb 14, 2018
Merged
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
238 changes: 106 additions & 132 deletions lib/TwilioIPMBot.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
var Botkit = require(__dirname + '/CoreBot.js');
var request = require('request');
var twilio = require('twilio');
var Twilio = require('twilio');
var async = require('async');

var AccessToken = twilio.AccessToken;
var IpMessagingGrant = AccessToken.IpMessagingGrant;

function Twiliobot(configuration) {

Expand All @@ -21,27 +19,15 @@ function Twiliobot(configuration) {
utterances: botkit.utterances,
};

bot.send = function(message, cb) {
bot.send = function (message, cb) {
botkit.debug('SEND ', message);

if (bot.identity === null || bot.identity === '') {
bot.api.channels(message.channel).messages.create({
body: message.text,
}).then(function(response) {
bot.api.channels(message.channel).messages.create(message)
.then(function (response) {
cb(null, response);
}).catch(function(err) {
}).catch(function (err) {
cb(err);
});
} else {
bot.api.channels(message.channel).messages.create({
body: message.text,
from: bot.identity
}).then(function(response) {
cb(null, response);
}).catch(function(err) {
cb(err);
});
}
};

bot.reply = function(src, resp, cb) {
Expand All @@ -63,40 +49,38 @@ function Twiliobot(configuration) {
bot.api.channels.list().then(function(full_channel_list) {
if (bot.config.autojoin === true) {
bot.channels = full_channel_list;
bot.channels.channels.forEach(function(chan) {
bot.channels.forEach(function(chan) {
bot.api.channels(chan.sid).members.create({
identity: bot.identity
}).then(function(response) {
botkit.debug('added ' +
bot.identity + ' as a member of the ' + chan.friendly_name);
}).fail(function(error) {
bot.identity + ' as a member of the ' + chan.friendlyName);
}).catch(function(error) {
botkit.debug('Couldn\'t join the channel: ' +
chan.friendly_name + ': ' + error);
chan.friendlyName + ': ' + error);
});
});
} else if (bot.identity) {

// load up a list of all the channels that the bot is currently

bot.channels = {
channels: []
};
bot.channels = [];

async.each(full_channel_list.channels, function(chan, next) {
async.each(full_channel_list, function(chan, next) {
bot.api.channels(chan.sid).members.list().then(function(members) {
for (var x = 0; x < members.members.length; x++) {
if (members.members[x].identity == bot.identity) {
bot.channels.channels.push(chan);
for (var x = 0; x < members.length; x++) {
if (members[x].identity === bot.identity) {
bot.channels.push(chan);
}
}
next();
}).fail(function(error) {
}).catch(function(error) {
botkit.log('Error loading channel member list: ', error);
next();
});
});
}
}).fail(function(error) {
}).catch(function(error) {
botkit.log('Error loading channel list: ' + error);
// fails if no channels exist
// set the channels to empty
Expand All @@ -111,15 +95,15 @@ function Twiliobot(configuration) {
var existingIdentity = null;

// try the get by identity thing
bot.api.users(bot.identity).get().then(function(response) {
bot.api.users(bot.identity).fetch().then(function(response) {
bot.autoJoinChannels();
}).fail(function(error) {
}).catch(function(error) {
// if not make the new user and see if they need to be added to all the channels
bot.api.users.create({
identity: bot.identity
}).then(function(response) {
bot.autoJoinChannels();
}).fail(function(error) {
}).catch(function(error) {
botkit.log('Could not get Bot Identity:');
botkit.log(error);
process.exit(1);
Expand Down Expand Up @@ -152,8 +136,8 @@ function Twiliobot(configuration) {
};


bot.client = new twilio.IpMessagingClient(config.TWILIO_ACCOUNT_SID, config.TWILIO_AUTH_TOKEN);
bot.api = bot.client.services(config.TWILIO_IPM_SERVICE_SID);
bot.client = new Twilio(config.TWILIO_ACCOUNT_SID, config.TWILIO_AUTH_TOKEN);
bot.api = bot.client.chat.services(config.TWILIO_IPM_SERVICE_SID);

if (config.identity) {
bot.identity = config.identity;
Expand Down Expand Up @@ -183,183 +167,173 @@ function Twiliobot(configuration) {
return twilio_botkit;
};

twilio_botkit.handleWebhookPayload = function(req, res, bot) {
// ensure all messages
// have a user & channel
twilio_botkit.handleWebhookPayload = function (req, res, bot) {
var message = req.body;
if (req.body.EventType == 'onMessageSent') {
twilio_botkit.ingest(bot, message, res);
};

// customize fields to be compatible with Botkit
message.text = req.body.Body;
message.from = req.body.From;
message.to = req.body.To;
message.user = req.body.From;
message.channel = req.body.ChannelSid;
//normalize the message to ensure channel, and user properties are present
twilio_botkit.middleware.normalize.use(function normalizeMessage(bot, message, next) {
if (message.EventType == 'onMessageSent') {

twilio_botkit.receiveMessage(bot, message);
// customize fields to be compatible with Botkit
message.text = message.Body;
message.from = message.From;
message.to = message.To;
message.user = message.From;
message.channel = message.ChannelSid;

}else if (req.body.EventType == 'onChannelAdded' || req.body.EventType == 'onChannelAdd') {
} else if (message.EventType == 'onChannelAdded' || message.EventType == 'onChannelAdd') {
// this event has a channel sid but not a user
message.channel = req.body.ChannelSid;
twilio_botkit.trigger(req.body.EventType, [bot, message]);
message.channel = message.ChannelSid;

}else if (req.body.EventType == 'onChannelDestroyed' || req.body.EventType == 'onChannelDestroy') {
} else if (message.EventType == 'onChannelDestroyed' || message.EventType == 'onChannelDestroy') {
// this event has a channel sid but not a user
message.channel = req.body.ChannelSid;
twilio_botkit.trigger(req.body.EventType, [bot, message]);
message.channel = message.ChannelSid;

}else if (req.body.EventType == 'onMemberAdded' || req.body.EventType == 'onMemberAdd') {
} else if (message.EventType == 'onMemberAdded' || message.EventType == 'onMemberAdd') {
// should user be MemberSid the The Member Sid of the newly added Member
message.user = req.body.Identity;
message.channel = req.body.ChannelSid;
twilio_botkit.trigger(req.body.EventType, [bot, message]);
} else if (req.body.EventType == 'onMemberRemoved' || req.body.EventType == 'onMemberRemove') {
message.user = req.body.Identity;
message.channel = req.body.ChannelSid;
twilio_botkit.trigger(req.body.EventType, [bot, message]);

if (req.body.EventType == 'onMemberRemoved') {

}
} else {
twilio_botkit.trigger(req.body.EventType, [bot, message]);
message.user = message.Identity;
message.channel = message.ChannelSid;
} else if (message.EventType == 'onMemberRemoved' || message.EventType == 'onMemberRemove') {
message.user = message.Identity;
message.channel = message.ChannelSid;
}
};

// handle events here
twilio_botkit.handleTwilioEvents = function() {
twilio_botkit.log('** Setting up custom handlers for processing Twilio messages');
twilio_botkit.on('message_received', function(bot, message) {


next();
});

if (bot.identity && message.from == bot.identity) {
return false;
//categorize the message type with appropriate checks
twilio_botkit.middleware.categorize.use(function categorizeMessage(bot, message, next) {
if (message.EventType === 'onMessageSent') {
//discard echo messages
if (bot.identity && message.from === bot.identity) {
return;
}

// message without text is probably an edit
if (!message.text) {
// message without text is probably an edit
return false;
return;
}

if (bot.identity) {
var channels = bot.channels.channels;
var channels = bot.channels;

// if its not in a channel with the bot
var apprChan = channels.filter(function(ch) {
return ch.sid == message.channel;
var apprChan = channels.filter(function (ch) {
return ch.sid === message.channel;
});

if (apprChan.length === 0) {
return false;
return;
}
}
});

message.type = 'message_received';

} else if (message.EventType === 'onMemberRemoved') {

// if a member is removed from a channel, check to see if it matches the bot's identity
// and if so remove it from the list of channels the bot listens to
twilio_botkit.on('onMemberRemoved', function(bot, message) {
if (bot.identity && message.user == bot.identity) {
if (bot.identity && message.user === bot.identity) {
// remove that channel from bot.channels.channels
var chan_to_rem = bot.channels.channels.map(function(ch) {
var chan_to_rem = bot.channels.map(function (ch) {
return ch.sid;
}).indexOf(message.channel);

if (chan_to_rem != -1) {
bot.channels.channels.splice(chan_to_rem, 1);
bot.channels.splice(chan_to_rem, 1);
twilio_botkit.debug('Unsubscribing from channel because of memberremove.');

}

} else if (bot.identity) {
var channels = bot.channels.channels;
var channels = bot.channels;

// if its not in a channel with the bot
var apprChan = channels.filter(function(ch) {
var apprChan = channels.filter(function (ch) {
return ch.sid == message.channel;
});

if (apprChan.length === 0) {
return false;
return;
}
}

if (bot.identity && bot.identity == message.user) {
twilio_botkit.trigger('bot_channel_leave', [bot, message]);
message.type = 'bot_channel_leave';
} else {
twilio_botkit.trigger('user_channel_leave', [bot, message]);
message.type = 'user_channel_leave';
}
});
} else if (message.EventType === 'onMemberAdded') {

twilio_botkit.on('onMemberAdded', function(bot, message) {
if (bot.identity && message.user == bot.identity) {
bot.api.channels(message.channel).get().then(function(response) {
bot.channels.channels.push(response);
if (bot.identity && message.user === bot.identity) {
bot.api.channels(message.channel).fetch().then(function (response) {
bot.channels.push(response);
twilio_botkit.debug('Subscribing to channel because of memberadd.');

}).fail(function(error) {
}).catch(function (error) {
botkit.log(error);
});
} else if (bot.identity) {
var channels = bot.channels.channels;
var channels = bot.channels;

// if its not in a channel with the bot
var apprChan = channels.filter(function(ch) {
var apprChan = channels.filter(function (ch) {
return ch.sid == message.channel;
});

if (apprChan.length === 0) {
return false;
return;
}
}

if (bot.identity && bot.identity == message.user) {
twilio_botkit.trigger('bot_channel_join', [bot, message]);
message.type = 'bot_channel_join';
} else {
twilio_botkit.trigger('user_channel_join', [bot, message]);
message.type = 'user_channel_join';
}

});


// if a channel is destroyed, remove it from the list of channels this bot listens to
twilio_botkit.on('onChannelDestroyed', function(bot, message) {
} else if (message.EventType === 'onChannelDestroyed') {
if (bot.identity) {
var chan_to_rem = bot.channels.channels.map(function(ch) {
var chan_to_rem = bot.channels.map(function (ch) {
return ch.sid;
}).indexOf(message.channel);
if (chan_to_rem != -1) {
bot.channels.channels.splice(chan_to_rem, 1);
if (chan_to_rem !== -1) {
bot.channels.splice(chan_to_rem, 1);
twilio_botkit.debug('Unsubscribing from destroyed channel.');
}
message.type = 'channel_destroyed'
}
});

// if a channel is created, and the bot is set in autojoin mode, join the channel
twilio_botkit.on('onChannelAdded', function(bot, message) {
} else if (message.EventType === 'onChannelAdded') {
if (bot.identity && bot.config.autojoin === true) {
// join the channel
bot.api.channels(message.channel).members.create({
identity: bot.identity
}).then(function(response) {
bot.api.channels(message.channel).get().then(function(response) {
bot.channels.channels.push(response);
}).then(function (response) {
return bot.api.channels(message.channel).fetch().then(function (response) {
bot.channels.push(response);
twilio_botkit.debug('Subscribing to new channel.');

}).fail(function(error) {
botkit.log(error);
});
}).fail(function(error) {
})
}).catch(function (error) {
botkit.log(error);
});
message.type = message.EventType;
message.type = 'channel_created';
}
});
} else {
message.type = message.EventType;
}

};
next();
});

twilio_botkit.startTicking();
twilio_botkit.middleware.format.use(function formatMessage(bot, message, platform_message, next) {
platform_message.channel = message.channel;
platform_message.body = message.text;
if (bot.identity) {
platform_message.from = bot.identity;
}
next();
});

twilio_botkit.handleTwilioEvents();
twilio_botkit.startTicking();

return twilio_botkit;

Expand Down
Loading