How to make inquiries in Business Central using Telegram Bot

Hello, today you want to share something that I found quite interesting and it’s how to send or receive notifications from Business Central to Telegram.

I initially thought of doing this project with Azure Functions, which I’m sure is easier to create, but to do something different, I thought of doing it in Asp.net.

This project focused on creating queries for Items in Business Central from telegram, but you can create notifications from Workflows or even create Sales Order or Post documents, as examples.

Below I share a screenshot of the final result.

The project will be divided into 3 parts:

  • 1) Configuring the Telegram Bot.
  • 2) Creating the WebServices in Busines Central.
  • 3) Creating the ASP.net project.

Below are the links to the projects that were created:

Business Center.
AzureFunctions.

Telegram Bot

To configure the telegram bot I base myself on the following website:
https://core.telegram.org/bots/api

The first thing I did was create the bot in telegram as follows:

1) We look for the telegram bot called botfather

2) We write the command /newbot and then the name of our bot.

3) We write a username

4) We copy the telegram token that we will use in the Asp.net project

5) Optionally, we can test in postman that our bot is well configured as follows:

https://api.telegram.org/bot{{Token}}/getMe

When executing we will obtain something similar to this:

Business Central

This is the simplest project, it consists of creating our web services. On this occasion I decided to use a codeunit.

Where I will publish it as a web services and consume it as an Odata.

For more information on how to achieve the above, check this Microsoft link or the link of this previous post where I specify in more detail how to achieve it.

ASP.NET

This project will function as a bridge between Business Central and the Telegram Bot.

To get started, we’ll create an ASP.net project using the Asp.net Core Web API template.

We put a name.

We use .NET 6 as framework

Once we have created it, we will have something similar to the following image, where the first thing we will do is delete the default “hello world” called Weather Forecast.

We install the following Nuget, the most important Telegram Bot.

Share Proyect

We add a new project called Share

We create a new project of type Class Library.

We put Share, because it will cut across our solution. Where we will have all our models and services that we will use in the Asp.net project.

Models

ConfigurationsValues:

This model will serve to read all our token and secret of the application.

The ones we will use are related to Oauth2, webservice call and Telegram token.

Note:

In the Azure portal, in Configuration, we must store our variables so that the project runs correctly when we publish it.

Item

The Item class is where we will use it as a model to signalize and deserialize the entry and exit of the Business Central web services.

Request BC

We use this class to store the “Item No” request in the jsonText.

Response Model:

The response model is a generic model that will allow us to store the response of our API call specifically in the method that connects to BC called GetDataFromBC, if it is successful in the Result property we will store our Json with the list of elements.

Output

With this class we will deserialize the output of requests to Business Central.

Error Model

In the event that our product is not found, we will use the following class to undo the exit from Business Central.

Services

I had already used this service for a similar purpose in the project I did on how to connect Business Central with a mobile application made in Xamarin, the difference is that in this project it was slightly modified to pass parameters to our BC web services.

Asp.Net

Program

The first thing we will modify when we create our ASP.net project is the Program.cs.

The specific parts that were added to the project have been encapsulated with the following comment: “//Added for telegrambot

In this, we have configured the “ITelegramBotClient” interface that we will use to access the methods that will communicate with the Telegram bot, our BCServices, and the ConfigureWebhook service that will initialize our Telegram Bot, and finally our Endpoint.

In the following Link there is more information and examples of how to connect from .NET to the telegram bot.

AppSettings.json

In this JSON we will configure all the global variables that we will deserialize in our previously shown “ConfigurationsValues” class and handle all Outh2 and telegram bot tokens.

Additionally, the HostAddress variable has been added, which will be the URL address once we have published our project.

This address is used to configure the bot which will be explained further down in this post.

Configure Webhook

This class, through the StartAsync method, configures the setWebhook according to the HostAddress and eliminates it when the application stops in StopAsync.

On the following website there is more detailed information on this method.

BCController

This controller is the one that is configured to receive the messages that are sent to the telegram bot.

ConnectToBC

This service will be in charge of reading the message sent to the bot, separating the command from the parameters, and subsequently calling the BC service to consult the requested information.

GetItem

In this method we serialize the query and send it to the GetDataFromBC service, in case of success we will respond to the Telegram Bot with the requested information, in case of no success we will also respond with the error message.

Create Item Request

The auxiliary method that performs the request.

When we finish our project, it should have the following structure:

And when we execute it, a view similar to the following:

Tests

Postman

Next, I show how we call our BC controller from the postman.

Body.json
Video 1:
Published Application

Once we have the application published, we can check the status of our webhook with the following request in postman:

https://api.telegram.org/bot{{Token}}/getWebhookInfo

More information at this link

Video 2:

That is all. Possibly later I will do this same post with Azure Functions.

I hope it has helped you, if you liked it you can share.

1 thought on “How to make inquiries in Business Central using Telegram Bot

  1. Pingback: How to read files via SFTP in Business Central - Ivan Singleton

Leave a Reply

Your email address will not be published. Required fields are marked *