web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

NAV meets Telegram. AL, JSON an so on.

Andrey Baludin Profile Picture Andrey Baludin 3,941

Hello team! In this article we'll create NAV integration with one of the popular messengers - Telegram. Also it's a good example of how to work with HTTP Client and JSON data.

You need Dynamics NAV 2018 (or Tenerife) and Visual Studio Code with AL extension to build this.

1. Firstly, we need to create telegram bot for communication.

Add @BotFather bot to your conversations list, type '/newbot' for new bot creation, type his description and username. After that, you'll get bot token. Keep it in secure place.

1.JPG

Now, if you call this request (where BOT_TOKEN is a bot token from BotFather) :

https://api.telegram.org/botBOT_TOKEN/getUpdates

you'll get messages from all the users to bot:

6.JPG

"text": "/shipments" - is text which I send to bot

For sending messages use SendMessage api (where CHATID is a value from "chat" : {"id"} object) :

https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHATID&text=TEXT

2. Open Visual Studio Code and create a new project.

3. Create new table 'Telegram bot Setup'

Needed fields:

  • Bot name - Key value to keep several bots setup. In this example we'll work with only one bot - so it'll be empty.
  • Bot token - save your bot token from BotFather here.
  • Bot offset - message offset to get only new messages from bot.

2.JPG

Create new page for Bot Setup table:

4.JPG

4. Create new table 'Telegram User Setup' to handle users which communicating with your bot.

Needed fields:

  • Salesperson code - to link bot user with NAV salesperson
  • Bot name - to link bot with bot user
  • Chat ID - to send messages to bot user
  • First Name - Bot User name
  • Last Name - Bot User last name
  • User ID - telegram user ID

3.JPG

Also create a page for this table:

5.JPG

5.  Now let's create some logic to handle messages.

Create new codeunit 'Telegram management' with next globals:

18.JPG

Create new function 'GetUpdates' :

7.JPG

In this function we create Request line to get messages from our bot.

Pay attention to last block:

8.JPG

Here we read response from Telegram bot to JsonText and after that we select object named 'result' as array  and read them 1 by 1 because messages are in it.

ReadUpdates function here:

9.JPG

This function makes next things:

  • Updates Bot offset in our setup
  • Gets Chat ID of sender
  • If User send '/start' command to bot - it parse the message and tries to create user with sender credentials. If successful - it sends message that user has been created, if not - message that user already created.

Used functions:

Important functions to parse JSON:

11.JPG

Functions for sending Messages to user:

16.JPG

14.JPG

15.JPG

6. Now let's return to BotFather and create commands list:

19.JPG

I create them to standardize commands text.

After that, you can see commands list directly in your bot:

20.JPG

7. Now I add '/shipments' and '/payments' to CASE cycle of ReadUpdates function (after '/start'):

10.JPG

12.JPG

13.JPG

CrLf variable needed to insert Carriage Return and Line Feed (each operation from new line).

8. And in the end I add event subscription to send message then user's Sales Order will be posted:

17.JPG

9. Now I add GetUpdates action on Bot setup page :

21.JPG

And Send Hello action on user setup page:

22.JPG

10. Time to test. Publish the extension and open Telegram Bot Setup page. Input bot token:

23.JPG

send '/start' command in your bot dialog and press GetUpdates button. You'll see next message in your messenger:

28.JPG

11. Open Telegram User setup page. You user appear:

Fill Salesperson code in it.

24.JPG

Press Send Hello action to send greetings:

25.JPG

12. Send '/shipments' and '/payments' command to your bot. After that press GetUpdates action on Bot Setup page. You'll get next messages:

26.JPG

27.JPG

13. If you post Sales Order with Salesperson code = salesperson code in Telegram User setup - this user will get a message about shipment. You can send any info to users.

14. To automate updates receiving you can set Telegram Management codeunit as job queue.

If you still read my post - hope that it was useful for you.

Project files you can download here: https://yadi.sk/d/GuqoR5OM3T3gHR

Many thanks to the authors who inspired me to do this job:

and his article: http://www.intech-systems.com/nav-telegram-integration/

Arend-Jan Kauffmann and his article: https://www.kauffmann.nl/2017/06/24/al-support-for-rest-web-services/

Comments

*This post is locked for comments

  • Antonio Paul Profile Picture Antonio Paul
    Posted at
    That is much interesting, I will try this on my website Tiktok mp4 Downloader
  • Pradeep.Banga Profile Picture Pradeep.Banga 15
    Posted at
    Hi, How to send PDF or excel reports via Telegram, we are able to send message but got stuck in sending the Documents. Any Luck.
  • Lucero Ortega Profile Picture Lucero Ortega 5
    Posted at

    Hi Andrey,how can I declare a variable that points to a table-type extension?