|
| 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 |
0 commit comments