npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tock-node

v24.9.1

Published

Nodejs framework to create stories for Tock chatbots

Downloads

32

Readme

Dependencies

Latest release Release Date NPM Downloads

Gitter Contributors Commit Activity

Home Demo License

Tock Node

A Nodejs framework to write stories in JS for a Tock chatbot.

🏠 Home: https://doc.tock.ai

💬 Contact: https://gitter.im/tockchat/Lobby

Prerequisites

  • Run a Tock bot in API mode
  • Create a Bot application using the web connector type in Tock Studio and get your API key

Installation

yarn add tock-node
npm i tock-node

The package has TypeScript type definitions

Usage

const { Bot } = require('tock-node');

const bot = new Bot('<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 are demo-bot.tock.ai (the public demo), 443 and wss (secured port/protocol).

For more examples, see tock-node-example.

Sending messages

You can call send 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?');
});

Sending cards

You can use send to send other message templates such as cards.

const imageCard = require('tock-node').imageCard;
const action = require('tock-node').action;

bot.send(imageCard('Card title', 'Card description', 'https://site/image.png', action('Button')));

Sending carousels

There is also a template that lets you send multiple cards in a carousel.

const imageCard = require('tock-node').imageCard;
const action = 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

Quick replies are highly buttons that are following a message. You can send quick replies following a message using send.

const suggestion = require('tock-node').suggestion;

bot.send('Check this out!', suggestion('View Tock on GitHub'), suggestion("View Tock's Website"));

Get entities and using the original user request

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 message
  console.log(request.message.text);
  // entities
  console.log(request.entities);
});

Manage user data/context

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 context
  console.log(bot.userData);
  // setting
  bot.dispatchUserData({ firstName: 'Tockito' });
});

In the case of multiple dispatches you can retrieve the current user data by using a callback function that uses the current 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' };
  });
});

Asynchronous stories

If you'd like to wait for an action before sending you can use a Promise as a callback to your story handler.

bot.addStory('intent', async bot => {
  const message = await fetchingData();
  bot.send(message);
});

Chaining/reusable story handlers

You can add as many story handlers as you want in the same addStory.

const handler1 = bot => send('First handler');
const handler2 = bot => send('Second handler');

bot.addStory('intentA', handler1, handler2);
bot.addStory('intentB', handler1, handler2);