text: /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             ______     ______     ______   __  __     __     ______
            /\  == \   /\  __ \   /\__  _\ /\ \/ /    /\ \   /\__  _\
            \ \  __<   \ \ \/\ \  \/_/\ \/ \ \  _"-.  \ \ \  \/_/\ \/
             \ \_____\  \ \_____\    \ \_\  \ \_\ \_\  \ \_\    \ \_\
              \/_____/   \/_____/     \/_/   \/_/\/_/   \/_/     \/_/
  
  
  This is a sample Slack bot built with Botkit.
  
  This bot demonstrates many of the core features of Botkit:
  
  * Connect to Slack using the real time API
  * Receive messages based on "spoken" patterns
  * Reply to messages
  * Use the conversation system to ask questions
  * Use the built in storage system to store and retrieve information
    for a user.
  
  # RUN THE BOT:
  
    Get a Bot token from Slack:
  
      -> http://my.slack.com/services/new/bot
  
    Run your bot from the command line:
  
      token=<MY TOKEN> node slack_bot.js
  
  # USE THE BOT:
  
    Find your bot inside Slack to send it a direct message.
  
    Say: "Hello"
  
    The bot will reply "Hello!"
  
    Say: "who are you?"
  
    The bot will tell you its name, where it running, and for how long.
  
    Say: "Call me <nickname>"
  
    Tell the bot your nickname. Now you are friends.
  
    Say: "who am I?"
  
    The bot will tell you your nickname, if it knows one for you.
  
    Say: "shutdown"
  
    The bot will ask if you are sure, and then shut itself down.
  
    Make sure to invite your bot into other channels using /invite @<my bot>!
  
  # EXTEND THE BOT:
  
    Botkit has many features for building cool and useful bots!
  
    Read all about it here:
  
      -> http://howdy.ai/botkit
  
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  
  
  if (!process.env.token) {
      console.log('Error: Specify token in environment');
      process.exit(1);
  }
  
  var Botkit = require('./lib/Botkit.js');
  var os = require('os');
  
  var controller = Botkit.slackbot({
      debug: true,
  });
  
  var bot = controller.spawn({
      token: process.env.token
  }).startRTM();
  
  var irene_messages = ["Hey hun, what are you having?", "You and your friends here to party tonight?", "Oh god if I hear that dogs song one more time the jukebox is gone!", "ATM is broken hun, try the Bank of America.", "No more darts, the college kiddies are out tonight."];
  
  controller.hears(['hello irene', 'hi irene'], 'direct_message,direct_mention,mention', function(bot, message) {
  
      bot.api.reactions.add({
          timestamp: message.ts,
          channel: message.channel,
          name: 'beer',
      }, function(err, res) {
          if (err) {
              bot.botkit.log('Failed to add emoji reaction :(', err);
          }
      });
  
      var which_message = Math.floor((Math.random() * 3));
      bot.reply(message, irene_messages[which_message]);
  });
  
  controller.hears(['can i please have a (.*)'], 'direct_mention,mention', function(bot, message) {
      
      var order = message.match[1];
      var drinks = ["PBR", "Gansett", "Pitcher of PBR", "Pitcher of Gansett", "Jameson", "Old Thompson", "Tanqueray", "Smirnoff", "Mr Boston"];
  
      if (drinks.indexOf(order) > -1) {
          bot.reply(message, 'You got it, one ' + order + ' coming up.');
          bot.reply(message, 'If you are having dinner tonight let me know.');
      } else {
          bot.reply(message, 'You know what we have here, but in case you are loopy here is the list again:');
          for (i = 0; i < drinks.length; i++) {
              bot.reply(message, drinks[i]);
          }
      }    
  });
       
  controller.hears(['can i please have some dinner'], 'direct_mention,mention', function(bot, message) {
  
      var popcorn = ['http://i.imgur.com/RnX6jNp.jpg', 'http://i.imgur.com/iJSBhrT.jpg', 'http://i.imgur.com/8L9dTru.jpg', 'http://i.imgur.com/9JSe3Rz.jpg', 'http://i.imgur.com/kO1xJUb.jpg'];
  
      bot.reply(message, 'Sure hun, one second.');
      bot.reply(message, 'Here ya go.');
      var which_popcorn = Math.floor((Math.random() * 4));
      bot.reply(message, popcorn[which_popcorn]);
  });
  
  controller.hears(['what song is currently playing on the jukebox'], 'direct_mention,mention', function(bot, message) {
  
      var songs = ["It's that god damn dogs are out song again!", "Hmmm can't tell, sounds like Metallica or something.", "How am I supposed to know I'm old as hell!", "Sounds like the Beatles to me. Then again, everything sounds like the Beatles to me. Why can't bands these days sound like the Beatles?"];
  
      var which_song = Math.floor((Math.random() * 3));
      bot.reply(message, songs[which_song]);
  });
  
  controller.hears(['help'], 'direct_mention,mention', function(bot, message) {
  
      var options = ["can i please have a <drink name>", "can i please have some dinner", "what song is currently playing on the jukebox"];
  
      bot.reply(message, 'Are you kidding me? You have been here a million times! Are you insane? Here is what we do here ya nut...');
      for (i = 0; i < options.length; i++) {
          bot.reply(message, options[i]);
      }
  });
  
  controller.hears(['call me (.*)', 'my name is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
      var name = message.match[1];
      controller.storage.users.get(message.user, function(err, user) {
          if (!user) {
              user = {
                  id: message.user,
              };
          }
          user.name = name;
          controller.storage.users.save(user, function(err, id) {
              bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
          });
      });
  });
  
  controller.hears(['what is my name', 'who am i'], 'direct_message,direct_mention,mention', function(bot, message) {
  
      controller.storage.users.get(message.user, function(err, user) {
          if (user && user.name) {
              bot.reply(message, 'Your name is ' + user.name);
          } else {
              bot.startConversation(message, function(err, convo) {
                  if (!err) {
                      convo.say('I do not know your name yet!');
                      convo.ask('What should I call you?', function(response, convo) {
                          convo.ask('You want me to call you `' + response.text + '`?', [
                              {
                                  pattern: 'yes',
                                  callback: function(response, convo) {
                                      // since no further messages are queued after this,
                                      // the conversation will end naturally with status == 'completed'
                                      convo.next();
                                  }
                              },
                              {
                                  pattern: 'no',
                                  callback: function(response, convo) {
                                      // stop the conversation. this will cause it to end with status == 'stopped'
                                      convo.stop();
                                  }
                              },
                              {
                                  default: true,
                                  callback: function(response, convo) {
                                      convo.repeat();
                                      convo.next();
                                  }
                              }
                          ]);
  
                          convo.next();
  
                      }, {'key': 'nickname'}); // store the results in a field called nickname
  
                      convo.on('end', function(convo) {
                          if (convo.status == 'completed') {
                              bot.reply(message, 'OK! I will update my dossier...');
  
                              controller.storage.users.get(message.user, function(err, user) {
                                  if (!user) {
                                      user = {
                                          id: message.user,
                                      };
                                  }
                                  user.name = convo.extractResponse('nickname');
                                  controller.storage.users.save(user, function(err, id) {
                                      bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
                                  });
                              });
  
  
  
                          } else {
                              // this happens if the conversation ended prematurely for some reason
                              bot.reply(message, 'OK, nevermind!');
                          }
                      });
                  }
              });
          }
      });
  });
  
  
  controller.hears(['shutdown'], 'direct_message,direct_mention,mention', function(bot, message) {
  
      bot.startConversation(message, function(err, convo) {
  
          convo.ask('Are you sure you want me to shutdown?', [
              {
                  pattern: bot.utterances.yes,
                  callback: function(response, convo) {
                      convo.say('Bye!');
                      convo.next();
                      setTimeout(function() {
                          process.exit();
                      }, 3000);
                  }
              },
          {
              pattern: bot.utterances.no,
              default: true,
              callback: function(response, convo) {
                  convo.say('*Phew!*');
                  convo.next();
              }
          }
          ]);
      });
  });
  
  
  controller.hears(['uptime', 'identify yourself', 'who are you', 'what is your name'],
      'direct_message,direct_mention,mention', function(bot, message) {
  
          var hostname = os.hostname();
          var uptime = formatUptime(process.uptime());
  
          bot.reply(message,
              ':robot_face: I am a bot named <@' + bot.identity.name +
               '>. I have been running for ' + uptime + ' on ' + hostname + '.');
  
      });
  
  function formatUptime(uptime) {
      var unit = 'second';
      if (uptime > 60) {
          uptime = uptime / 60;
          unit = 'minute';
      }
      if (uptime > 60) {
          uptime = uptime / 60;
          unit = 'hour';
      }
      if (uptime != 1) {
          unit = unit + 's';
      }
  
      uptime = uptime + ' ' + unit;
      return uptime;
  }
  