- Notifications
You must be signed in to change notification settings - Fork2
Tock client to build conversational stories using Nodejs.
License
theopenconversationkit/tock-node
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A Nodejs framework to write stories in JS for aTock chatbot.
🏠 Home:https://doc.tock.ai
💬 Contact:https://gitter.im/tockchat/Lobby
- Run aTock bot in API mode
- Create a Bot application using theweb connector type in Tock Studio and get your API key
yarn add tock-nodenpm i tock-nodeThe package has TypeScript type definitions
const{ Bot}=require('tock-node');constbot=newBot('<TOCK_API_KEY>','<TOCK_HOST>',<TOCK_WS_PORT>, '<TOCK_WS_PROTOCOL>');bot.addStory('intent', bot =>{bot.send('Hello World!');});
Default Tock platform host, port and WebSocket protocol aredemo-bot.tock.ai (the public demo),443 andwss (secured port/protocol).
For more examples, seetock-node-example.
You can callsend as many times as you need. All messages will be sent within the same response.
bot.addStory('intent',bot=>{bot.send('Good morning!');bot.send('How are you?');});
You can use send to send other message templates such as cards.
constimageCard=require('tock-node').imageCard;constaction=require('tock-node').action;bot.send(imageCard('Card title','Card description','https://site/image.png',action('Button')));
There is also a template that lets you send multiple cards in a carousel.
constimageCard=require('tock-node').imageCard;constaction=require('tock-node').action;bot.send({cards:[imageCard('Card 1','','https://site/image.png'),imageCard('Card 2','','https://site/image.png'),imageCard('Card 3','','https://site/image.png'),],});
Quick replies are highly buttons that are following a message. You can send quick replies following a message usingsend.
constsuggestion=require('tock-node').suggestion;bot.send('Check this out!',suggestion('View Tock on GitHub'),suggestion("View Tock's Website"));
You can get the original user request (containing his message) from the second argument of the story handler. This is also how you retrieve entities found by Tock.
bot.addStory('intent',(bot,request)=>{// user messageconsole.log(request.message.text);// entitiesconsole.log(request.entities);});
Each user has a user context that is persistent across all stories. You can use it and also set it.
bot.addStory('intent',bot=>{// getting user contextconsole.log(bot.userData);// settingbot.dispatchUserData({firstName:'Tockito'});});
In the case of multiple dispatches you can retrieve thecurrent user data by using a callback function that uses thecurrent user data as an argument.
bot.addStory('intent',bot=>{bot.dispatchUserData({firstName:'Foo'});bot.dispatchUserData(userData=>{console.log(userData);// {"firstname":"Foo"}return{firstName:userData.firstName,lastName:'Bar'};});});
If you'd like to wait for an action before sending you can use aPromise as a callback to your story handler.
bot.addStory('intent',asyncbot=>{constmessage=awaitfetchingData();bot.send(message);});
You can add as many story handlers as you want in the sameaddStory.
consthandler1=bot=>send('First handler');consthandler2=bot=>send('Second handler');bot.addStory('intentA',handler1,handler2);bot.addStory('intentB',handler1,handler2);
About
Tock client to build conversational stories using Nodejs.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.