PubNub has added a Gupshup BLOCK on the PubNub BLOCKS catalogue. You can now trigger a bot action via a PubNub BLOCK.
Read this blog post for an introduction to the PubNub Data Stream Network and an overview of BLOCKS.
PubNub has added a BLOCK that enables you to connect your chatbot created on the Gupshup platform, to real-time data streams. Thus you can trigger any action on the bot based on an input.
Lets create a chatbot that echoes the message that the PubNub BLOCK sends to it.
First create a chatbot on the Gupshup platform using the IDE Bot Builder. Read these guides for details on how to do so.
The Gupshup BLOCK uses the HTTP endpoint capability of a bot created on the Gupshup platform. Essentially, each bot created on Gupshup has an endpoint which can be used to either trigger a function within the bot or store data for the bot. For more details about the HTTP endpoint of the bot, read this guide
The helper method that executes when corresponding HTTP endpoint is invoked is the httpResponseHandler() method. In your httpResponseHandler() method, write code to echo what the BLOCK has sent. The method will look like this:
function HttpEndpointHandler(context, event) {
var message = event.params.text;
context.sendResponse("Your message is " + message);
}
Your chatbot will now echo any message that is sent to it. You can test it out by pasting this URL in your browser: https://www.gupshup.io/developer/bot/{botname}/public?text=Hello.
Let us now use the Gupshup BLOCK on PubNub. Sign up for a PubNub account. Navigate to the Gupshup BLOCK page and hit the 'Try it Now' button.
Enter the Name and Description of the BLOCK. Create a new app and a new keyset for this block and hit the 'Get Started' button.
You will see a screen with the Gupshup BLOCK already imported.
To test out this block, scroll down to the Test Payload section in the left menu. Change the botname field to the name of the bot that you have just created on Gupshup and change the text to your custom text.
Hit the 'Start block' button on the top right corner of the screen. This deploys the BLOCK to the data stream. You will see this in the console once the BLOCK is deployed:
12:35:49 (Block Status): Deploying in regions: Eastern US, Western US, Asia, Western Europe, Central Europe, South America.
12:35:50 (Block Status): Block is now globally deployed.
Hit the 'Publish' button under the Test Payload section to publish a payload to the BLOCK. Once the payload is published, the BLOCK is invoked. What the block essentially does is send a POST request (with a payload) to the endpoint of the bot that you have specified. The bot parses the payload and prints out the custom text from the payload.
If it is successful you will see in your console:
12:39:14 (Block Status): Deploying in regions: Eastern US, Western US, Asia, Western Europe, Central Europe, South America.
12:39:15 (Block Status): Block is now globally deployed.
12:39:17 (Publish): { "botname": "testBot", "response-channel": "resp-channel", "text": "My first BLOCK" }
12:39:17 js: "URL is https://www.gupshup.io/developer/bot/testBot/public"
12:39:18 js: "Your message is \"My first BLOCK\""
12:39:18 (Publish): Your message is "My first BLOCK"
That is how you can use the Gupshup BLOCK on PubNub to trigger a bot action. You can use the PubNub API to send data back to the channel on which the BLOCK resides. You can use this BLOCK in a number of different scenarios, such as controlling a smart bulb or any other IoT-enabled home automation device using a chatbot. You can also query real-time dashboards for live and updated cuts of data, and view it using a chatbot. This BLOCK also will enable you to easily add a chatbot channel to your existing application.