From cc862f2bb8a6a751612bab7fb2d07f7306f9a4fd Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 4 Oct 2024 12:28:29 +0700 Subject: add telegram --- indoteknik_custom/__manifest__.py | 1 + indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/website_telegram.py | 63 ++++++++++++++++++++++++++ indoteknik_custom/security/ir.model.access.csv | 1 + indoteknik_custom/views/website_telegram.xml | 62 +++++++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 indoteknik_custom/models/website_telegram.py create mode 100644 indoteknik_custom/views/website_telegram.xml diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index e1a67592..3104b9a3 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -59,6 +59,7 @@ 'views/website_brand_homepage.xml', 'views/website_categories_homepage.xml', 'views/website_categories_management.xml', + 'views/website_telegram.xml', 'views/website_categories_lob.xml', 'views/sales_target.xml', 'views/purchase_outstanding.xml', diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 3d700ce0..1620c82e 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -128,3 +128,4 @@ from . import sales_order_reject from . import approval_date_doc from . import account_tax from . import approval_unreserve +from . import website_telegram diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py new file mode 100644 index 00000000..8fc6a8ea --- /dev/null +++ b/indoteknik_custom/models/website_telegram.py @@ -0,0 +1,63 @@ +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() + + 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}) + + + diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 19e3bdca..84147e04 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -24,6 +24,7 @@ access_website_categories_homepage,access.website.categories.homepage,model_webs access_website_categories_lob,access.website.categories.lob,model_website_categories_lob,,1,1,1,1 access_website_categories_management,access.website.categories.management,model_website_categories_management,,1,1,1,1 access_website_categories_management_line,access.website.categories.management.line,model_website_categories_management_line,,1,1,1,1 +access_website_telegram,access.website.telegram,model_website_telegram,,1,1,1,1 access_sales_target,access.sales.target,model_sales_target,,1,1,1,1 access_purchase_outstanding,access.purchase.outstanding,model_purchase_outstanding,,1,1,1,1 access_sales_outstanding,access.sales.outstanding,model_sales_outstanding,,1,1,1,1 diff --git a/indoteknik_custom/views/website_telegram.xml b/indoteknik_custom/views/website_telegram.xml new file mode 100644 index 00000000..9698bfc4 --- /dev/null +++ b/indoteknik_custom/views/website_telegram.xml @@ -0,0 +1,62 @@ + + + + + Channel Telegram + website.telegram + tree,form + + + + Channel Telegram + website.telegram + + + + +