How to send workflows notifications to Telegram in Business Central

Hello, in view of my last publication related to “How to make inquiries in Business Central using Telegram Bot“, it generated a lot of interest, I had the idea to create another project but 100% native in Business Central, this time, it will focus on sending notifications to users subscribed to the Telegram Bot about approval requests in workflows.

This post will be comprised of 2 phases:

  • Operation and configuration of the project in Business Central.
  • Explanation of the code.

Below is the link to the project created on Github.

-Business Center.

Business Central

This development was done 100% in Business Central, only a single page called Telegram Setup was created, which will contain the token and the list of subscribed users to which a User will also be configured, taken from the Business Central User Setup that could be approver or the one who sends the approval requests.

Telegram Setup Page

Below is how the Telegram Setup page will work.
It is composed of 2 parts:

  • Telegram Token.
  • List of subscribed Users.

Telegram Token

To obtain the token please read the last post that is explained in detail.

Once the bot is obtained, we configure our Telegram Setup Page and it will look something like this:

The next step will be to obtain the list of users subscribed to the telegram bot.

To achieve this and explain this post, we will use 2 telegram accounts,

At first, we will configure it as a username: ApprovalRequestWF.

Second, we configure another account that will receive the Approver account, I have placed the username ApproverWF.

Once the UserNames are configured, in both accounts we write the /subscribe command to our bot. This will allow the Business Central module to send us notifications.

List of subscribed Users.

Now, on the Telegram Setup page, we click on Refresh User.

We will see that the 2 users who sent the /subscriber command will appear in the list of Telegram users on the Telegram Setup page.

At this time, through the “Approval user Id” field we can select the relevant users that are configured in the Workflow to receive notifications.

Additionally, in this step a notification will be automatically sent to all users through the telegram bot.

Workflow

For this Post, I used a simple example of Workflow for Purch Invoice as seen in the following image.

I also set it to send approvals to an approver.

Approval User Setup

Previously configured the Workflow, we now configure Approval User Setup.

Test

Sending Approval Request

I have created a Purch Invoice that has the Workflow restriction, and where when sending the “Send Approval Request” the notification will automatically reach the user Approver in Telegram.

This is what the notification would look like on Telegram, with the following message: The user IVAN.LABRADOR 1 requires your approval in the company CRONUS USA, Inc. for the following document: Purchase Invoice 107209

Approving the request

Now, changing to the Approver user, if we go to the “Request to Approve” page we will see the list of pending requests.

At the time of approving the document, an approval notification will automatically be sent to the user who made the approval request.

Below is what the message received that the order has already been approved would look like, with the following message: “User IVAN.LABRADOR has approved document CRONUS USA, Inc. in company Purchase Invoice 107213

Video 1

Creating users

Video 2

Generating approval request:

Video 3

Approving approval request.

CODE

Tables:

For this project, 2 tables were created, the first where we will store the Telegram Token and the second where we will store the Users who subscribed to the notifications.

Telegram Setup

In this table, we store the Telegram Token. This table will only have one record.

Telegram Users

This table stores the User Id (UserName) but also the Chat ID, which is where we will send the notification. Lastly, we also have the Approval User Id field, related to the User Setup table.

Pages

Telegram User

This is a List Part type page, where it will be embedded in the Telegram Setup page. It is tied to the Telegram Users table.

Telegram User Setup

This is the Configuration Page where we will store the Token, and we will have a button called “Refresh User”, where you will get all the users who have subscribed to the Telegram module.

For more information on getting updates, here is the official API documentation: https://core.telegram.org/bots/api#getting-updates

Codeunits

Two codeunits were created, one as the heart of the application, which is in charge of obtaining updates and sending notifications, and another of events that intervenes the moment of sending approval requests and approvals made in the Business Central.

Telegram

This codeunit called telegram has 3 functions.

  • GetUpdates.
  • CreateTelegramUser.
  • SendMessage.
  • GetUpdates: this is the one that will be executed when the “Refresh Users” button is clicked.

Through the Request with the form: https://api.telegram.org/bot%1/getUpdates we can consult, according to our Token, all the queries or commands that were sent to the Bot.

  • CreateTelegramUser: Once the previous method has been executed, this method will be called, whose response will allow us to create Telegram Users, with their respective Chat Id, which is where we will send the notifications.

In the event that the user is being created, we will send the immediate notification: ‘The user has been correctly configured to receive Workflow notifications.‘ otherwise we will only update the user in Business Central.

  • SendMessage: This method will be in charge of sending the notification through the request: https://api.telegram.org/bot%1/sendMessage?chat_id=%2&text=%3

The parameters are the ‘Telegram Token’, the ‘Telegram Chat ID‘, and the desired Text Message.

Events

Last but not least, the Events codeunit.

Here we will manage 2 Events,

  • OnAfterInsertEvent of the “Approval Entry” table: in this event, we will use it to notify the approval request with the following form:

‘The user %1 requires your approval in the company %2 for the following document: %3’

  • OnApproveApprovalRequest from the “Approvals Mgmt.” codeunit: we will use this event to notify that the “Approval Request” has been approved. with the following form:

‘User %1 has approved document %2 in company %3’

In both scenarios, the document type and number are specified. To obtain this information, create the GetType method.

This project, for now, works with workflows that read the following tables on approval input:

  • Reversal Entry.
  • G/L Register.
  • Gen. JournalBatch.
  • Bank Acc. Reconciliation.
  • Purchase Header.
  • Sales Header.

Without a doubt, personalized notifications can be extended to more scenarios. It could also include cases of approval cancellations.

I hope this is useful for you.

Leave a Reply

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