Odoo Credit Limit Approval Using Telegram

Hendra PM
IT Paragon
Published in
6 min readJul 12, 2020

Credit Limit is a method used to give a warning when the amount of a sale order from a customer and outstanding A/R Invoice exceeds the specified limit. If this is applied there will be an opportunity for customers to pay off outstanding A/R Invoice so that it will have a good impact on the company’s monthly cash flow. In this article, we will use the open-source ERP System, Odoo, and Telegram bot as media approval.

Notes: Before proceeding to the tutorial below it is hoped that readers will understand Odoo and customize the modules on Odoo. Because this tutorial is integration between Odoo and Telegram. The following is the Odoo 11 documentation link.

Prerequisites

When making this prototyping project, I used:

Architecture

Architecture

Flowchart

Flowchart

Create Telegram Bot

You need to talk with BotFather to create a new Telegram bot. Start the conversation with type /newbot, then BotFather will guide you like the following example and keep the access token after bot has created.

Create Telegram Bot

Running Odoo

We will install Odoo version 11.0 using docker compose.

  • Create a workspace like the example below
  • Add the code below to docker-compose.yml
  • Add 2 custom modules namely partner_credit_limit and pcla_via_telegram in the addons folder. The pcla_via_telegram module is a module that inherits the partner_credit_limit module because the partner_credit_limit module does not yet have an approval function using telegrams. To be more clear what code is added for approval, you can scroll to the Inherit Partner Credit Limit section.
  • Run the docker-compose.yml with command.
user@notebook:~/my_workspace/odoo$ docker-compose up -d
user@notebook:~/my_workspace/odoo$ sudo chown 101:101 -R odoo-data/
user@notebook:~/my_workspace/odoo$ docker-compose logs -f

You will see the output from the database and Odoo. Once it has completed start up then you can access your instance at http://localhost:8069.

And congrats you have Odoo running inside docker containers.

Odoo Login Form
  • Login to Odoo then install the pcla_via_telegram module.
  • Don’t forget to fill in the config parameters for bot access token and approver chat_id.

Inherit Partner Credit Limit Module

I assume the reader already understands the basics of how to create and inherit the Odoo module, if not, please read more in the official Odoo documentation here.

Previously, we have downloaded the pcla_via_telegram module, the following code will explain the added code so that the approval message can be sent to the Telegram and the controller function to respond to the action of the approver.

Telegram Config Parameter

To be able to send messages to the Telegram we need several parameters including the Telegram API URL, bot access token, and Telegram approver chat_id. These parameters are in the pcla_via_telegram/data/ ir.config_parameter.xml module with the following content.

Then the file above must be registered at pcla_via_telegram/__manifest__.py (line 14) as below.

Confirmation Approval Request Dialog (Wizard)

In the partner_credit_limit module when confirming the sale order and the customer order amount reaches a certain limit, a notification pop-up will appear without any other action. For that, we need to change the notification pop-up to a confirmation dialog so that the next action is to send a message to the Telegram.

Before: Warning Notification Only Without Next Action
After: Confirmation Dialog to Send Approval Request

What we need to do is create a wizard-like the one in the pcla_via_telegram / wizard folder. In the wizard’s folder, there is a confirmation_dialog.py file that aims to send messages to the Telegram and confirmation_dialog.xml file to generate the form/confirmation dialog as shown above. The following content is in the confirmation_dialog.py file.

At line 48 there is an endpoint/sendMessage that we call when sending a message to the Telegram. For more details, can be accessed here, then in lines 41–46 is the inline keyboard format that we use to make the approve/reject button along with the information/message we send to the approver. Lines 43 and 44 contain callback_data which contains the statement true/t or false/f followed by the id of the sale order for which approval is requested (ex. Format: ‘t | 21’). The data will later be sent by the approver together when the approver clicks the button.

Controllers to Accept Approve or Reject Requests

When the approver clicks approve/reject button on the Telegram, the data will be sent to the Telegram server, then the polling script (will be explained in the section below) will consume the data then make an http request to the Odoo server. The following controller is made in the pcla_via_telegram/controllers/ main.py file

Controller to Receipt Request Approve or Reject

In the script above the method request that we allow is POST, where later when polling/listener script makes an http request to the endpoint above (http://localhost: 8069/API/credit_limit/approval), then the action taken is to process the sale order depending on the data received, whether approve or reject. If approved, the sale order will be confirmed and the state sale order will change to sale, but if it rejects, then the sale order will be canceled and the state sale order will change to cancel. The process is in lines 17 and 19.

Create Polling/Listener Script

There are 2 methods that can be used to get message updates from Telegram, namely polling and webhook. In this paper, we will not use the webhook method because this method requires public IP/DNS so that when there is an update message from a Telegram it can be redirected to the IP/DNS. For that, we will create a poll/listener script where we will request an update message from the Telegram every 5 seconds using the infinite loop then send the data via an HTTP request to the Odoo server. Create a file with the name telegram_polling.py, keep it separate from the Odoo location. Here is the code:

Run the above script with the command

user@notebook:~/my_workspace$ python3 telegram_polling.py

It’s Demo Time

Demo Time

Things to be Improved

  • Add a message queue between Odoo -> telegram and polling / listener script -> Odoo. The goal is to increase user experience.
  • Improve security such as adding access tokens when creating a controller/route on Odoo.
  • Adds more comprehensive and meaningful error handling.

Conclusion

The integration of Odoo and telegram is not only limited to the credit limit feature as above, but we can develop it more broadly according to the needs and creativity of each, for example, such as to request report P/L statement via telegram and so forth. The author hopes this article can help the reader. If there are feedback or comments please leave a comment column. Happy reading.

--

--