> For the complete documentation index, see [llms.txt](https://manuals.dialox.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://manuals.dialox.ai/advanced-topics/shomi-custom/custom-actions.md). Agents can also search and read this documentation through its [MCP server](https://manuals.dialox.ai/~gitbook/mcp).

# Custom actions

A custom action is a button that you add to the Shomi chat of a call. When a user of the Enreach Contact desktop app presses it, Shomi runs a task in your Shomi Custom bot, for example to create a ticket in your helpdesk or a follow-up task in your CRM. The task returns a short message, which Shomi shows in the chat.

You build actions in the same Shomi Custom bot that post-processes the transcripts, see [Shomi Custom](https://manuals.dialox.ai/advanced-topics/shomi-custom.md).

### How it works

1. After a call is transcribed, Shomi runs the `custom_transcript` task in your Shomi Custom bot. There you offer one button per action with `add_shomi_action`.
2. The user opens the call in Shomi and sees your buttons in the chat.
3. When the user presses a button, Shomi runs the action task you chose in your Shomi Custom bot. The task gets the call's transcript.
4. The task returns a status, a message and optionally a link. Shomi shows the message to the user.

### Prerequisites

* A Shomi Custom bot, ordered as described on the [Shomi Custom](https://manuals.dialox.ai/advanced-topics/shomi-custom.md) page, with the REST API channel enabled.
* Version 1.6.0 or later of the Shomi Custom skill. Update the Smart App if your bot runs an older version.
* Knowledge of [BubbleScript](https://developer.dialox.ai/).

There is nothing to configure in Shomi itself: it shows the buttons that a call's transcript contains.

### Build your first action

#### 1. Declare the action task

An action is a task that is declared with `group: "shomi_action"`. The `label` is the default text of the button.

```ruby
declare task create_example_ticket, group: "shomi_action", label: "Create ticket",
  in: @schemas.shomi_action_request,
  out: @schemas.shomi_action_result,
  description: "Example action: pretends to create a ticket for this call"
```

The `in` and `out` schemas come with the Shomi Custom skill. You don't need `rest_api: true`.

#### 2. Write what the action does

The task receives the request as `_args` and returns a result.

```ruby
task create_example_ticket do
  _priority = _args.params.priority || "normal"
  # a real action calls the ticket system here; key it on transcript.conversation_uuid + _args.id
  # so that a second press doesn't create a second ticket
  _message = "Example ticket created (#{_priority})"
  if _args.locale == "nl", do: _message = "Voorbeeldticket aangemaakt (#{_priority})"
  return %{"status" => "done", "message" => _message}
end
```

#### 3. Offer the action as a button

In your `custom_transcript` task, call `add_shomi_action` once for every button. One task can back several buttons, for example with a different `params` map.

```ruby
task custom_transcript do
  perform add_shomi_action, with: %{"id" => "ticket", "task" => "create_example_ticket", "params" => %{"priority" => "normal"}}
  perform add_shomi_action, with: %{"id" => "ticket_urgent", "task" => "create_example_ticket", "label" => %{"$i18n" => true, "en" => "Create urgent ticket", "nl" => "Urgent ticket aanmaken"}, "params" => %{"priority" => "urgent"}}
end
```

#### 4. Publish the bot

{% hint style="warning" %}
Actions run in the **published** version of your Shomi Custom bot. Publish after every change to an action, otherwise Shomi keeps running the previous version.
{% endhint %}

#### 5. Test it

Use the DialoX test runner. The Shomi Custom bot ships with `tests/actions.test.b`, which shows how to test both the buttons and the action tasks:

```ruby
test "process_transcript offers the buttons" do
  transcript = perform process_transcript, with: @samples_example
  assert pluck(transcript.custom_actions, "id") == ["ticket", "ticket_urgent"]
end

test "the action returns its result" do
  _r = perform run_shomi_action, with: %{"id" => "ticket_urgent", "task" => "create_example_ticket", "params" => %{"priority" => "urgent"}, "transcript" => @samples_example, "locale" => "en"}
  assert _r.status == "done"
  assert _r.message == "Example ticket created (urgent)"
end
```

`run_shomi_action` is the task Shomi calls when a button is pressed, so this test runs the action the same way Shomi does.

### Reference

#### add\_shomi\_action

| Key          | Description                                                                                                                                                                                     |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `task`       | The action task (required). It must be declared with `group: "shomi_action"`, otherwise no button is added and a message is logged.                                                             |
| `id`         | Unique per call. Defaults to the task name. Adding the same `id` again replaces the earlier button.                                                                                             |
| `label`      | The button text: a string, or an `$i18n` map such as `%{"$i18n" => true, "en" => "Create lead", "nl" => "Lead aanmaken"}`. Defaults to the `label` of the declared task, then to the task name. |
| `params`     | Any map. It comes back unchanged in `_args.params` when the button is pressed.                                                                                                                  |
| `repeatable` | `true` keeps the button after the action is done. By default the button is hidden once the action returned `done`.                                                                              |

#### What the action task receives

The task gets `_args` with these keys:

| Key          | Description                                                                                                                                                                                                                                         |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`         | The `id` of the button.                                                                                                                                                                                                                             |
| `task`       | The name of the action task.                                                                                                                                                                                                                        |
| `params`     | The `params` you added to the button.                                                                                                                                                                                                               |
| `transcript` | The transcript as the Shomi chat has it, including edits made by the user. It is also available as the `transcript` variable. See the [data structure](https://manuals.dialox.ai/advanced-topics/shomi-custom.md#data-structure) of the transcript. |
| `locale`     | The language of the user, for example `nl`.                                                                                                                                                                                                         |

#### What the action task returns

| Key       | Description                                                                               |
| --------- | ----------------------------------------------------------------------------------------- |
| `status`  | `done` (the default), `failed` or `unavailable`. See below.                               |
| `message` | Shown to the user as is. Translate it yourself, using `_args.locale`.                     |
| `url`     | Optional link to what the action created. Shomi adds it to the message as an "Open" link. |

| Status        | What the user sees                    | The button                                |
| ------------- | ------------------------------------- | ----------------------------------------- |
| `done`        | Your message, or "Done."              | Hidden, unless the button is `repeatable` |
| `failed`      | Your message, or a general apology    | Stays, so the user can try again          |
| `unavailable` | "This action is no longer available." | Hidden                                    |

{% hint style="info" %}
An action that returns nothing counts as `done`. A bare `return` does not: it doesn't match the `shomi_action_result` schema and the user gets an error.
{% endhint %}

### Calling another system

Most actions call an external system. This example creates a ticket in a helpdesk. It sends an `external_id` that is the same for a second press of the same button, so a helpdesk that honours it creates one ticket, and it returns `failed` when the helpdesk doesn't answer, so the user can try again.

```ruby
declare task create_ticket, group: "shomi_action", label: "Create ticket",
  in: @schemas.shomi_action_request,
  out: @schemas.shomi_action_result,
  description: "Creates a ticket in the helpdesk for this call"

task create_ticket do
  _transcript = _args.transcript
  _priority = _args.params.priority || "normal"
  _ticket = %{
    "title" => _transcript.topic,
    "description" => _transcript.summary,
    "priority" => _priority,
    "caller" => _transcript.caller_number,
    "external_id" => "#{_transcript.conversation_uuid}-#{_args.id}"
  }
  _headers = ["Authorization": "Bearer #{get_access_token(bot.tokens.helpdesk)}"]
  _result = http_post("https://helpdesk.example.com/api/tickets", headers: _headers, json: _ticket, decoder: :json, timeout: 20)

  if _result.error || _result.status_code >= 300 do
    log "create_ticket failed: #{_result.reason} #{_result.status_code}"
    _message = "The ticket could not be created. Please try again."
    if _args.locale == "nl", do: _message = "Het ticket kon niet worden aangemaakt. Probeer het opnieuw."
    return %{"status" => "failed", "message" => _message}
  end

  _created = _result.body
  _message = "Ticket #{_created.id} created"
  if _args.locale == "nl", do: _message = "Ticket #{_created.id} aangemaakt"
  return %{"status" => "done", "message" => _message, "url" => _created.url}
end
```

The URL and the fields are placeholders for your own system. Keep the API key in an integration, see the [developer documentation](https://developer.dialox.ai/) on integrations and `get_access_token`.

### Good to know

* **Pressing twice.** A user can press a button a second time before the first answer arrives. Make actions that create something idempotent, for example by keying them on `transcript.conversation_uuid` and `_args.id`, as above.
* **The full transcript.** `_args.transcript` contains the call text (`text`, `caller_text`) and the `audio_url`, whatever the setting "Allow full transcript to be pushed to email and CRM" says. That setting is only checked by Shomi's own email and CRM pushes, not by your actions. An action that sends data to another system should check `transcript.allow_push_full_transcript` before it includes the call text:

  ```ruby
  _note = _transcript.summary
  if _transcript.allow_push_full_transcript == true, do: _note = _transcript.text
  ```
* **Languages.** Shomi shows the label in the language of the user and falls back to English. The message is not translated for you: use `_args.locale`.
* **Typing instead of pressing.** A user can also type the label of a button. Capitals don't matter. A button that is done or unavailable can't be run this way either.
* **Choose distinctive labels.** A button whose label is the same as one of Shomi's own buttons is skipped.
* **Number of buttons.** There is no fixed limit. Every button takes room in the chat, so keep the list short.
* **Reserved names.** The group `shomi_action` and the schemas `shomi_action_request` and `shomi_action_result` belong to the Shomi Custom skill. Don't define the schemas in your own `schemas.yml`: the import fails with "Duplicate schema definition".

### How long a conversation stays open

Shomi talks to your Shomi Custom bot over the REST API. One conversation per call processes the transcript, and the actions of that call run in the same conversation. Since version 1.6.0 of the skill such a conversation is closed after **24 hours** without activity. It used to be 5 minutes.

* The conversation stays open in your inbox for up to a day after the call. Every button press starts the 24 hours again.
* A press after the conversation closed opens a new conversation for the call. Shomi does that by itself.
* Closing is not deleting: a closed conversation keeps its data.
* **Send the transcript to another system with the `push_transcript` webhook**, or with a custom webhook task that `custom_transcript` performs. See [How to integrate](https://manuals.dialox.ai/advanced-topics/shomi-custom.md#how-to-integrate). An integration on the standard `conversation_close` webhook now receives the transcript up to a day after the call, because that webhook fires when the conversation closes.
* The 24 hours apply to every conversation of the bot, including Studio previews. To use another value, define `@timeout` in the main script of your own bot.

### Troubleshooting

| What you see                                       | Likely cause                                                                                                                                                              |
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The button doesn't appear                          | `custom_transcript` didn't add it, or the task isn't declared with `group: "shomi_action"` (a message is logged), or the Shomi custom add-on isn't ordered for that user. |
| The button doesn't do what you changed             | The bot isn't published. Actions run in the published version.                                                                                                            |
| "Sorry, that didn't work. Please try again later." | The action couldn't run or raised an error, for instance because it returned a bare `return`. Open the conversation of that call in the Inbox of your Shomi Custom bot.   |
| "This action is no longer available."              | The task isn't declared with `group: "shomi_action"` any more, or returned `unavailable`.                                                                                 |
| The button is gone after pressing                  | The action returned `done`. Add `"repeatable" => true` to keep it.                                                                                                        |
