Tiledesk Docs
Search…
Send Text Buttons

Tiledesk External Chatbot - Send Buttons

The Tiledesk native Widget supports many special messages that go beyond simple text sent back to the user.
A chatbot can also reply with images, videos, maps, buttons or even with a message embedding an entire interactive app.
External chatbots can benefit of all of these features, and sending a rich message to the client is very easy. You can use two ways to send rich messages:
  1. 1.
    The Widget's native JSON Protocol
  2. 2.
    or the Tiledesk chatbot's Microlanguage
In this tutorial we'll send back a message with a couple of text buttons to the widget
image

Widget native protocol

The Widget native protocol is a special JSON format that you can use to specify the message content, type (ex. 'image' or 'text'), attachments or other features.
In the next example we'll specify how to send a couple of text buttons to the Widget to let the user choose between a couple of options. If you want to know more about text buttons and why they are useful in chatbot programming you can read Advanced chatbot styling: buttons in the Tiledesk's Knowledge Base.
We'll use the code that you can find in the Widget JSON protocol, at the paragraph Text message with reply buttons
You can find the full index.js code of the Node.js application on the repl.it project for this tutorial:
1
const express = require('express');
2
const bodyParser = require('body-parser');
3
const { TiledeskChatbotClient } = require('@tiledesk/tiledesk-chatbot-client');
4
5
const app = express();
6
app.use(bodyParser.json());
7
8
app.get('/', (req, res) => {
9
res.send("Tiledesk External-Chatbot Buttons example");
10
});
11
12
app.post('/bot', (req, res) => {
13
res.status(200).send({"success":true});
14
const tdclient =
15
new TiledeskChatbotClient(
16
{
17
request: req,
18
APIKEY: '__APIKEY__'
19
});
20
if (tdclient.text === "\\start") {
21
tdclient.sendMessage(
22
{
23
text: 'Do yiou agree?',
24
attributes: {
25
attachment: {
26
type:"template",
27
buttons: [
28
{
29
type: "text",
30
value: "Yes, I do"
31
},
32
{
33
type: "text",
34
value: "No, I don't"
35
}
36
]
37
}
38
}
39
}
40
);
41
}
42
else {
43
tdclient.sendMessage({text: 'Not trained for this'});
44
}
45
});
46
47
app.listen(3000, () => {
48
console.log('server started');
49
});
Copied!

Microlanguage

Microlanguage is a special tag-based-language, specifically designed to allow chatbot developers to use advanced chatbot features without programming skills.
We just added another endpoint in the same index.js that you can use to understand how to use microlanguage to send buttons back to the widget.
1
app.post('/bot-micro', (req, res) => {
2
res.status(200).send({"success":true});
3
const tdclient =
4
new TiledeskChatbotClient(
5
{
6
request: req,
7
APIKEY: '__APIKEY__'
8
});
9
if (tdclient.text === "\\start") {
10
tdclient.sendMessage(
11
{
12
text: "Do you agree?\n* Yes, I do\n* No, I don't",
13
attributes: {
14
microlanguage: true
15
}
16
}
17
);
18
}
19
else {
20
tdclient.sendMessage({text: 'Not trained for this'});
21
}
22
});
Copied!
As you can see you just have to add the buttons with the same synthax you use for lists in markdown. The microlanguage automatically converts the items in buttons.
Last modified 5d ago