Odoo provides a wide range of pre-built connectors and APIs that enable seamless integration with popular third-party applications and services. Odoo Telegram integration lets you connect your Odoo instance with the Telegram messaging platform. With this integration, you can send and receive messages between your Odoo system and Telegram users, enabling seamless communication and automating processes.
Integrating Odoo With Telegram:
Telegram does not provide direct access to personal accounts through APIs for security and privacy reasons. Therefore, it is not possible to directly fetch incoming messages from your personal Telegram account. The Telegram Bot API is the only official way to interact with Telegram programmatically, and it requires a bot token to authenticate and authorize your application to access Telegram data.
Steps to create a Telegram Bot and fetch BotToken:
->Open Telegram and search for the “BotFather” user.
->Search for BotFather in telegram and initiate a new chat. Send the command “/newbot” to create a new bot.
->BotFather will ask you to choose a name for your bot (e.g., OdooTestBot) and
a unique username (e.g., OdooTestBot). Once you provide a name and username,
->BotFather provides a token for your new bot
Fetch Incoming Messages in Real-Time From Odoo:- Whenever a user messages your bot, it is possible to fetch it in real-time by configuring a webhook URL on your Odoo server
Eg:-
Create a controller to set up a webhook
from odoo import http, _
import json
class TelegramWebhookController(http.Controller):
@http.route('/telegram/webhook/receiver', type='json',auth='public',methods=['POST'], csrf=False)
def handle_webhook(self, **post):
payload = json.loads(http.request.httprequest.data)
print("payload", payload)
Create a function to register the webhook in Telegram using the setWebhook API provided by Telegram.
set_webhook_url = f'https://api.telegram.org/bot{bot_token}/setWebhook?url={webhook_url}'
Here, give your token fetched from “BotFather” as the value of ‘bot_token’ and the controller route with the domain (Note:- Webhook only supports HTTPS. Use ‘ngrok’ for test cases) pass the server as the value of ‘wehook_url’. When the response is True, the webhook is successfully registered in telegram, and whenever a user messages to your bot, it will trigger, and you can fetch the message from the JSON value.
To check your webhook status in telegram, use getWebhookInfo API
Eg:-
# Check webhook info
webhook_info = f'https://api.telegram.org/bot{bot_token}/getWebhookInfo'
response = requests.post(webhook_info)
print("Telegram Webhook Info Response:", response.json())
When Telegram sends an update to your webhook URL, you can come up with a message handling function in Odoo to process the incoming messages and take appropriate actions based on the content of the messages.
Eg:-
def handle_webhook(self, **post):
payload = json.loads(http.request.httprequest.data)
print("pal", payload)
if payload:
data = payload['message']['from']
message = payload['message']['text']
print("me", data)
data = {
'contact': data,
'message': message,
'chat_id': data.get('id')
}
record = self.env['message.history'].with_user(SUPERUSER_ID).create(data)
print("record", record)
You can create a record in your custom model, to save the telegram messages with the unique chat_id to differentiate the user.
Polling method to fetch incoming messages from Odoo:-
You can use the getUpdates API to poll Telegram for updates instead of webhook. This means your bot will periodically request the Telegram API to check for new messages. Polling is less efficient and can lead to delays in receiving updates, especially if the polling interval is long. It is more suitable for testing and development purposes
In the case of Odoo, we can simply use scheduled actions instead of polling by calling getUpdates API between a scheduled time period
Send message from odoo:-
Telegram provides sendMessage API to send a text message to the user from the using Bot Token
eg:-
message = "Thank you for contacting us"
url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
data = {'chat_id': chat_id, 'text': message}
response = requests.post(url, data=data)
print("response", response.json())
Here, the message is passed to provided Chat ID (Unique Id to identify and differentiate between different chats)
Telegram also provides API to send different types of messages
Eg:- sendPhoto
sentAudio
sendDocument
sendSticker
sendVideo
sendVoice
(Telegram provides detailed descriptions of each API in their document)
Benefits Of Odoo Telegram Integration:
Integrating Odoo with Telegram can offer several benefits to your business, enhancing communication, efficiency, and overall operations
Such as:-
Chat with Customers: You can set up a Telegram chatbot to provide customer support, answer frequently asked questions, and handle inquiries from your website visitors or customers by achieving this through the implementation of live chat functionalities or other relevant features.
Task Management: Create a Telegram bot to manage tasks and to-do lists directly from the messaging app. You can add tasks, set deadlines, and receive reminders without leaving Telegram.
Sales and CRM Integration: Connect Telegram with Odoo’s CRM module to capture leads and manage customer interactions directly from Telegram.
Inventory Alerts: Receive inventory alerts and stock level notifications on Telegram, enabling you to take timely actions to restock products.
Order Tracking: Integrate Telegram with your Odoo e-commerce platform to allow customers to track their order status and receive updates on delivery progress.
Survey and Feedback: Conduct surveys and gather customer feedback using Telegram’s interactive features.
To read more about integrating Facebook messenger in Odoo 16 app, refer to our blog How to Integrate Facebook Messenger in Odoo 16 App