> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/examples/echo-bot/python.md).

# Python

```python
import asyncio
import os
from dnotifier import DNotifier

BOT_ID = "echo-bot"

def extract_sender_id(prefixed_sender: str, app_id: str) -> str:
    prefix = f"{app_id}_"
    if prefixed_sender.startswith(prefix):
        return prefixed_sender[len(prefix):]
    return prefixed_sender

async def main():
    app_id = os.environ["DNOTIFIER_APP_ID"]

    async def on_message(msg):
        body = msg.payload.to_json()
        if not isinstance(body, dict) or body.get("type") != "text":
            return
        text = body.get("text")
        if not text:
            return

        sender_user_id = extract_sender_id(msg.metadata.sender, app_id)

        await notifier.send(
            sender_id=BOT_ID,
            receiver_id=sender_user_id,
            data={"type": "text", "text": f"Echo: {text}"},
        )
        print(f"Echoed to {sender_user_id}: {text}")

    notifier = DNotifier(
        app_id=app_id,
        secret=os.environ["DNOTIFIER_SECRET"],
        transport="ws",
        user_id=BOT_ID,
        on_connected=lambda: print("Echo bot ready"),
        on_message=lambda msg: asyncio.create_task(on_message(msg)),
        on_disconnected=lambda code=None, reason=None: print(f"Disconnected: {reason}"),
    )

    await notifier.connect()
    await asyncio.sleep(3600)

asyncio.run(main())
```

## Test

Send from another client to `echo-bot` and verify the `Echo:` prefix in the reply.
