Actions¶
Hardcoded actions¶
greeting:
states:
- name: "root"
action:
text: "How are you?" # sends a text message
replies: # sends quick replies below the message
- "I'm fine!"
- "Don't even ask"
next: "next.root:" # moves to a different state
Coding actions in Python¶
You can also define the action as a regular Python function. In this function, you can implement business logic and send messages from the bot.
greeting:
states:
- name: "root"
# relative import ...
action: actions.foo
# ... or absolute import
action: chatbot.bots.default.conditions.bar
dialog
. This object can be used to send messages and query conversation context and user profile.next
in YAML).import random
from custom.logic import get_all_jokes
from botshot.core.dialog import Dialog
from botshot.core.responses import *
def show_joke(dialog: Dialog):
jokes = get_all_jokes()
joke = random.choice(jokes)
dialog.send(joke) # sends a string message
return "next.root:"
dialog.send()
. This method takes one argument - the message!botshot.core.responses
, or a list of messages.dialog.context
. See `Context`_ for more information.Note
The function will be run asynchronously. While waiting for the function to return, a typing indicator will be displayed. (three dots in messenger)
Sending more messages at once¶
messages = []
for i in range(3):
messages.append("Message #{}".format(i))
dialog.send(messages)
Warning
Avoid calling dialog.send() in a loop. In bad network conditions, the messages might be sent in wrong order.
Sending delayed messages¶
You can schedule messages to be sent at a time in the future, or when the user is inactive for a period of time. Read more in `Scheduling messages`_.
payload = {"_state": "default.schedule", "schedule_id": "123"}
# Regular scheduled message - use a datetime or number of seconds
dialog.schedule(payload, at=None, seconds=None)
# Executed only if the user remains inactive
dialog.inactive(payload, seconds=None)
Amazing, you’re now ready to implement your first action. But wait, the chatbot does not yet understand anything the user said, what about that? Continue reading and you’ll find out in Natural language processing.
Message templates¶
This section contains a list of all message templates that can be sent using dialog.send()
.
The templates are universal, but they will render slightly different on each messaging service.
Text message¶
TextMessage(text, buttons=[], replies=[])
Buttons are shown below the message. They can be used to provide additional related actions.
LinkButton(title, url)
- Opens a web page upon being clicked.PayloadButton(title, payload)
- Sends a special `postback message`_ with programmer-defined payload on click.PhoneButton(title, phone_number)
- Facebook only. Calls a number when clicked.ShareButton()
- Facebook only. Opens the “Share” window.
Quick replies are used to suggest a user’s response. They contain text that is sent back to the chatbot when clicked. They can also contain payload.
QuickReply(title)
- Used to suggest what the user can say. Sends “title” as a message.LocationQuickReply()
- Facebook only. Opens the “Send location” window.
msg = "Botshot is the best!"
dialog.send(msg)
msg = TextMessage("I'm a message with buttons!")
msg.add_button(LinkButton("I'm a button", "https://example.com"))
msg.add_button(PayloadButton("Next page", payload={"_state": "next.root:"}))
msg.add_button(ShareButton())
# or ...
msg.with_buttons(button_list)
dialog.send(msg)
msg = TextMessage("I'm a message with quick replies!")
# or ...
msg.add_reply(LocationQuickReply())
msg.add_reply(QuickReply("Lorem ipsum ..."))
msg.add_reply("dolor sit amet ...")
# or ...
msg.with_replies(reply_list)
Note
Different platforms have different message limitations. For example, quick replies in Facebook Messenger can have a maximum of 20 characters.
Media message¶
MediaMessage can be used to send an image with optional buttons. The image should be located on a publicly available URL.
Example:
from botshot.core.responses import MediaMessage
message = MediaMessage(url="http://placehold.it/300x300", buttons=[LinkButton("Open", "http://example.com"))
renders as:
Card template¶
The card template displays a card with a title, subtitle, an image and buttons. It can be used to present structured information about an item or product.
msg = CardTemplate(
title="A card",
subtitle="Hello world!",
image_url="http://placehold.it/300x300",
item_url="http://example.com"
)
msg.add_button(button)
Carousel template¶
The carousel template is a horizontal list of cards.
msg = CarouselTemplate()
msg.add_element(
CardTemplate(
title="Card 1",
subtitle="Hello world!",
image_url="http://placehold.it/300x300"
)
)
List template¶
The list template is a vertical list of cards.