import requests import clipboard from odoo import models, fields, api class WebsiteTelegram(models.Model): _name = 'website.telegram' _description = 'Telegram Channel' name = fields.Char(string='Bot Name', required=True) bot_name = fields.Char(string='Bot Token', help="Create your bot from Bot Father form Telegram App", required=True, ) python_code = fields.Char(compute="_calc_python_code") chatID = fields.Char(string='Channel Name', required=True, help="Create yor channel , Add members , Add Bot As an Admin") test_message = fields.Char(string='Test Message', required=False, default='Test') def send_to_telegram(self, message): # if self.env.all.registry.db_name == 'odoo016':return # message = self.env.all.registry.db_name + " : " + message # message = self.test_message apiURL = f'https://api.telegram.org/bot{self.bot_name}/sendMessage' try: requests.post(apiURL, json={'chat_id': self.chatID, 'text': message}) except Exception as e: print(e) def test_send(self): try: self.env.cr.savepoint() #kembalikan database ke save point jika mengalami kesalahan self.env['website.telegram'].search([('name', '=', self.name)])[0].send_to_telegram(self.test_message) except Exception as e: print(e) @api.depends('name', 'chatID', 'bot_name') def _calc_python_code(self): for record in self: if not record.name: record.python_code = "pleas put a name" elif not record.test_message: record.test_message = "test" else: record.python_code = "self.env['website.telegram'].search([('name', '=', '" + record.name + "')])[0].send_to_telegram('" + record.test_message + "')" def copy_chat_id(self): record = self.browse(self.id) clipboard.copy(record.python_code) def paste_chat_id(self): record = self.browse(self.id) chat_id = clipboard.paste() record.write({'chatID': chat_id}) def paste_bot_name(self): record = self.browse(self.id) bot_name = clipboard.paste() record.write({'bot_name': bot_name}) def get_updates(self): apiURL = f'https://api.telegram.org/bot{self.bot_name}/getUpdates' try: response = requests.get(apiURL) updates = response.json() return updates # Ini akan berisi semua pembaruan, termasuk pesan baru except Exception as e: print(e) return None def receive_messages(self): updates = self.get_updates() if updates and 'result' in updates: for update in updates['result']: if 'text' in update['channel_post']: message_text = update['channel_post']['text'] chat_id = update['channel_post']['chat']['id'] chat_name = update['channel_post']['chat']['username'] print(f"Received message: {message_text} from chat_id: {chat_id}, that is: {chat_name}")