Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const {
- BotFrameworkAdapter,
- MemoryStorage,
- ConversationState
- } = require('botbuilder');
- const botbuilder_dialogs = require('botbuilder-dialogs');
- const restify = require('restify');
- const server = restify.createServer();
- const dialogs = new botbuilder_dialogs.DialogSet();
- const adapter = new BotFrameworkAdapter({
- appId: '', //process.env.MICROSOFT_APP_ID,
- appPassword: '', //process.env.MICROSOFT_APP_PASSWORD
- });
- const conversationState = new ConversationState(new MemoryStorage());
- adapter.use(conversationState);
- var reservationInfo = {};
- dialogs.add('greetings', [
- async function (dc) {
- await dc.prompt('textPrompt', 'What is your name?');
- },
- async function (dc, results) {
- var userName = results;
- await dc.context.sendActivity(`Hello ${userName}!`);
- await dc.end(); // Ends the dialog
- }
- ]);
- dialogs.add('reserveTable', [
- async function(dc, args, next){
- await dc.context.sendActivity("Welcome to the reservation service.");
- reservationInfo = {}; // Clears any previous data
- await dc.prompt('dateTimePrompt', "Please provide a reservation date and time.");
- },
- async function(dc, result){
- reservationInfo.dateTime = result[0].value;
- // Ask for next info
- await dc.prompt('partySizePrompt', "How many people are in your party?");
- },
- async function(dc, result){
- reservationInfo.partySize = result;
- // Ask for next info
- await dc.prompt('textPrompt', "Whose name will this be under?");
- },
- async function(dc, result){
- reservationInfo.reserveName = result;
- // Reservation confirmation
- var msg = `Reservation confirmed. Reservation details:
- <br/>Date/Time: ${reservationInfo.dateTime}
- <br/>Party size: ${reservationInfo.partySize}
- <br/>Reservation name: ${reservationInfo.reserveName}`;
- await dc.context.sendActivity(msg);
- await dc.end();
- }
- ]);
- dialogs.add('textPrompt', new botbuilder_dialogs.TextPrompt());
- dialogs.add('dateTimePrompt', new botbuilder_dialogs.DatetimePrompt());
- dialogs.add('partySizePrompt', new botbuilder_dialogs.NumberPrompt());
- // Listen for incoming activity
- server.post('/api/messages', (req, res) => {
- adapter.processActivity(req, res, async (context) => {
- const isMessage = (context.activity.type === 'message');
- // State will store all of your information
- const convo = conversationState.get(context);
- const dc = dialogs.createContext(context, convo);
- if (isMessage) {
- // Check for valid intents
- if(context.activity.text.match(/hi/ig)){
- await dc.begin('greetings');
- }
- else if(context.activity.text.match(/reserve table/ig)){
- await dc.begin('reserveTable');
- }
- }
- if(!context.responded){
- // Continue executing the "current" dialog, if any.
- await dc.continue();
- if(!context.responded && isMessage){
- // Default message
- await context.sendActivity("Hi! I'm a simple bot. Please say 'Hi' or 'Reserve table'.");
- }
- }
- });
- });
- // Start server
- server.listen(process.env.port || process.env.PORT || 3978, function () {
- console.log(`${server.name} listening to ${server.url}`);
- });
- /**
- // Listen for incoming requests
- server.post('/api/messages', (req, res) => {
- // Route received request to adapter for processing
- adapter.processActivity(req, res, (context) => {
- if (context.activity.type === 'message') {
- let state = conversationState.get(context);
- let count = state.count === undefined ? state.count = 0 : ++state.count;
- return context.sendActivity(`"[Message (${count})]: ${context.activity.text}"`); // message count is available
- } else {
- return context.sendActivity(`"[Event]: ${context.activity.type}"`);
- }
- });
- });
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement