> For the complete documentation index, see [llms.txt](https://readme.directual.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://readme.directual.com/api-integrations/other-integrations/telegram/telegram-advanced-techniques.md).

# Telegram: Advanced Techniques

### Using the templating system

Feel free to use all the features of the [Templating system](/template-system/basics-of-template-system.md) in the [Telegram step](/scenarios/editing-scenarios/integration-steps/telegram-step.md).

### Processing system commands

Telegram bots have `/commands`, so add them in the BotFather (using `/setcommands`) and create **one** scenario which triggers new objects from `TMessageIn`, and if there is no system commands in `text` field, the scenario sends an object to the **Router** **scenario**.

![](https://3071851461-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Nnmtk9_gFGWOddsf6%2F-MQf_jWF3KLwMkd9BRK0%2F-MQfdvp-ASQJAEuSwUeH%2Fsystem-commands.png?alt=media\&token=e629e57d-c633-493d-8ac1-ad05a9f1794d)

{% hint style="info" %}
Important! The best bot architecture includes **only one** scenario which triggers new `TMessageIn` objects.
{% endhint %}

### Context-based architecture

**The Router** scenario (which is not triggered by events but invoked) is routing messages, based on the value of the field `context` in `TChat` object.

![](https://3071851461-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Nnmtk9_gFGWOddsf6%2F-MQf_jWF3KLwMkd9BRK0%2F-MQfgEylh62bus4hpR9K%2Frouter.png?alt=media\&token=1896e7ce-e596-4c3b-89e0-e43485225861)

Condition step (checking `context`) looks like:

![](https://3071851461-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Nnmtk9_gFGWOddsf6%2F-MQf_jWF3KLwMkd9BRK0%2F-MQfggwZed1T6j8IdoUU%2Fcheck-context.png?alt=media\&token=e0ef5e12-bb3b-4290-8368-cc16f5ff04b2)

{% hint style="info" %}
**Multi-context architecture**

You can add as many fields in `TChat` structure (e.g. `subcontext`) and compose conditions on them. That is a way to create multi-context architecture.
{% endhint %}

### Dealing with files, images and userpics

#### Step 1. Getting temporary file path

Call Telegram API-method `getFile` using Telegram step this way:

![](https://3071851461-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Nnmtk9_gFGWOddsf6%2F-MQfhhlX8pC1X3fWHZkr%2F-MQfihLjK1Skat4-Loyh%2Ffile-1.png?alt=media\&token=0e37510d-80ad-4936-a271-b622eb6f72b7)

Then, save to a field temporary URL: `https://api.telegram.org/file/bot_BOT_TOKEN_/{{API_response.file_path}}`, where `_BOT_TOKEN_` is token of your bot.

#### Step 2. Saving file to Directual

Use internal SDK method [$D.fs.download](/javascript-sdk/internal-usdd-methods.md#save-file-to-the-internal-file-storage-usdd-fs-download) (it can be applied in both [SDK step](/scenarios/editing-scenarios/action-steps/js-sdk-step.md) and in [Edit object step](/scenarios/editing-scenarios/action-steps/edit-object-step.md))
