# Conversation Class

The `Conversation` class is located at `app/conversation.rb` file.

Every message that arrives and every reply sent can be handled and accessed via callbacks defined here and all the [Contexts](/contexts.md) should inherit this class.

{% hint style="info" %}
For those who are familiar with Ruby on Rails, this class is the equivalent to `ApplicationController`.
{% endhint %}

```ruby
class Conversation < Kogno::Context

  before_blocks :do_something_before_blocks
  after_blocks :do_something_after_blocks

  def do_something_before_blocks
    # This will be called before the blocks method in the current context will be executed
  end

  def do_something_after_blocks
    # This will be called after the blocks method in the current context will be executed
  end

end
```

| Callback       | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| before\_blocks | This callback will be executed before the [<mark style="color:orange;">`blocks()`</mark>](/contexts.md#method-blocks) method from a Context class is called.                                                                                                                                                                                                                                                                                                                                                                                                      |
| after\_blocks  | This callback will be executed after the [<mark style="color:orange;">`blocks()`</mark>](/contexts.md#method-blocks) method from a Context class is called.                                                                                                                                                                                                                                                                                                                                                                                                       |
| before\_exit   | <p>This callback will be executed before the conversation context is changed.<br><br>Specifically in the call of methods <a href="/pages/-M1X18SZXeuWsbNwekF0#moving-between-contexts"><mark style="color:orange;"><code>change\_to()</code></mark> or <mark style="color:orange;"><code>context\_exit()</code></mark></a>. </p><p></p><p>In this callback, the method <a href="/pages/-M1XkeTckF1BaecfGa82#halt"><mark style="color:orange;"><code>halt()</code></mark></a> can be implemented, to prevent the conversation context finally changes or exit.</p> |

## Accessible Instances and methods

These instances and methods are accessible from the `Conversation` class and also from any [`Context` class](/contexts.md) defined in a project.

### <mark style="color:orange;">`@user`</mark>

It is an instance of the `User` model (`ActiveRecord::Base`) that corresponds to the user who sent the incoming message.

#### Usage

```ruby
class Conversation < Kogno::Context

  before_blocks :log_user_platform

  def log_user_platform
    logger.info "The user's platform is #{@user.platform}"
  end

end
```

### <mark style="color:orange;">`@message`</mark>

It is the instance of the user's incoming message.

#### Usage

* **To see the message content:** `@message.text`
* **Catch a button click event:** `@message.postback_payload` and if includes parameters `@message.postback_params`. Read more about [postbacks](/contexts/blocks/postback.md).
* Check if the message is empty: `@message.empty?`

See the full list of methods here.

### <mark style="color:orange;">`@reply`</mark>

It is an instance of the `Kogno::Notification` class, which contains a wide variety of reply messages like text, button, url, carousel, etc. Full list [here](/replies-notifications.md#message-formats).

{% hint style="success" %}
In Kogno, we try to unify almost all reply types for all supported platforms, so that a single code can be written for all of them.
{% endhint %}

#### Usage

```ruby
class Conversation < Kogno::Context

  after_blocks :send_a_final_message

  def send_a_final_message
    @reply.text "I'll respond this mesage all the time."
  end


end
```

For more information and examples, check the [Replies section](/replies-notifications.md).

### Methods

Also methods like [<mark style="color:orange;">`change_to()`</mark>](/contexts.md#change_to-route-string-params-hash), [<mark style="color:orange;">`delegate_to()`</mark>](/contexts.md#delegate_to-route-string-args-hash) and [<mark style="color:orange;">`halt()`</mark>](/contexts/blocks.md#halt).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kogno.io/conversation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
