forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (69 loc) · 2.69 KB
/
app.js
File metadata and controls
80 lines (69 loc) · 2.69 KB
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
//load environment variables from the .env file
require('dotenv-extended').load();
//loading modules
var express = require("express");
var restify = require("restify");
var botbuilder = require("botbuilder");
var request = require("request-promise");
//create an express server
var app = express();
var port = process.env.port || process.env.PORT || 3978;
app.listen(port, function () {
console.log('%s listening in port %s', app.name, port);
});
//create a chat connector for the bot
var connector = new botbuilder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
//load the botbuilder classes and build a unversal bot using the chat connector
var bot = new botbuilder.UniversalBot(connector);
//hook up bot endpoint
app.post("/api/messages", connector.listen());
//root dialog
bot.dialog("/", function (session) {
console.log("-------------------------------------------------");
console.log("Bot Received Message at '/' dialogue endpoint: ");
//detect Facebook Messenger message here
if (session.message.address.channelId === "facebook") {
session.send("Facebook message recognized!");
session.beginDialog("/send_share_button");
} else session.send("Channel other than Facebook recognized.");
});
//where we create a facebook share button using sourceEvent
bot.dialog("/send_share_button", function (session) {
//construct a new message with the current session context
var msg = new botbuilder.Message(session).sourceEvent({
//specify the channel
facebook: {
//format according to channel's requirements
//(in our case, the above JSON required by Facebook)
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "Microsoft Bot Framework",
subtitle: "Check it out!",
buttons: [
{
type: "web_url",
url: "https://dev.botframework.com",
title: "Go to Dev Portal"
},
{
//this is our share button
type: "element_share"
}
]
}
]
}
} //end of attachment
}
});
//send message
session.send(msg);
session.endDialog("Show your friends!");
});