Telegram notification on Odoo Updates

Here we’ll make a telegram bot, that sends you notification whenever new task assigned to you is created. You’ll need:

Deployment

Create a bot

  • In telegram client open BotFather

  • Send /newbot command to create a new bot

  • Follow instruction to set bot name and get bot token

  • Keep your token secure and store safely, it can be used by anyone to control your bot

Prepare zip file

mkdir /tmp/bot
cd /tmp/bot

pip3 install python-telegram-bot -t .
wget https://raw.githubusercontent.com/it-projects-llc/odoo-sync/master/doc-src/odoo2x/odoo2telegram/lambda_function.py
zip -r /tmp/bot.zip *

Create Lambda function

Runtime

Use Python 3.6

Function code

  • Set Code entry type to Upload a .zip file

  • Select bot.zip file you made

Environment variables

  • BOT_TOKEN – the one you got from BotFather

  • LOGGING_LEVEL – Level of loger. Allowed values: DEBUG, INFO, CRITICAL, ERROR, WARNING. Default value: INFO

  • TELEGRAM_USER_ID – put here your ID. You can get one by sending any message to Get My ID bot

Trigger

  • API Gateway. Once you configure it and save, you will see Invoke URL under Api Gateway details section

  • Set the security mechanism for your API endpoint as Open

Register lambda webhook

  • Model: Task (project.task)

  • Trigger Condition: On Creation

  • Filter by task by users:

    • In Odoo 10.0: set Filter to [["user_id","=",123]]

    • In Odoo 11.0+: set Apply On to [["user_id","=",123]]

    Here 123 is your user ID. To get the ID, navigate to [[ Settings ]] >> Users menu, open your user, check ID value in the browser URL

  • Python Code:

    INVOKE_URL="https://PASTE-YOUR-INVOKE-URL"
    data = {
        "task_name": record.name,
        "created_by_name": record.create_uid.name,
    }
    log("Sending to webhook: %s" % data)
    requests.post(INVOKE_URL, json=data)
    

Try it out

  • Open created telegram bot and send any message to it. It’s needed to allow bot send a message to you.

  • Open Odoo and create new task assigned to you.

  • RESULT: the bot will send you a notification

  • If something goes wrong, check Odoo logs and CloudWatch logs

Source

Key script of the bot is presented below:

import telegram  # https://github.com/python-telegram-bot/python-telegram-bot
import sys
import os
import logging
import re
import json


logger = logging.getLogger()
LOG_LEVEL = os.environ.get('LOG_LEVEL')
if LOG_LEVEL:
    logger.setLevel(getattr(logging, LOG_LEVEL))


BOT_TOKEN = os.environ.get('BOT_TOKEN')
TELEGRAM_USER_ID = os.environ.get('TELEGRAM_USER_ID')


def lambda_handler(event, context):
    logger.debug("Event: \n%s", event)
    data = json.loads(event['body'])

    bot = telegram.Bot(token=BOT_TOKEN)
    message = "New task is created by %s:\n%s" % (data['created_by_name'], data['task_name'])
    bot.sendMessage(TELEGRAM_USER_ID, message)

Custom integration

For any further assistance contact us by email or fill out request form: