---
title: General Hooks
description: Common hooks implementation
---
This section will show how the general hooks are processed, These hooks include:

- on-receive       - `[POST]` logic to process after user responds / sends a message
- on-generate      - `[PRE]` logic to process before a template is processed for rendering to user
- middleware       - `[POST]` middleware logic to process mid-flight

Consider an example for `middleware` below

> The example below works for `on-receive`, `on-generate` and `middleware` hooks

## Middleware
Imagine you want to do any general business logic mid-flight of the template processing.
You can define a `middleware` hook just for that.

An example will be saving some data to the db or retrieving some data and save them in the user session

```yaml
1000:
  type: text
  middleware: "example.engine_chatbot.hooks.account_service.fetch_accounts"
  message: "Type `transfer` to start funds transfer process"
  routes:
    "transfer": "FUNDS-TRANSFER"
    "no": "HOME"
```

When user responds, the hook will be processed.

Consider a sample business logic

```python
  # example/engine_chatbot/hooks/account_service.py

  from pywce import HookArg

  def fetch_accounts(arg: HookArg) -> HookArg:
    """
     fetch corresponding user accounts & save to user session to retrieve later
    """
    print(f"Received hook arg: {arg}")

    if arg.user_input.lower() == "transfer":
      user_accounts: list = AccountsManager(mobile=arg.session_id).get_accounts()
    
      arg.session_manager.save(
        session_id=arg.session_id,
        key="accounts",
        data=user_accounts
      )

    return arg
```
