Living The Life

Living The Life

Monday, May 29, 2017

Playing With Salesforce Bot Toolkit

On the Salesforce developer blog, they had a neat post: A Toolkit for Creating and Deploying Bots Inside Salesforce.  It is only a simple regular expression based bot, and I'm sure there are other Bot APIs out there, but the post seemed pretty inviting and looked fun, so I decided to take it for a spin.  It was every bit as fun as I thought.  I may port Zork over to this ☺

First installing and setting it up was pretty easy, just follow the post about it.  Works just like they described.  I think I fall in love when I tried out the select feature.  I could imagine all sorts of neat developer utilities put into that like run test and search the audit trail for who broke my stuff :)

Of course, you don't truly understand something until you hack at it, so I created a few simple Bot Handler.  Just look over the Handler apex classes for examples.  Of course, before I set off to re-create Zork, I did a simple one to create a task.  I did it two different ways to play with the framework.




New Task Bot Command - Using Session

First, create a Bot Command entry in the object.  Tip: Maybe just clone a pre-existing row to help get you going.
  • Command Name: New Task - or whatever you want
  • Invocation Pattern: (?i)new task
  • Apex Class: HandlerNewTask - source follows
  • Sample Utterance: new task
Apex Class HandlerNewTask
public with sharing class HandlerNewTask  implements BotHandler {
    
    
    public BotResponse handle(String utterance, String[] params, Map<String, String> session) {
        if (session == null) {
            session = new Map<String, String>();
            session.put('nextCommand', 'HandlerNewTask');
            session.put('step', 'askSubject');
            return new BotResponse(new BotMessage('Bot', 'What\'s the task for?'), session);
        }
        
        String step = session.get('step');
        if (step == 'askSubject') {
            session.put('subject', utterance);
            session.put('nextCommand', 'HandlerNewTask');
            session.put('step', 'newTask');
            return new BotResponse(new BotMessage('Bot', 'When is the task due?'), session);
        } else {
        String subject = session.get('subject');
        Date dueDate = null;
       
        if('today'==utterance) {
        dueDate = Date.today();
        }
        else if('tomorrow'==utterance) {
        dueDate = Date.today()+1;
        }
        else {
        dueDate = Date.parse(utterance);
        }
       
        Task newTask = new Task(
        Subject = subject,
        ActivityDate = dueDate);
        insert newTask;
       
        List<BotRecord> records = new List<BotRecord>();
       
        List<BotField> fields = new List<BotField>();
            fields.add(new BotField('Subject', newTask.Subject, '#/sObject/' + newTask.Id + '/view'));
            records.add(new BotRecord(fields));
       
        return new BotResponse(
        new BotMessage(
        'Bot',
        'Created Task: ',
        records));
        }
        
    }
    
    
}

New Task Bot Command - Using Parameters

First, create a Bot Command entry in the object.  Tip: Maybe just clone a pre-existing row to help get you going.
  • Command Name: New Task All
  • Invocation Pattern: (?i)create task ([A-Za-z0-9 ].*), due ([A-Za-z0-9/].*)
  • Apex Class: HandlerNewTaskAllAtOnce
  • Sample Utterance: create task shopping for fireworks, due 7/4/17
Apex Class HandlerNewTaskAllAtOnce
public with sharing class HandlerNewTaskAllAtOnce  implements BotHandler {
    
    
    public BotResponse handle(String utterance, String[] params, Map<String, String> session) {
   
        String subject = params[0];
        Date dueDate = null;
       
        if('today'==params[1]) {
        dueDate = Date.today();
        }
        else if('tomorrow'==params[1]) {
        dueDate = Date.today()+1;
        }
        else {
        dueDate = Date.parse(params[1]);
        }
       
        Task newTask = new Task(
        Subject = subject,
        ActivityDate = dueDate);
        insert newTask;
       
        List<BotRecord> records = new List<BotRecord>();
       
        List<BotField> fields = new List<BotField>();
            fields.add(new BotField('Subject', newTask.Subject, '#/sObject/' + newTask.Id + '/view'));
            records.add(new BotRecord(fields));
       
        return new BotResponse(
        new BotMessage(
        'Bot',
        'Created Task: ',
        records));
        
    }
    
    
}

No comments:

Post a Comment