NAV meets Telegram. AL, JSON an so on.
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.
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:
"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.
Create new page for Bot Setup table:
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
Also create a page for this table:
5. Now let's create some logic to handle messages.
Create new codeunit 'Telegram management' with next globals:
Create new function 'GetUpdates' :
In this function we create Request line to get messages from our bot.
Pay attention to last block:
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:
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:
Functions for sending Messages to user:
6. Now let's return to BotFather and create commands list:
I create them to standardize commands text.
After that, you can see commands list directly in your bot:
7. Now I add '/shipments' and '/payments' to CASE cycle of ReadUpdates function (after '/start'):
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:
9. Now I add GetUpdates action on Bot setup page :
And Send Hello action on user setup page:
10. Time to test. Publish the extension and open Telegram Bot Setup page. Input bot token:
send '/start' command in your bot dialog and press GetUpdates button. You'll see next message in your messenger:
11. Open Telegram User setup page. You user appear:
Fill Salesperson code in it.
Press Send Hello action to send greetings:
12. Send '/shipments' and '/payments' command to your bot. After that press GetUpdates action on Bot Setup page. You'll get next messages:
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
-
-
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.
-
*This post is locked for comments