forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
140 lines (124 loc) · 4.91 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var Swagger = require('swagger-client');
var open = require('open');
var rp = require('request-promise');
// config items
var pollInterval = 1000;
var directLineSecret = 'DIRECTLINE_SECRET';
var directLineClientName = 'DirectLineClient';
var directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';
var directLineClient = rp(directLineSpecUrl)
.then(function (spec) {
// client
return new Swagger({
spec: JSON.parse(spec.trim()),
usePromise: true
});
})
.then(function (client) {
// add authorization header to client
client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));
return client;
})
.catch(function (err) {
console.error('Error initializing DirectLine client', err);
});
// once the client is ready, create a new conversation
directLineClient.then(function (client) {
client.Conversations.Conversations_StartConversation() // create conversation
.then(function (response) {
return response.obj.conversationId;
}) // obtain id
.then(function (conversationId) {
sendMessagesFromConsole(client, conversationId); // start watching console input for sending new messages to bot
pollMessages(client, conversationId); // start polling messages from bot
});
});
// Read from console (stdin) and send input to conversation using DirectLine client
function sendMessagesFromConsole(client, conversationId) {
var stdin = process.openStdin();
process.stdout.write('Command> ');
stdin.addListener('data', function (e) {
var input = e.toString().trim();
if (input) {
// exit
if (input.toLowerCase() === 'exit') {
return process.exit();
}
// send message
client.Conversations.Conversations_PostActivity(
{
conversationId: conversationId,
activity: {
textFormat: 'plain',
text: input,
type: 'message',
from: {
id: directLineClientName,
name: directLineClientName
}
}
}).catch(function (err) {
console.error('Error sending message:', err);
});
process.stdout.write('Command> ');
}
});
}
// Poll Messages from conversation using DirectLine client
function pollMessages(client, conversationId) {
console.log('Starting polling message for conversationId: ' + conversationId);
var watermark = null;
setInterval(function () {
client.Conversations.Conversations_GetActivities({ conversationId: conversationId, watermark: watermark })
.then(function (response) {
watermark = response.obj.watermark; // use watermark so subsequent requests skip old messages
return response.obj.activities;
})
.then(printMessages);
}, pollInterval);
}
// Helpers methods
function printMessages(activities) {
if (activities && activities.length) {
// ignore own messages
activities = activities.filter(function (m) { return m.from.id !== directLineClientName });
if (activities.length) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
// print other messages
activities.forEach(printMessage);
process.stdout.write('Command> ');
}
}
}
function printMessage(activity) {
if (activity.text) {
console.log(activity.text);
}
if (activity.attachments) {
activity.attachments.forEach(function (attachment) {
switch (attachment.contentType) {
case "application/vnd.microsoft.card.hero":
renderHeroCard(attachment);
break;
case "image/png":
console.log('Opening the requested image ' + attachment.contentUrl);
open(attachment.contentUrl);
break;
}
});
}
}
function renderHeroCard(attachment) {
var width = 70;
var contentLine = function (content) {
return ' '.repeat((width - content.length) / 2) +
content +
' '.repeat((width - content.length) / 2);
}
console.log('/' + '*'.repeat(width + 1));
console.log('*' + contentLine(attachment.content.title) + '*');
console.log('*' + ' '.repeat(width) + '*');
console.log('*' + contentLine(attachment.content.text) + '*');
console.log('*'.repeat(width + 1) + '/');
}