# Channels in Rasa

Channels in Rasa are the abstraction that allows you to connect the Rasa Assistant to your desired platform where your users are. If the built-in channels in Rasa do not fit your needs, you can create a custom channel.

A custom channel connector must be implemented as a Python class. When building a custom channel, think of it like a two-way conversation between your desired platform and Rasa. You need:

- **InputChannel**: Receives messages from users on your platform and forwards them to Rasa for processing.

- **OutputChannel**: Takes Rasa's responses and sends them back to users on your platform.

The flow is simple: User sends message → InputChannel receives it → Rasa processes → OutputChannel sends response back to user.

This separation lets you customize how messages come in (webhook, WebSocket, etc.) independently from how responses go out (REST API calls, real-time streaming, etc.).

## InputChannel

A custom connector class must subclass `rasa.core.channels.channel.InputChannel` and implement at least `blueprint` and `name` methods.

### The `name` method

The `name` method defines the url prefix for the connector's webhook. It also defines the channel name you should use in any channel specific response variations and the name you should pass to the `output_channel` query parameter on the trigger intent endpoint.

For example, if your custom channel is named `myio`, you would define the `name` method as:

```python
from rasa.core.channels.channel import InputChannel

class MyIO(InputChannel):
    def name() -> Text:
        """Name of your custom channel."""
        return "myio"
```

You would write a response variation specific to the `myio` channel as:

```yaml
domain.yml
responses:
  utter_greet:
    - text: Hi! I'm the default greeting.
    - text: Hi! I'm the custom channel greeting
      channel: myio
```

The webhook you give to the custom channel to call would be `http://<host>:<port>/webhooks/myio/webhook`, replacing the host and port with the appropriate values from your running Rasa server.

### The `blueprint` method

The `blueprint` method must create a Sanic blueprint that can be attached to a sanic server. Your blueprint should have at least the two routes: `health` on the route `/`, and `receive` on the route `/webhook` (see example custom channel below).

As part of your implementation of the `receive` endpoint, you will need to tell Rasa to handle the user message. You do this by calling

```python
    on_new_message(
      rasa.core.channels.channel.UserMessage(
        text,
        output_channel,
        sender_id
      )
    )
```

Calling `on_new_message` will send the user message to the `handle_message` method.

### Optional InputChannel Methods

You can override these methods for additional functionality:

- **`from_credentials(credentials)`** \- Class method to create channel instance from credentials configuration.

## OutputChannel

The `OutputChannel` class is responsible for sending Rasa's responses back to users on your platform. There are two main options:

1. **Use `CollectingOutputChannel`** \- Collects all bot responses in a list that you can return in your webhook response (good for REST-style channels).
2. **Create your own OutputChannel subclass** \- Implement custom logic for sending responses directly to your platform (good for real-time channels like WebSocket, Slack, etc.).

### Using CollectingOutputChannel

CollectingOutputChannel only collects sent messages in a list (doesn't send them anywhere). The collected messages can be accessed via the `messages` property.

### Creating a Custom OutputChannel

To create your own OutputChannel, subclass `rasa.core.channels.channel.OutputChannel` and implement at minimum the `send_text_message` method:

```python
from rasa.core.channels.channel import OutputChannel
from typing import Text, Any

class MyCustomOutputChannel(OutputChannel):
    def __init__(self, webhook_url: str):
        super().__init__()
        self.webhook_url = webhook_url

async def send_text_message(self, recipient_id: Text, text: Text, **kwargs: Any) -> None:
        """Required method: Send a simple text message."""
        # Your implementation to send text to your platform
        # e.g., make HTTP request, send via WebSocket, etc.
        pass
```

### Common Use Cases

#### Accessing Conversation State

The `tracker_state` property contains comprehensive conversation data including slots, active flows, intents, custom actions called, and other state information. This information can be used to enrich the responses of your channel.

#### Passing Metadata to Rasa

If you need to use extra information from your front end in your custom actions, you can pass this information using the `metadata` key of your user message. This information will accompany the user message through the Rasa server into the action server when applicable, where you can find it stored in the `tracker`. Message metadata will not directly affect NLU classification or action prediction.
