text: var restify = require('restify');
  var builder = require('botbuilder');
  var spellCheck = require('./spell-checker.js');
  
  //=========================================================
  // Bot Setup
  //=========================================================
  
  // Setup Restify Server
  var server = restify.createServer();
  server.listen(process.env.port || process.env.PORT || 3978, function () {
     console.log('%s listening to %s', server.name, server.url); 
  });
    
  // Create chat bot
  var connector = new builder.ChatConnector({
      appId: process.env.MICROSOFT_APP_ID,
      appPassword: process.env.MICROSOFT_APP_PASSWORD
  });
  var bot = new builder.UniversalBot(connector);
  server.post('/api/messages', connector.listen());
  
  //=========================================================
  // Activity Events
  //=========================================================
  
  bot.on('contactRelationUpdate', function (message) {
      if (message.action === 'add') {
          var name = message.user ? message.user.name : null;
          var reply = new builder.Message()
                  .address(message.address)
                  .text("Hello %s... Thanks for adding me. Say 'hello' to see more information.", name || 'there');
          bot.send(reply);
      } else {
          // delete their data
      }
  });
  
  bot.on('deleteUserData', function (message) {
      // User asked to delete their data
  });
  
  //=========================================================
  // Bots Global Actions
  //=========================================================
  
  bot.beginDialogAction('help', '/help', { matches: /^help/i });
  
  // Install First Run middleware and dialog
  bot.use(builder.Middleware.firstRun({ version: 1.0, dialogId: '*:/firstRun' }));
  
  bot.dialog('/', [
      function (session) {
          spellCheck.check(session.message.text, function(respond) {
              session.send(respond);
          })
      }
  ]);
  
  //=========================================================
  // Bots Dialogs
  //=========================================================
  
  bot.dialog('/firstRun', [
      function (session) {
          // Send a greeting and show help.
          var card = new builder.HeroCard(session)
              .title("Spell/Grammer Checker Bot")
              .text("Powered by Bing")
              .images([
                   builder.CardImage.create(session, "http://docs.botframework.com/images/demo_bot_image.png")
              ]);
          var msg = new builder.Message(session).attachments([card]);
          session.send(msg);
          session.beginDialog('/help');
      }
  ]);
  
  bot.dialog('/help', [
      function (session) {
          session.endDialog("Type a word or a parageraph and press send. The bot will reply with corrections.");
      }
  ]);
  