The other day I was experimenting with new Geofencing feature in Azure Maps. What I was trying to achieve is sending a message to my telegram when I leave my home area. How hard can it be right?

Then I discovered that there is no Logic App connector for Telegram and that their Bot API URL format isn't as straight forward as I would like. This explains why there isn't a connector yet. To solve this I used a Logic App connector to call an Azure Function Proxy. The proxy transforms the call to something the Telegram API would understand. This allowed me to make the connection once and easily send a message from multiple logic apps in that same Azure subscription. And above all, this is a 'no code' solution.

Prerequisites

For this to work, there is a prerequisite to registering a bot with Telegram. This is a simple process which is already explained by Microsoft. Register your bot by using this guide https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-connect-telegram?view=azure-bot-service-4.0 or have a bot key ready from a previously registered bot.

Next to the bot registration we also need to retrieve the chat id. This can be done by starting a chat with @get_id_bot. Say anything and it will return your chat id. The @get_id_bot can also be added to groups to retrieve the group id.

Sending a message with Telegram Bot API

After retrieving the bot key and the chat id we should be able to send a message with the bot. This can be done by using the following url and replace the placeholders:

https://api.telegram.org/bot<botKey_here>/sendMessage?text=<text_here>&chat_id=<id_here>

An example would be:

https://api.telegram.org/bot628752025:AAFjS9MR3MubX-RWpxqltVtR5Wju9EoUAAo/sendMessage?text=Hello World&chat_id=123456789

Notice that there is a prefix "bot" in the URL we are calling. This is precisely why only a Logic App connector would not be sufficient. A Logic App Connector does not allow to use the API key from the Authentication Type "API Key" in the path. Only 'Header' and 'Query' are supported at this time. What we can do is make use of an Azure Function Proxy to rewrite the URL from only query string parameters to a valid Telegram Bot API URL. You can test the URL by just pasting it in your browser and you will get your first message from the Telegram Bot.

Azure Function Proxy

To be able to change the URL we need to add a new Azure Function App. I used the consumption plan for this but dedicated plan works as well. I created a function app like this:

CreateFunctionProxy

After the creation of the function app go to the advanced editor and use the JSON below:

{
    "$schema": "http://json.schemastore.org/proxies",
    "proxies": {
        "telegramtrigger": {
            "matchCondition": {
                "route": "/{operation}",
                "methods": [
                    "GET"
                ]
            },
            "backendUri": "https://api.telegram.org/bot{request.querystring.apikey}/{operation}"
        }
    }
}

"route": "/{operation}" makes this proxy trigger on every call to the base URL that can be found in the proxy overview. The 'backendUri' is the new URL that this proxy will call.

TelegramProxy

The URL is then changed by adding the bot key in there. The {operation} at the end literally just copies everything that is behind your base URL and paste it in the new 'backendUri'.

Logic App Custom Connector

Next on the list is implementing the Logic App Custom Connector. You can download the custom connector template from https://github.com/foppenm/TelegramLogicAppConnector. Open de JSON file in an editor like VS code and change to following to match your proxy URL:

ChangeJson

Now let's create a Logic App Custom Connector and press edit.

EditConnector

After importing the changed JSON from the previous step you only have to press "Update Connector" and you can start using it in your logic apps. Optionally you can pick a color and choose an icon to use for this. This determines how it is presented in the logic apps designer.

Now I am able to send a message to my Telegram when an area change is detected within Azure Maps Geofencing. To achieve this, first add the custom Telegram connector to your Logic App and use your bot key.

TelegramConnection

Then use the event grid trigger together with the 'Send Message' step from the custom connector to send messages on area updates from Azure Maps.

EventGridTelegram

Conclusion

Was this the best use of a custom connector and a function proxy? Probably not but it sure was fun to figure out what it can do and that it can be quite powerful in some scenarios. What I find re[mark]able is the fact that this is a no code solution. Of course you could just use an azure function with the telegram .net library but for this scenario for just sending simple messages is, in my opinion, a total overkill.

Thanks for reading!