forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (92 loc) · 4.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder');
var restify = require('restify');
var Promise = require('bluebird');
var url = require('url');
var Swagger = require('swagger-client');
// Swagger client for Bot Connector API
var connectorApiClient = new Swagger({
url: 'https://raw.githubusercontent.com/Microsoft/BotBuilder/master/CSharp/Library/Microsoft.Bot.Connector.Shared/Swagger/ConnectorAPI.json',
usePromise: true
});
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages
server.post('/api/messages', connector.listen());
// Bot setup
var bot = new builder.UniversalBot(connector, function (session) {
var message = session.message;
var conversationId = message.address.conversation.id;
// when a group conversation message is recieved,
// get the conversation members using the REST API and print it on the conversation.
// 1. inject the JWT from the connector to the client on every call
addTokenToClient(connector, connectorApiClient).then(function (client) {
// 2. override API client host and schema (https://api.botframework.com) with channel's serviceHost (e.g.: https://slack.botframework.com or http://localhost:NNNN)
var serviceUrl = url.parse(message.address.serviceUrl);
var serviceScheme = serviceUrl.protocol.split(':')[0];
client.setSchemes([serviceScheme]);
client.setHost(serviceUrl.host);
// 3. GET /v3/conversations/{conversationId}/members
return client.Conversations.Conversations_GetConversationMembers({ conversationId: conversationId })
.then(function (res) {
printMembersInChannel(message.address, res.obj);
});
}).catch(function (error) {
console.log('Error retrieving conversation members', error);
});
});
bot.on('conversationUpdate', function (message) {
if (message.membersAdded && message.membersAdded.length > 0) {
var membersAdded = message.membersAdded
.map(function (m) {
var isSelf = m.id === message.address.bot.id;
return (isSelf ? message.address.bot.name : m.name) || '' + ' (Id: ' + m.id + ')';
})
.join(', ');
bot.send(new builder.Message()
.address(message.address)
.text('Welcome ' + membersAdded));
}
if (message.membersRemoved && message.membersRemoved.length > 0) {
var membersRemoved = message.membersRemoved
.map(function (m) {
var isSelf = m.id === message.address.bot.id;
return (isSelf ? message.address.bot.name : m.name) || '' + ' (Id: ' + m.id + ')';
})
.join(', ');
bot.send(new builder.Message()
.address(message.address)
.text('The following members ' + membersRemoved + ' were removed or left the conversation :('));
}
});
// Helper methods
// Inject the connector's JWT token into to the Swagger client
function addTokenToClient(connector, clientPromise) {
// ask the connector for the token. If it expired, a new token will be requested to the API
var obtainToken = Promise.promisify(connector.getAccessToken.bind(connector));
return Promise.all([obtainToken(), clientPromise]).then(function (values) {
var token = values[0];
var client = values[1];
client.clientAuthorizations.add('AuthorizationBearer', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'));
return client;
});
}
// Create a message with the member list and send it to the conversationAddress
function printMembersInChannel(conversationAddress, members) {
if (!members || members.length === 0) return;
var memberList = members.map(function (m) { return '* ' + m.name + ' (Id: ' + m.id + ')'; })
.join('\n ');
var reply = new builder.Message()
.address(conversationAddress)
.text('These are the members of this conversation: \n ' + memberList);
bot.send(reply);
}