Skip to content

Latest commit

 

History

History
134 lines (108 loc) · 3.16 KB

README.md

File metadata and controls

134 lines (108 loc) · 3.16 KB

CircleCI

DeepDialog Node Client

Bot building superpowers.

Documentation

Example

// dialogs/astrologer.js
import {FlowDialog} from 'deepdialog';

export const Astrologer = new FlowDialog({
  name: "Astrologer",
  flows: {
    onStart: [
      "Hi, I'm Sybil the Astrologer...",
      { type:'wait', seconds:2 }, // a pregnant pause
      {
        start: ["Sys:YesNoPrompt", {
          text: "I can read your horoscope. Would you like that?"
        }],
        then: {
          if: ({value})=>value,
          then: {
            text: "Would you like to subscribe for daily readings?",
            actions: {
              yes: [
                { set: {Subscribed: true} }, //
                "You are now subscribed for daily readings"
              ],
              no: [
                { set: {Subscribed: false} },
                "Ok, I won't subscribe you to daily readings"
              ]
            }
          },
          else: "I'm sorry, I'm just a simple bot.  I can only tell horoscopes."
        }
      }
    ]
  }
});

// dialogs/readhoroscope.js
export {FlowDialog} from 'deepdialog';

export const ReadHoroscope = new FlowDialog({
  name: 'ReadHoroscope',
  flows: {
    start: {
      if: ({ZodiacSign})=>!ZodiacSign,
      then: async ({ZodiacSign}) => {
        var horoscope = await retrieveHoroscope(ZodiacSign),
        session.send(horoscope);
      },
      else: {
        start: "PickSign",
        then: {
          if: {result}=>result,
          then: async ({result}) => await session.save({ZodiacSign: result});
        }
      }
    }
  }
});

// dialogs/picksign.js
// A dialog which enables the user to choose their Zodiac sign
// etc.

Get started

Step by step instructions - or checkout the starter bot:

1. Write Dialogs

Checkout the FlowScript for a high-level approach to writing dynamic conversations.

// maindialog.js
import {Dialog} from 'deepdialog';

export const MainDialog = new Dialog({
  name: 'MainDialog',
});

// When the dialog starts:
MainDialog.onStart(async function (session, localVars) {
  await session.send("I'm a bot, what can I help you with?");
});

// When the dialog receives text:
MainDialog.onText(/hello.*/, async function (session, text) {
  // respond to the hello message
  await session.send("Hello to you!");
});

2. Create the App

And add the dialogs:

import {MainDialog} from './maindialog';

var app = new App({
  appId: process.env.DEEPDIALOG_APPID,
  appSecret: process.env.DEEPDIALOG_APPSECRET,
  hostURL: process.env.HOST_URL,
  mainDialog: 'MainDialog', // point at the starting dialog
  deepDialogServer: process.env.DEEPDIALOG_SERVER_URL,
  automaticTypingState: true
});

app.addDialogs(MainDialog);

3. Start the App Server and save the state

app.server.start(process.env.PORT, async function () {
  log.info('Bot started');
  await app.save();
});