-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
169 lines (141 loc) · 5.69 KB
/
server.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
@author Nils Whitmont <[email protected]>
@summary Slack Bot example for MS Bot Framework
*/
'use strict';
// INIT NODE MODULES
var bb = require('botbuilder');
// Setup Restify Server
var botServer = require('restify').createServer();
botServer.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', botServer.name, botServer.url);
});
// lets us know our bot is online when visiting the deployment URL in browser
botServer.get('/', function (request, response, next) {
response.send({ status: 'online' });
next();
});
// status route
botServer.get('/status', function statusHandler(request, response, next) {
response.send('Bot server online.');
next();
});
// Create chatConnector for communicating with the Bot Framework Service
var chatConnector = new bb.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
botServer.post('/api/messages', chatConnector.listen());
// Create your bot with a function to receive messages from the user
var bot = new bb.UniversalBot(chatConnector);
var HeroCardName = 'Hero Card';
var SlackButtonsName = 'Slack Buttons';
var BasicTextName = 'Basic Text';
var CodeFormatTextName = 'Code Formatting';
var SlackChannelData = 'Slack Channel Data';
var DemoNames = [
HeroCardName,
SlackButtonsName,
BasicTextName,
CodeFormatTextName,
SlackChannelData
];
bot.dialog('/', [
function (session) {
session.send('Hi, I am Slack Support Bot!\n\n I will show you what is possible with Bot Framework for Slack.');
bb.Prompts.choice(session, 'Choose a demo', DemoNames);
},
function (session, result) {
console.log('result:\n');
console.log(result);
switch (result.response.entity) {
case HeroCardName:
session.beginDialog('herocard');
break;
case SlackButtonsName:
session.beginDialog('buttons');
break;
case BasicTextName:
session.beginDialog('basicMessage');
break;
case CodeFormatTextName:
session.beginDialog('code-formatting');
break;
case SlackChannelData:
session.beginDialog('slack-channel-data');
break;
default:
session.send('invalid choice');
break;
}
}
]);
bot.dialog('herocard', [
function (session) {
var heroCard = new bb.Message(session)
.textFormat(bb.TextFormat.xml)
.attachments([
new bb.HeroCard(session)
.title("Hero Card")
.subtitle("Space Needle")
.text("The Space Needle is an observation tower in Seattle, Washington, a landmark of the Pacific Northwest, and an icon of Seattle.")
.images([
bb.CardImage.create(session, "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/320px-Seattlenighttimequeenanne.jpg")
])
.tap(bb.CardAction.openUrl(session, "https://en.wikipedia.org/wiki/Space_Needle"))
]);
session.endDialog(heroCard);
}
]);
bot.dialog('basicMessage', function (session) {
session.endDialog('A basic message');
});
bot.dialog('exit', function (session) {
session.endConversation('Goodbye!');
}).triggerAction({ matches: /exit/i });
bot.dialog('buttons', [
function (session) {
session.send('What kind of sandwich would you like?');
bb.Prompts.choice(session, "Choose a sandwich:", ["Tuna", "Roast Beef", "Veggie Special"]);
},
function (session, result) {
session.endDialog(`You picked: ${result.response.entity}`)
}
]).triggerAction({ matches: /button/i });
bot.dialog('code-formatting', function (session) {
session.send("Code with back-tick style code formatting: `var foo = 'bar';`");
session.send("Code in double parens with single backtick `var code = 'formatted correctly?';`");
session.send("Code in double parens with triple backtick: ```var code.formatted = true;```");
session.send('Code in single parens with single backtick `var code = "formatted correctly?"`');
session.send('Code in single parens with triple backtick: ```var code.formatted = true;```');
session.endDialog();
}).triggerAction({ matches: /code/i });
bot.dialog('slack-channel-data', function(session) {
var message = {};
message.channelData = {
text: ":tada: Code with back-tick style code formatting: `var foo = 'bar';`"
}
session.send("Sending first message:");
session.send(message);
session.send("Sending second message:");
session.send({"channelData": { "text": "Code in double parens with triple backtick: ```var code.formatted = true;```"}});
session.send("Sending third message:");
session.send({"channelData": { 'text': 'Code in single parens with single backtick `var code = "formatted correctly?"`'}});
session.send("Sending fourth message:");
session.send({"channelData": { 'text': 'Code in single parens with triple backtick: ```var code.formatted = true;```'}});
session.send("Sending fifth message:");
session.send({
channelData: {
text: "channel data text with code formatting: \`var foo = 'formatted text'\`"
}
});
session.send("Sending 6th message:");
session.send({
"channelData": {
"text": "*Channel data* _text_ with triple backtick:\n```var foo = 'formatted text'```"
}
});
session.endDialog();
}).triggerAction({matches: /channel/i});
// END OF LINE