Skip to content

Commit dfb31d7

Browse files
committed
Merge PR #1824 into 18.0
Signed-off-by pedrobaeza
2 parents 15cd24e + ae557f2 commit dfb31d7

File tree

15 files changed

+884
-0
lines changed

15 files changed

+884
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
===================
2+
Telegram Standalone
3+
===================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:7b454e99bd84f3c52065a3893e37299095582f3afd6bc2e449ffe6ec7b9f9459
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github
20+
:target: https://github.com/OCA/social/tree/18.0/mail_gateway_telegram_standalone
21+
:alt: OCA/social
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_telegram_standalone
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/social&target_branch=18.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module provides a generic engine to integrate Telegram Bots with
32+
Odoo. It serves as a foundation for other modules to send notifications,
33+
allowing you to manage bot tokens and authorized chat IDs in a
34+
centralized way.
35+
36+
**Table of contents**
37+
38+
.. contents::
39+
:local:
40+
41+
Usage
42+
=====
43+
44+
To use this module:
45+
46+
1. Go to **Settings > Technical > Email > Gateways**.
47+
2. Create a new Gateway, set the Gateway Type to "Telegram", and enter
48+
the Token provided by @BotFather.
49+
3. Click the **Fetch Chats** button to automatically find Chat IDs of
50+
people who have messaged the bot.
51+
4. Click **Test Connection** to verify that Odoo can communicate with
52+
Telegram.
53+
54+
Bug Tracker
55+
===========
56+
57+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/social/issues>`_.
58+
In case of trouble, please check there if your issue has already been reported.
59+
If you spotted it first, help us to smash it by providing a detailed and welcomed
60+
`feedback <https://github.com/OCA/social/issues/new?body=module:%20mail_gateway_telegram_standalone%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
61+
62+
Do not contact contributors directly about support or help with technical issues.
63+
64+
Credits
65+
=======
66+
67+
Authors
68+
-------
69+
70+
* Anmol Garg
71+
72+
Contributors
73+
------------
74+
75+
- ``Anmol Garg <https://github.com/AnmollGarg>``\ \_:
76+
77+
- Anmol Garg
78+
79+
Maintainers
80+
-----------
81+
82+
This module is maintained by the OCA.
83+
84+
.. image:: https://odoo-community.org/logo.png
85+
:alt: Odoo Community Association
86+
:target: https://odoo-community.org
87+
88+
OCA, or the Odoo Community Association, is a nonprofit organization whose
89+
mission is to support the collaborative development of Odoo features and
90+
promote its widespread use.
91+
92+
This module is part of the `OCA/social <https://github.com/OCA/social/tree/18.0/mail_gateway_telegram_standalone>`_ project on GitHub.
93+
94+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Telegram Standalone",
3+
"summary": "Generic Telegram API connector",
4+
"version": "18.0.1.0.0",
5+
"category": "Social",
6+
"author": "Anmol Garg, Odoo Community Association (OCA)",
7+
"website": "https://github.com/OCA/social",
8+
"license": "AGPL-3",
9+
"depends": [
10+
"base",
11+
"mail",
12+
"mail_gateway_telegram",
13+
],
14+
"data": [
15+
"security/ir.model.access.csv",
16+
"views/mail_gateway_views.xml",
17+
],
18+
"installable": True,
19+
"application": False,
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import mail_gateway
2+
from . import telegram_chat
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import logging
2+
3+
import requests
4+
5+
from odoo import _, fields, models
6+
from odoo.exceptions import UserError
7+
8+
_logger = logging.getLogger(__name__)
9+
10+
11+
class MailGateway(models.Model):
12+
_inherit = "mail.gateway"
13+
14+
telegram_chat_ids = fields.One2many(
15+
"telegram.chat", "gateway_id", string="Authorized Chats"
16+
)
17+
18+
def send_message(self, chat_id, message, parse_mode="HTML"):
19+
"""Low-level method to send a raw message via Telegram API"""
20+
self.ensure_one()
21+
if self.gateway_type != "telegram":
22+
return False
23+
24+
url = f"https://api.telegram.org/bot{self.token}/sendMessage"
25+
payload = {"chat_id": chat_id, "text": message, "parse_mode": parse_mode}
26+
try:
27+
with requests.Session() as session:
28+
response = session.post(url, json=payload, timeout=10)
29+
response.raise_for_status()
30+
return True
31+
except Exception as e:
32+
_logger.error("Telegram error for bot %s: %s", self.name, e)
33+
return False
34+
35+
def action_test_connection(self):
36+
"""Button to test connection to all registered simple chats"""
37+
self.ensure_one()
38+
if self.gateway_type != "telegram":
39+
return False
40+
41+
if not self.telegram_chat_ids:
42+
raise UserError(_("Please add or fetch at least one Chat ID first."))
43+
44+
for chat in self.telegram_chat_ids:
45+
msg = (
46+
_("<b>Success!</b> Connection from Odoo to <i>%s</i> is working.")
47+
% self.name
48+
)
49+
self.send_message(chat.chat_id, msg)
50+
51+
return {
52+
"effect": {
53+
"fadeout": "slow",
54+
"message": _("Test messages sent!"),
55+
"type": "rainbow_man",
56+
}
57+
}
58+
59+
def action_fetch_chats(self):
60+
"""Automatically discovers Chat IDs of people who messaged the bot"""
61+
self.ensure_one()
62+
if self.gateway_type != "telegram":
63+
return False
64+
65+
if self.webhook_key:
66+
raise UserError(
67+
_(
68+
"Telegram does not allow fetching updates manually "
69+
"while a Webhook is active. "
70+
"Please disable the Webhook before using 'Fetch Chats'."
71+
)
72+
)
73+
74+
url = f"https://api.telegram.org/bot{self.token}/getUpdates"
75+
try:
76+
response = requests.get(url, timeout=10)
77+
response.raise_for_status()
78+
data = response.json()
79+
80+
if not data.get("ok"):
81+
return False
82+
83+
for result in data.get("result", []):
84+
msg = result.get("message") or result.get("edited_message")
85+
if not msg:
86+
continue
87+
88+
chat_info = msg.get("chat")
89+
c_id = str(chat_info.get("id"))
90+
c_name = (
91+
chat_info.get("username")
92+
or chat_info.get("first_name")
93+
or "Unknown"
94+
)
95+
96+
if not self.telegram_chat_ids.filtered(
97+
lambda c, c_id=c_id: c.chat_id == c_id
98+
):
99+
self.env["telegram.chat"].create(
100+
{
101+
"name": c_name,
102+
"chat_id": c_id,
103+
"gateway_id": self.id,
104+
}
105+
)
106+
return True
107+
except Exception as e:
108+
_logger.error("Fetch failed: %s", e)
109+
return False
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from odoo import fields, models
2+
3+
4+
class TelegramChat(models.Model):
5+
_name = "telegram.chat"
6+
_description = "Telegram Chat"
7+
8+
name = fields.Char(
9+
required=True, help="Friendly name for the chat (e.g. Admin Group)"
10+
)
11+
chat_id = fields.Char(required=True, help="Numeric ID from Telegram")
12+
gateway_id = fields.Many2one(
13+
"mail.gateway", ondelete="cascade", string="Mail Gateway"
14+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* `Anmol Garg <https://github.com/AnmollGarg>`_:
2+
* Anmol Garg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This module provides a generic engine to integrate Telegram Bots with Odoo.
2+
It serves as a foundation for other modules to send notifications,
3+
allowing you to manage bot tokens and authorized chat IDs in a centralized way.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
To use this module:
2+
3+
1. Go to **Settings > Technical > Email > Gateways**.
4+
2. Create a new Gateway, set the Gateway Type to "Telegram", and enter the Token provided by @BotFather.
5+
3. Click the **Fetch Chats** button to automatically find Chat IDs of people who have messaged the bot.
6+
4. Click **Test Connection** to verify that Odoo can communicate with Telegram.

0 commit comments

Comments
 (0)