How to create a Typetalk bot that responds to keywords
typetalk
February 01, 2018
In any business, there are lots of little bits of information employees might need. From extension numbers to office hours, wouldn’t it be great to retrieve that information by simply asking your chat app?
With Typetalk, you can do just that by creating a bot that responds to particular keywords. We will walk you through our own example and show you how to create a bot that provides the extension number of a department.
How to create a keyword bot for Typetalk
1. Create Google Apps Script
First, you’ll need to create a Google Apps Script, and run a test to make sure the API call succeeds.
In your Google Drive, create a new Google Apps Script.
You can copy our code and replace the API Key with your own, or create your own code from scratch. Once you’ve finished, save the script.
function createReplyMessage(message) { //Change it to your number! var extentsion_number = ‘5566’; var rtn = message.match(/extension/i)? 'Hi! The Extension Number of the General Affairs Department is ' + extentsion_number : null; return rtn; } function testCurrent() { Logger.log(createReplyMessage("What's the extension number?")); }
Select the “testCurrent” function in your drop-down menu, and click the triangle button to test the code.
After running the test, you can see a log by selecting View > Logs from the drop-down menu.
If the API call succeeded, you should see logs like the following.
2. Deploy your script
Add Typetalk and a doPost function in the code to receive a webhook from Typetalk.
var Typetalk = { getMessage: function (e) { var data = e.postData && JSON.parse(e.postData.getDataAsString()); return data && data.post; }, composeReplyMessage: function (message, postId) { var response = { "message" : message, "replyTo" : postId }; return ContentService.createTextOutput(JSON.stringify(response)) .setMimeType(ContentService.MimeType.JSON); } } function doPost(e) { try { var post = Typetalk.getMessage(e); if (post) { var message = createReplyMessage(post.message); if (message) { return Typetalk.composeReplyMessage(message, post.id); } } } catch(ex) { return Typetalk.composeReplyMessage(ex.toString(), null); } }
Then “Deploy as web app” from the Publish section of the drop-down menu.
In the pop-up, select “Me” for “Execute the app as” and select “Anyone, even anonymous” for “Who has access to the app”.
Then copy the web app URL in the dialog.
3. Create your bot
In the Typetalk web app, click the Topic Settings icon and select the “BOTS” tab. Click the “Add New” button, and then create an ID and Full Name. Finally, check “Use Webhook”, and paste the URL that you copied in the previous step before clicking the “Create” button to save.
Finally, post a message like “What is the extension?” in the topic. Your new bot will reply with the answer!
You can code your bot to respond to all kinds of keywords. Bots make it easy (and fun!) for your team members to retrieve useful information.