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/models/__init__.py | 1 + indoteknik_custom/models/website_telegram.py | 63 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 indoteknik_custom/models/website_telegram.py (limited to 'indoteknik_custom/models') 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}) + + + -- cgit v1.2.3 From ec1e2331be248a505d89c00244d6b0fe5bd61c26 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 4 Oct 2024 14:31:33 +0700 Subject: add get text from channel --- indoteknik_custom/models/website_telegram.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 8fc6a8ea..bb3031a0 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -29,7 +29,7 @@ class WebsiteTelegram(models.Model): def test_send(self): try: - self.env.cr.savepoint() + 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: @@ -59,5 +59,24 @@ class WebsiteTelegram(models.Model): 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}") -- cgit v1.2.3 From 04d994e53a36ea9a6f7a2c0dd3f5f1b2c0ca9a74 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 8 Oct 2024 15:37:58 +0700 Subject: update code --- indoteknik_custom/models/website_telegram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index bb3031a0..5684eff8 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -19,7 +19,7 @@ class WebsiteTelegram(models.Model): # if self.env.all.registry.db_name == 'odoo016':return # message = self.env.all.registry.db_name + " : " + message - message = self.test_message + # message = self.test_message apiURL = f'https://api.telegram.org/bot{self.bot_name}/sendMessage' try: -- cgit v1.2.3 From 94141d0871e9abed968d3c249eeaf7b3c1ae87d9 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 9 Oct 2024 15:59:34 +0700 Subject: update telegram create channel --- indoteknik_custom/models/stock_picking.py | 7 +++++ indoteknik_custom/models/website_telegram.py | 39 +++++++++++++++++++++------- 2 files changed, 37 insertions(+), 9 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 14190474..5397b59f 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -4,6 +4,7 @@ from odoo.tools.float_utils import float_is_zero from datetime import datetime from itertools import groupby import pytz, datetime +from odoo.http import request class StockPicking(models.Model): @@ -310,11 +311,17 @@ class StockPicking(models.Model): self.approval_receipt_status = 'pengajuan1' def ask_return_approval(self): + telegram_data = { + 'tittle': 'Permintaan retur ' + self.name, + 'about': 'Permintaan retur ' + self.name, + } + telegram = request.env['website.telegram'].create(telegram_data) for pick in self: if self.env.user.is_accounting: pick.approval_return_status = 'approved' else: pick.approval_return_status = 'pengajuan1' + telegram.create_channel() def calculate_line_no(self): for picking in self: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 5684eff8..c0e7be2f 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -1,19 +1,40 @@ import requests - import clipboard - from odoo import models, fields, api +from telethon.sessions import StringSession +from telethon.sync import TelegramClient +from telethon import functions, types +from telethon.tl.types import InputChannel, InputPeerChannel +import asyncio 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') + tittle = fields.Char("Channel Title") + about = fields.Char("Channel Description") + is_broadcast = fields.Boolean("Is Broadcast", default=True) + is_megagroup = fields.Boolean("Is Megagroup", default=False) + # session_string = 'MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIRyy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwvplUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCOj4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB' + + def create_channel(self, *args, **kwargs): + asyncio.run(self._async_create_channel()) + + async def _async_create_channel(self): + async with TelegramClient(StringSession('1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), 27799517, 'df8ee44b0ed11108245037d47b511201') as client: + result = await client(functions.channels.CreateChannelRequest( + title=self.tittle, + about=self.about, + broadcast=True, + megagroup=False, + )) + channel = result.updates[3].message.peer_id + username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] + for name in username_to_add: + user_to_add = await client.get_entity(name) + result = await client(functions.channels.InviteToChannelRequest( + channel=channel, + users=[user_to_add.id], + )) def send_to_telegram(self, message): # if self.env.all.registry.db_name == 'odoo016':return -- cgit v1.2.3 From dcbb5680c9dd6ecf18eaf3dbbc3af26ac7a47134 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 9 Oct 2024 17:00:18 +0700 Subject: update bisa kirim chat --- indoteknik_custom/models/stock_picking.py | 1 + indoteknik_custom/models/website_telegram.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 5397b59f..b65d06e4 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -314,6 +314,7 @@ class StockPicking(models.Model): telegram_data = { 'tittle': 'Permintaan retur ' + self.name, 'about': 'Permintaan retur ' + self.name, + 'id_data': self.id } telegram = request.env['website.telegram'].create(telegram_data) for pick in self: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index c0e7be2f..45927b73 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -6,11 +6,13 @@ from telethon.sync import TelegramClient from telethon import functions, types from telethon.tl.types import InputChannel, InputPeerChannel import asyncio +from telethon.tl.functions.messages import SendMessageRequest class WebsiteTelegram(models.Model): _name = 'website.telegram' _description = 'Telegram Channel' tittle = fields.Char("Channel Title") + id_data = fields.Char("Channel ID") about = fields.Char("Channel Description") is_broadcast = fields.Boolean("Is Broadcast", default=True) is_megagroup = fields.Boolean("Is Megagroup", default=False) @@ -24,8 +26,8 @@ class WebsiteTelegram(models.Model): result = await client(functions.channels.CreateChannelRequest( title=self.tittle, about=self.about, - broadcast=True, - megagroup=False, + broadcast=False, + megagroup=True, )) channel = result.updates[3].message.peer_id username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] @@ -35,6 +37,8 @@ class WebsiteTelegram(models.Model): channel=channel, users=[user_to_add.id], )) + message = 'https://erp.indoteknik.com/web#id='+self.id_data+'&action=209&model=stock.picking&view_type=form&cids=1&menu_id=101' + result = await client(SendMessageRequest(channel, message)) def send_to_telegram(self, message): # if self.env.all.registry.db_name == 'odoo016':return -- cgit v1.2.3 From 9c6e2e8f35f14dda376e543e5b63b0bddee2fd88 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 10 Oct 2024 08:53:39 +0700 Subject: update telegram --- indoteknik_custom/models/stock_picking.py | 3 +- indoteknik_custom/models/website_telegram.py | 41 +++++++++++++++++++++------- 2 files changed, 33 insertions(+), 11 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index b65d06e4..58eb5d82 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -314,7 +314,8 @@ class StockPicking(models.Model): telegram_data = { 'tittle': 'Permintaan retur ' + self.name, 'about': 'Permintaan retur ' + self.name, - 'id_data': self.id + 'id_data': self.id, + 'username': '@'+self.name.replace('/', '') } telegram = request.env['website.telegram'].create(telegram_data) for pick in self: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 45927b73..d67cafc5 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -12,6 +12,7 @@ class WebsiteTelegram(models.Model): _name = 'website.telegram' _description = 'Telegram Channel' tittle = fields.Char("Channel Title") + username = fields.Char("Channel Name") id_data = fields.Char("Channel ID") about = fields.Char("Channel Description") is_broadcast = fields.Boolean("Is Broadcast", default=True) @@ -30,7 +31,8 @@ class WebsiteTelegram(models.Model): megagroup=True, )) channel = result.updates[3].message.peer_id - username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] + # username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] + username_to_add = ['@imsep81'] for name in username_to_add: user_to_add = await client.get_entity(name) result = await client(functions.channels.InviteToChannelRequest( @@ -40,6 +42,24 @@ class WebsiteTelegram(models.Model): message = 'https://erp.indoteknik.com/web#id='+self.id_data+'&action=209&model=stock.picking&view_type=form&cids=1&menu_id=101' result = await client(SendMessageRequest(channel, message)) + result = client(functions.channels.UpdateUsernameRequest( + channel=channel, + username='@indoJaya21' + )) + print(result.stringify()) + + def receive_messages(self): + asyncio.run(self._async_receive_messages()) + + async def _async_receive_messages(self): + async with TelegramClient(StringSession('1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), + 27799517, 'df8ee44b0ed11108245037d47b511201') as client: + channel = await client.get_entity(self.username) + result = client(functions.channels.GetMessagesRequest( + channel=channel, + id=channel.chats + )) + print(result.stringify()) def send_to_telegram(self, message): # if self.env.all.registry.db_name == 'odoo016':return @@ -94,14 +114,15 @@ class WebsiteTelegram(models.Model): 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}") + # 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}") + -- cgit v1.2.3 From c631620706a24426faea6b43c2a0602eebf9a5e4 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 11 Oct 2024 10:02:33 +0700 Subject: fix code get telegram api --- indoteknik_custom/models/stock_picking.py | 37 ++++++++--- indoteknik_custom/models/website_telegram.py | 92 +++++++++++++++++++--------- 2 files changed, 91 insertions(+), 38 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 58eb5d82..5f2fe99a 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -312,18 +312,39 @@ class StockPicking(models.Model): def ask_return_approval(self): telegram_data = { - 'tittle': 'Permintaan retur ' + self.name, + 'tittle': self.name, 'about': 'Permintaan retur ' + self.name, 'id_data': self.id, 'username': '@'+self.name.replace('/', '') } - telegram = request.env['website.telegram'].create(telegram_data) - for pick in self: - if self.env.user.is_accounting: - pick.approval_return_status = 'approved' - else: - pick.approval_return_status = 'pengajuan1' - telegram.create_channel() + channel_data = self.env['website.telegram'].search([('tittle', '=', self.name)]) + if channel_data: + for pick in self: + self._check_telegram(pick) + else: + telegram = request.env['website.telegram'].create(telegram_data) + telegram.create_channel() + for pick in self: + pick.approval_return_status = 'pengajuan1' + + def read(self, fields=None, load='_classic_read'): + # Panggil method 'read' bawaan terlebih dahulu + records = super(StockPicking, self).read(fields, load) + + # Jalankan _check_telegram untuk setiap record yang diakses + for record in self: + if record.approval_return_status == 'pengajuan1': + record._check_telegram() + + return records + + + def _check_telegram(self): + telegram = self.env['website.telegram'].search([('id_data', '=', self.id)]) + if telegram: + ask_return = telegram.receive_messages() + if ask_return: + self.approval_return_status = 'approved' def calculate_line_no(self): for picking in self: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index d67cafc5..8bd789a1 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -4,68 +4,103 @@ from odoo import models, fields, api from telethon.sessions import StringSession from telethon.sync import TelegramClient from telethon import functions, types -from telethon.tl.types import InputChannel, InputPeerChannel +from telethon.tl.types import InputPeerChannel import asyncio from telethon.tl.functions.messages import SendMessageRequest +from telethon.tl.types import InputMediaPoll, Poll, PollAnswer, PeerChannel +import datetime +from telethon.tl.types import MessageMediaPoll +from telethon.tl.functions.messages import SendMediaRequest +from telethon.tl.types import TextWithEntities class WebsiteTelegram(models.Model): _name = 'website.telegram' _description = 'Telegram Channel' tittle = fields.Char("Channel Title") + invite_link = fields.Char("Channel invite link") username = fields.Char("Channel Name") id_data = fields.Char("Channel ID") about = fields.Char("Channel Description") is_broadcast = fields.Boolean("Is Broadcast", default=True) is_megagroup = fields.Boolean("Is Megagroup", default=False) + is_accept = fields.Boolean("Is Megagroup", default=False) + # session_string = 'MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIRyy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwvplUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCOj4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB' def create_channel(self, *args, **kwargs): asyncio.run(self._async_create_channel()) async def _async_create_channel(self): - async with TelegramClient(StringSession('1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), 27799517, 'df8ee44b0ed11108245037d47b511201') as client: + async with TelegramClient(StringSession( + '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), + 27799517, 'df8ee44b0ed11108245037d47b511201') as client: result = await client(functions.channels.CreateChannelRequest( - title=self.tittle, + title='Permintaan retur ' + self.tittle, about=self.about, broadcast=False, megagroup=True, )) channel = result.updates[3].message.peer_id - # username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] - username_to_add = ['@imsep81'] + channel_link = await client(functions.messages.ExportChatInviteRequest( + peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), + )) + self.invite_link = channel_link.link + username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] + # username_to_add = ['@imsep81'] for name in username_to_add: user_to_add = await client.get_entity(name) - result = await client(functions.channels.InviteToChannelRequest( + result_add_user = await client(functions.channels.InviteToChannelRequest( channel=channel, users=[user_to_add.id], )) - message = 'https://erp.indoteknik.com/web#id='+self.id_data+'&action=209&model=stock.picking&view_type=form&cids=1&menu_id=101' - result = await client(SendMessageRequest(channel, message)) - - result = client(functions.channels.UpdateUsernameRequest( - channel=channel, - username='@indoJaya21' + message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=stock.picking&view_type=form&cids=1&menu_id=101' + result_massage = await client(SendMessageRequest(channel, message)) + + # Send the poll to the channel using PeerChannel with the channel's ID + result_polling = await client(SendMediaRequest( + peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), + media=InputMediaPoll(poll=Poll( + question=TextWithEntities(text="ASK RETURN?", entities=[]), + answers=[ + PollAnswer(text=TextWithEntities(text="Accept", entities=[]), option=b'\x01'), + PollAnswer(text=TextWithEntities(text="Reject", entities=[]), option=b'\x02'), + ], + id=2 + )), + message='Ask Return Polling' )) - print(result.stringify()) def receive_messages(self): - asyncio.run(self._async_receive_messages()) + return asyncio.run(self._async_receive_messages()) async def _async_receive_messages(self): - async with TelegramClient(StringSession('1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), - 27799517, 'df8ee44b0ed11108245037d47b511201') as client: - channel = await client.get_entity(self.username) - result = client(functions.channels.GetMessagesRequest( - channel=channel, - id=channel.chats + async with TelegramClient(StringSession( + '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), + 27799517, 'df8ee44b0ed11108245037d47b511201') as client: + channel = await client.get_entity(self.invite_link) + result = await client(functions.messages.GetHistoryRequest( + peer=channel, + offset_id=42, + offset_date=datetime.datetime(2024, 12, 31), + add_offset=0, + limit=100, + max_id=0, + min_id=0, + hash=channel.access_hash )) - print(result.stringify()) - def send_to_telegram(self, message): - # if self.env.all.registry.db_name == 'odoo016':return + accept_found = False + + for message in result.messages: + if not isinstance(message.media, MessageMediaPoll): + continue + if message.media.results.total_voters > 0: + if message.media.results.results[0].voters > message.media.results.results[1].voters: + accept_found = True + break - # message = self.env.all.registry.db_name + " : " + message - # message = self.test_message + return accept_found + def send_to_telegram(self, message): apiURL = f'https://api.telegram.org/bot{self.bot_name}/sendMessage' try: requests.post(apiURL, json={'chat_id': self.chatID, 'text': message}) @@ -74,7 +109,7 @@ class WebsiteTelegram(models.Model): def test_send(self): try: - self.env.cr.savepoint() #kembalikan database ke save point jika mengalami kesalahan + self.env.cr.savepoint() self.env['website.telegram'].search([('name', '=', self.name)])[0].send_to_telegram(self.test_message) except Exception as e: @@ -109,7 +144,7 @@ class WebsiteTelegram(models.Model): try: response = requests.get(apiURL) updates = response.json() - return updates # Ini akan berisi semua pembaruan, termasuk pesan baru + return updates except Exception as e: print(e) return None @@ -123,6 +158,3 @@ class WebsiteTelegram(models.Model): # 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}") - - - -- cgit v1.2.3 From 6ad0aa164ed76c8889fd76bff99f5d57b1ac1b2e Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 4 Dec 2024 15:31:54 +0700 Subject: add telegram --- indoteknik_custom/models/sale_order.py | 14 ++++++++++++++ indoteknik_custom/models/website_telegram.py | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 7fc6d96a..88bbe3cf 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -145,6 +145,20 @@ class SaleOrder(models.Model): ('NP', 'Non Pareto') ]) + def action_print_proforma_invoice(self): + """Cetak laporan Jasper secara langsung""" + self.ensure_one() + # Konfigurasi laporan Jasper + report_action = self.env['ir.actions.report'].search([ + ('model', '=', 'sale.order'), + ('report_name', '=', 'Proforma Invoice New') + ], limit=1) + + if report_action: + return report_action.report_action(self) + else: + raise ValueError('Report not found!') + @api.onchange('payment_status') def _is_continue_transaction(self): if not self.is_continue_transaction: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 8bd789a1..e17da371 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -45,8 +45,8 @@ class WebsiteTelegram(models.Model): peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), )) self.invite_link = channel_link.link - username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] - # username_to_add = ['@imsep81'] + # username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] + username_to_add = ['@imsep81'] for name in username_to_add: user_to_add = await client.get_entity(name) result_add_user = await client(functions.channels.InviteToChannelRequest( -- cgit v1.2.3 From bab6a911535b771e988093725e0ba378c77297c6 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 10 Dec 2024 08:58:25 +0700 Subject: update telegram --- indoteknik_custom/models/sale_order.py | 14 -------------- indoteknik_custom/models/website_telegram.py | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 88bbe3cf..7fc6d96a 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -145,20 +145,6 @@ class SaleOrder(models.Model): ('NP', 'Non Pareto') ]) - def action_print_proforma_invoice(self): - """Cetak laporan Jasper secara langsung""" - self.ensure_one() - # Konfigurasi laporan Jasper - report_action = self.env['ir.actions.report'].search([ - ('model', '=', 'sale.order'), - ('report_name', '=', 'Proforma Invoice New') - ], limit=1) - - if report_action: - return report_action.report_action(self) - else: - raise ValueError('Report not found!') - @api.onchange('payment_status') def _is_continue_transaction(self): if not self.is_continue_transaction: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index e17da371..58816fe2 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -46,7 +46,7 @@ class WebsiteTelegram(models.Model): )) self.invite_link = channel_link.link # username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] - username_to_add = ['@imsep81'] + username_to_add = ['@radiant81'] for name in username_to_add: user_to_add = await client.get_entity(name) result_add_user = await client(functions.channels.InviteToChannelRequest( -- cgit v1.2.3 From 1bf746c93dc5010e30490d0a5ed6a2a174726be0 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 10 Dec 2024 13:21:14 +0700 Subject: update code telegram print so when amount > 200jt --- indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/ir_actions_report.py | 29 ++++++++++++++++++ indoteknik_custom/models/stock_picking.py | 34 --------------------- indoteknik_custom/models/website_telegram.py | 43 ++++++++++++++++----------- 4 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 indoteknik_custom/models/ir_actions_report.py (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 1e0cedc9..b319883c 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -135,3 +135,4 @@ from . import find_page from . import approval_retur_picking from . import va_multi_approve from . import va_multi_reject +from . import ir_actions_report diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py new file mode 100644 index 00000000..d620f8a7 --- /dev/null +++ b/indoteknik_custom/models/ir_actions_report.py @@ -0,0 +1,29 @@ +from odoo import models +import requests +class IrActionsReport(models.Model): + _inherit = 'ir.actions.report' + + def _get_readable_fields(self): + self.send_to_telegram() + return super()._get_readable_fields() + + def send_to_telegram(self): + so_id = self.env.context.get('active_id') + if so_id: + sale_order = self.env['sale.order'].browse(so_id) + if sale_order.amount_total < 200000000: + return + telegram_data = { + 'tittle': sale_order.name, + 'about': 'Permintaan retur ' + sale_order.name, + 'id_data': sale_order.id, + 'username': '@' + sale_order.name.replace('/', '') + } + channel_data = self.env['website.telegram'].search([('tittle', '=', sale_order.name)]) + if channel_data: + channel_data.send_to_telegram(sale_order.name + " Telah di print") + for pick in self: + self._check_telegram(pick) + else: + telegram = self.env['website.telegram'].create(telegram_data) + telegram.create_channel() diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 2095e957..31c45531 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -468,40 +468,6 @@ class StockPicking(models.Model): raise UserError('Harus Sales Admin yang Ask Return') else: raise UserError('Harus Purchasing yang Ask Return') - telegram_data = { - 'tittle': self.name, - 'about': 'Permintaan retur ' + self.name, - 'id_data': self.id, - 'username': '@'+self.name.replace('/', '') - } - channel_data = self.env['website.telegram'].search([('tittle', '=', self.name)]) - if channel_data: - for pick in self: - self._check_telegram(pick) - else: - telegram = request.env['website.telegram'].create(telegram_data) - telegram.create_channel() - for pick in self: - pick.approval_return_status = 'pengajuan1' - - def read(self, fields=None, load='_classic_read'): - # Panggil method 'read' bawaan terlebih dahulu - records = super(StockPicking, self).read(fields, load) - - # Jalankan _check_telegram untuk setiap record yang diakses - for record in self: - if record.approval_return_status == 'pengajuan1': - record._check_telegram() - - return records - - - def _check_telegram(self): - telegram = self.env['website.telegram'].search([('id_data', '=', self.id)]) - if telegram: - ask_return = telegram.receive_messages() - if ask_return: - self.approval_return_status = 'approved' def calculate_line_no(self): diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 58816fe2..92707ea2 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -35,7 +35,7 @@ class WebsiteTelegram(models.Model): '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), 27799517, 'df8ee44b0ed11108245037d47b511201') as client: result = await client(functions.channels.CreateChannelRequest( - title='Permintaan retur ' + self.tittle, + title=self.tittle + ' Telah Diprint', about=self.about, broadcast=False, megagroup=True, @@ -45,30 +45,30 @@ class WebsiteTelegram(models.Model): peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), )) self.invite_link = channel_link.link - # username_to_add = ['@imsep81', '6285764475716', '@stephanchrst'] - username_to_add = ['@radiant81'] + # username_to_add = ['@radiant81', '6285764475716', '@stephanchrst'] + username_to_add = ['6282339129611'] for name in username_to_add: user_to_add = await client.get_entity(name) result_add_user = await client(functions.channels.InviteToChannelRequest( channel=channel, users=[user_to_add.id], )) - message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=stock.picking&view_type=form&cids=1&menu_id=101' + message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=sale.order&view_type=form&cids=1&menu_id=101' result_massage = await client(SendMessageRequest(channel, message)) # Send the poll to the channel using PeerChannel with the channel's ID - result_polling = await client(SendMediaRequest( - peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), - media=InputMediaPoll(poll=Poll( - question=TextWithEntities(text="ASK RETURN?", entities=[]), - answers=[ - PollAnswer(text=TextWithEntities(text="Accept", entities=[]), option=b'\x01'), - PollAnswer(text=TextWithEntities(text="Reject", entities=[]), option=b'\x02'), - ], - id=2 - )), - message='Ask Return Polling' - )) + # result_polling = await client(SendMediaRequest( + # peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), + # media=InputMediaPoll(poll=Poll( + # question=TextWithEntities(text="ASK RETURN?", entities=[]), + # answers=[ + # PollAnswer(text=TextWithEntities(text="Accept", entities=[]), option=b'\x01'), + # PollAnswer(text=TextWithEntities(text="Reject", entities=[]), option=b'\x02'), + # ], + # id=2 + # )), + # message='Ask Return Polling' + # )) def receive_messages(self): return asyncio.run(self._async_receive_messages()) @@ -101,12 +101,19 @@ class WebsiteTelegram(models.Model): return accept_found def send_to_telegram(self, message): - apiURL = f'https://api.telegram.org/bot{self.bot_name}/sendMessage' + # apiURL = f'https://api.telegram.org/bot{self.bot_name}/sendMessage' try: - requests.post(apiURL, json={'chat_id': self.chatID, 'text': message}) + # requests.post(apiURL, json={'chat_id': self.chatID, 'text': message}) + asyncio.run(self._async_send_to_telegram(message)) except Exception as e: print(e) + async def _async_send_to_telegram(self, message): + async with TelegramClient(StringSession( + '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), + 27799517, 'df8ee44b0ed11108245037d47b511201') as client: + channel = await client.get_entity(self.invite_link) + result_massage = await client(SendMessageRequest(channel, message)) def test_send(self): try: self.env.cr.savepoint() -- cgit v1.2.3 From e81db305d2727e7b95f1bb579315da1597a8185b Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Sat, 14 Dec 2024 09:47:33 +0700 Subject: update send message to telegram --- indoteknik_custom/models/ir_actions_report.py | 4 ++-- indoteknik_custom/models/website_telegram.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py index d620f8a7..286c3a3d 100644 --- a/indoteknik_custom/models/ir_actions_report.py +++ b/indoteknik_custom/models/ir_actions_report.py @@ -15,13 +15,13 @@ class IrActionsReport(models.Model): return telegram_data = { 'tittle': sale_order.name, - 'about': 'Permintaan retur ' + sale_order.name, + 'about': sale_order.name + ' Telah Di Print', 'id_data': sale_order.id, 'username': '@' + sale_order.name.replace('/', '') } channel_data = self.env['website.telegram'].search([('tittle', '=', sale_order.name)]) if channel_data: - channel_data.send_to_telegram(sale_order.name + " Telah di print") + channel_data.send_to_telegram(sale_order.name + " Telah di print Oleh " + self.env.user.name) for pick in self: self._check_telegram(pick) else: diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 92707ea2..25dea14e 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -54,7 +54,9 @@ class WebsiteTelegram(models.Model): users=[user_to_add.id], )) message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=sale.order&view_type=form&cids=1&menu_id=101' + message2 = self.tittle + ' di print oleh ' + self.env.user.name result_massage = await client(SendMessageRequest(channel, message)) + result_massage2 = await client(SendMessageRequest(channel, message2)) # Send the poll to the channel using PeerChannel with the channel's ID # result_polling = await client(SendMediaRequest( -- cgit v1.2.3 From 05d8edef111df1f32be76f3fdfaabfb1c8057644 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 16 Dec 2024 15:05:10 +0700 Subject: update telegram --- indoteknik_custom/models/ir_actions_report.py | 2 +- indoteknik_custom/models/website_telegram.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py index 286c3a3d..f6ef4e3e 100644 --- a/indoteknik_custom/models/ir_actions_report.py +++ b/indoteknik_custom/models/ir_actions_report.py @@ -15,7 +15,7 @@ class IrActionsReport(models.Model): return telegram_data = { 'tittle': sale_order.name, - 'about': sale_order.name + ' Telah Di Print', + 'about': sale_order.name, 'id_data': sale_order.id, 'username': '@' + sale_order.name.replace('/', '') } diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 25dea14e..a3c64c7e 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -16,9 +16,13 @@ from telethon.tl.types import TextWithEntities class WebsiteTelegram(models.Model): _name = 'website.telegram' _description = 'Telegram Channel' + + tittle = fields.Char("Channel Title") invite_link = fields.Char("Channel invite link") username = fields.Char("Channel Name") + username_to_add = fields.Char("username to add on channel", compute='_compute_username_to_add') + user_id = fields.Many2many('res.partner', string='User Member', store=True) id_data = fields.Char("Channel ID") about = fields.Char("Channel Description") is_broadcast = fields.Boolean("Is Broadcast", default=True) @@ -27,6 +31,11 @@ class WebsiteTelegram(models.Model): # session_string = 'MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIRyy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwvplUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCOj4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB' + @api.depends('user_id') + def _compute_username_to_add(self): + for name in self.user_id: + self.username_to_add.append(str(name.mobile)) + def create_channel(self, *args, **kwargs): asyncio.run(self._async_create_channel()) @@ -35,7 +44,7 @@ class WebsiteTelegram(models.Model): '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), 27799517, 'df8ee44b0ed11108245037d47b511201') as client: result = await client(functions.channels.CreateChannelRequest( - title=self.tittle + ' Telah Diprint', + title=self.tittle, about=self.about, broadcast=False, megagroup=True, @@ -115,12 +124,21 @@ class WebsiteTelegram(models.Model): '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), 27799517, 'df8ee44b0ed11108245037d47b511201') as client: channel = await client.get_entity(self.invite_link) + + for name in self.username_to_add: + data = name + user_to_add = await client.get_entity(data) + result_add_user = await client(functions.channels.InviteToChannelRequest( + channel=channel, + users=[user_to_add.id], + )) result_massage = await client(SendMessageRequest(channel, message)) + def test_send(self): try: self.env.cr.savepoint() - self.env['website.telegram'].search([('name', '=', self.name)])[0].send_to_telegram(self.test_message) + self.env['website.telegram'].search([('tittle', '=', self.tittle)])[0].send_to_telegram('test message') except Exception as e: print(e) -- cgit v1.2.3 From 9ba922eded6d752c2a4299c9c444238309877bb9 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Dec 2024 09:39:47 +0700 Subject: update code --- indoteknik_custom/models/ir_actions_report.py | 1 + indoteknik_custom/models/website_telegram.py | 38 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py index f6ef4e3e..b7d4e372 100644 --- a/indoteknik_custom/models/ir_actions_report.py +++ b/indoteknik_custom/models/ir_actions_report.py @@ -16,6 +16,7 @@ class IrActionsReport(models.Model): telegram_data = { 'tittle': sale_order.name, 'about': sale_order.name, + 'user_id': sale_order, 'id_data': sale_order.id, 'username': '@' + sale_order.name.replace('/', '') } diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index a3c64c7e..ade6c326 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -33,8 +33,13 @@ class WebsiteTelegram(models.Model): @api.depends('user_id') def _compute_username_to_add(self): - for name in self.user_id: - self.username_to_add.append(str(name.mobile)) + for record in self: + usernames = [] + for partner in record.user_id: + if partner.mobile: # Pastikan mobile tidak None + usernames.append(partner.mobile) + # Gabungkan semua nomor menjadi satu string, dipisahkan koma + record.username_to_add = ', '.join(usernames) def create_channel(self, *args, **kwargs): asyncio.run(self._async_create_channel()) @@ -125,14 +130,27 @@ class WebsiteTelegram(models.Model): 27799517, 'df8ee44b0ed11108245037d47b511201') as client: channel = await client.get_entity(self.invite_link) - for name in self.username_to_add: - data = name - user_to_add = await client.get_entity(data) - result_add_user = await client(functions.channels.InviteToChannelRequest( - channel=channel, - users=[user_to_add.id], - )) - result_massage = await client(SendMessageRequest(channel, message)) + # Memproses username_to_add menjadi list + usernames = self.username_to_add.split(', ') # Pisahkan berdasarkan koma dan spasi + + # Iterasi untuk setiap username + for username in usernames: + if username: # Pastikan username tidak kosong + try: + # Mendapatkan entitas user berdasarkan username + user_to_add = await client.get_entity(username) + + # Mengundang user ke channel + result_add_user = await client(functions.channels.InviteToChannelRequest( + channel=channel, + users=[user_to_add.id], + )) + except Exception as e: + # Tangani error (misal user tidak ditemukan atau sudah ada di channel) + print(f"Error adding user {username}: {e}") + + # Mengirim pesan ke channel + result_message = await client(SendMessageRequest(channel, message)) def test_send(self): try: -- cgit v1.2.3 From b4a8de6bd7ca7051eee16f877b4e5d8fd65e3056 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Dec 2024 13:57:52 +0700 Subject: add telegram --- indoteknik_custom/models/ir_actions_report.py | 6 ++++-- indoteknik_custom/models/res_partner.py | 1 + indoteknik_custom/models/website_telegram.py | 26 ++++++++++---------------- 3 files changed, 15 insertions(+), 18 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py index b7d4e372..7b1dcee8 100644 --- a/indoteknik_custom/models/ir_actions_report.py +++ b/indoteknik_custom/models/ir_actions_report.py @@ -1,5 +1,5 @@ from odoo import models -import requests +from odoo.http import request class IrActionsReport(models.Model): _inherit = 'ir.actions.report' @@ -13,10 +13,12 @@ class IrActionsReport(models.Model): sale_order = self.env['sale.order'].browse(so_id) if sale_order.amount_total < 200000000: return + # id ci vita 79160 + partner = request.env['res.partner'].search([('id', '=', 112718)], limit=1) telegram_data = { 'tittle': sale_order.name, 'about': sale_order.name, - 'user_id': sale_order, + 'user_id': partner, 'id_data': sale_order.id, 'username': '@' + sale_order.name.replace('/', '') } diff --git a/indoteknik_custom/models/res_partner.py b/indoteknik_custom/models/res_partner.py index da4a6cb6..c648c729 100644 --- a/indoteknik_custom/models/res_partner.py +++ b/indoteknik_custom/models/res_partner.py @@ -74,6 +74,7 @@ class ResPartner(models.Model): "customer is crossed blocking amount." "Set its value to 0.00 to disable " "this feature", tracking=3) + telegram_id = fields.Char(string="Telegram") @api.model def _default_payment_term(self): diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index ade6c326..7d76f27d 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -36,7 +36,7 @@ class WebsiteTelegram(models.Model): for record in self: usernames = [] for partner in record.user_id: - if partner.mobile: # Pastikan mobile tidak None + if partner.telegram_id: # Pastikan mobile tidak None usernames.append(partner.mobile) # Gabungkan semua nomor menjadi satu string, dipisahkan koma record.username_to_add = ', '.join(usernames) @@ -59,15 +59,16 @@ class WebsiteTelegram(models.Model): peer=InputPeerChannel(channel_id=channel.channel_id, access_hash=result.chats[0].access_hash), )) self.invite_link = channel_link.link - # username_to_add = ['@radiant81', '6285764475716', '@stephanchrst'] - username_to_add = ['6282339129611'] - for name in username_to_add: - user_to_add = await client.get_entity(name) + + # Iterasi untuk setiap username + for name in self.user_id: + user_to_add = await client.get_entity(name.telegram_id) result_add_user = await client(functions.channels.InviteToChannelRequest( channel=channel, users=[user_to_add.id], )) - message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=sale.order&view_type=form&cids=1&menu_id=101' + # message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=sale.order&view_type=form&cids=1&menu_id=101' + message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=357&model=sale.order&view_type=form&cids=1&menu_id=212' message2 = self.tittle + ' di print oleh ' + self.env.user.name result_massage = await client(SendMessageRequest(channel, message)) result_massage2 = await client(SendMessageRequest(channel, message2)) @@ -130,26 +131,19 @@ class WebsiteTelegram(models.Model): 27799517, 'df8ee44b0ed11108245037d47b511201') as client: channel = await client.get_entity(self.invite_link) - # Memproses username_to_add menjadi list - usernames = self.username_to_add.split(', ') # Pisahkan berdasarkan koma dan spasi - # Iterasi untuk setiap username - for username in usernames: - if username: # Pastikan username tidak kosong + for username in self.user_id: + if username: try: - # Mendapatkan entitas user berdasarkan username - user_to_add = await client.get_entity(username) + user_to_add = await client.get_entity(username.telegram_id) - # Mengundang user ke channel result_add_user = await client(functions.channels.InviteToChannelRequest( channel=channel, users=[user_to_add.id], )) except Exception as e: - # Tangani error (misal user tidak ditemukan atau sudah ada di channel) print(f"Error adding user {username}: {e}") - # Mengirim pesan ke channel result_message = await client(SendMessageRequest(channel, message)) def test_send(self): -- cgit v1.2.3 From 6a7b2e28c9c1612ac3e91ac321b72e3400fdb5a3 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Dec 2024 14:05:19 +0700 Subject: update --- indoteknik_custom/models/ir_actions_report.py | 2 +- indoteknik_custom/models/website_telegram.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py index 7b1dcee8..31bcf168 100644 --- a/indoteknik_custom/models/ir_actions_report.py +++ b/indoteknik_custom/models/ir_actions_report.py @@ -29,4 +29,4 @@ class IrActionsReport(models.Model): self._check_telegram(pick) else: telegram = self.env['website.telegram'].create(telegram_data) - telegram.create_channel() + telegram.create_channel(sale_order.name + " Telah di print Oleh " + self.env.user.name) diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 7d76f27d..74955b7b 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -41,10 +41,10 @@ class WebsiteTelegram(models.Model): # Gabungkan semua nomor menjadi satu string, dipisahkan koma record.username_to_add = ', '.join(usernames) - def create_channel(self, *args, **kwargs): - asyncio.run(self._async_create_channel()) + def create_channel(self, message): + asyncio.run(self._async_create_channel(message)) - async def _async_create_channel(self): + async def _async_create_channel(self, message): async with TelegramClient(StringSession( '1BVtsOJABu30MWCBFwYvvaYbFoIWi43r5a7TUZ2IWwrnSlXIwEhJS5k2y4UKjoDeMPKwhgUWn9lMk02zQjM0ZDzq61YyhkRBvZuu3hCxMsrtP92bkuZtL2g3g1VgI8s7rMhOD_WaGrZbuj-TmbTwIEbN5i1J4raDW2kYzmlLRCbT74xxrGjpzWCnVv7CSS9L2juXuut0lLMgli3_JZbqDO1IyBYh4ZFQYbMf7zv6moDR4MQp1qfnFArsikQcfxjlNXKFcSoyA_GjiIFfCuymwQVtdERXOAH03M_lm8fYbjvgxEkJvxR6hdCnYMcKpIujEEo9SmMmK7Vnl29g1TCPO5tlrDNXq3Ng='), 27799517, 'df8ee44b0ed11108245037d47b511201') as client: @@ -68,10 +68,10 @@ class WebsiteTelegram(models.Model): users=[user_to_add.id], )) # message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=sale.order&view_type=form&cids=1&menu_id=101' - message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=357&model=sale.order&view_type=form&cids=1&menu_id=212' + message_template = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=357&model=sale.order&view_type=form&cids=1&menu_id=212' message2 = self.tittle + ' di print oleh ' + self.env.user.name - result_massage = await client(SendMessageRequest(channel, message)) - result_massage2 = await client(SendMessageRequest(channel, message2)) + result_massage = await client(SendMessageRequest(channel, message_template)) + result_massage2 = await client(SendMessageRequest(channel, message)) # Send the poll to the channel using PeerChannel with the channel's ID # result_polling = await client(SendMediaRequest( -- cgit v1.2.3 From 1d5e4ca1fa58bcd59954a08b694f273d4c563cdf Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 31 Dec 2024 09:44:40 +0700 Subject: update telegram --- indoteknik_custom/models/website_telegram.py | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 74955b7b..290fd1f9 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -21,7 +21,6 @@ class WebsiteTelegram(models.Model): tittle = fields.Char("Channel Title") invite_link = fields.Char("Channel invite link") username = fields.Char("Channel Name") - username_to_add = fields.Char("username to add on channel", compute='_compute_username_to_add') user_id = fields.Many2many('res.partner', string='User Member', store=True) id_data = fields.Char("Channel ID") about = fields.Char("Channel Description") @@ -31,15 +30,6 @@ class WebsiteTelegram(models.Model): # session_string = 'MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIRyy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwvplUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCOj4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB' - @api.depends('user_id') - def _compute_username_to_add(self): - for record in self: - usernames = [] - for partner in record.user_id: - if partner.telegram_id: # Pastikan mobile tidak None - usernames.append(partner.mobile) - # Gabungkan semua nomor menjadi satu string, dipisahkan koma - record.username_to_add = ', '.join(usernames) def create_channel(self, message): asyncio.run(self._async_create_channel(message)) -- cgit v1.2.3 From 69b84d53a07b77aaca96f88ff4a3b24e52d48fc3 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 20 Jan 2025 12:57:18 +0700 Subject: update code --- indoteknik_custom/models/ir_actions_report.py | 3 ++- indoteknik_custom/models/website_telegram.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/ir_actions_report.py b/indoteknik_custom/models/ir_actions_report.py index 31bcf168..e6b9c303 100644 --- a/indoteknik_custom/models/ir_actions_report.py +++ b/indoteknik_custom/models/ir_actions_report.py @@ -14,7 +14,8 @@ class IrActionsReport(models.Model): if sale_order.amount_total < 200000000: return # id ci vita 79160 - partner = request.env['res.partner'].search([('id', '=', 112718)], limit=1) + # id iman 112718 + partner = request.env['res.partner'].search([('id', '=', 79160)], limit=1) telegram_data = { 'tittle': sale_order.name, 'about': sale_order.name, diff --git a/indoteknik_custom/models/website_telegram.py b/indoteknik_custom/models/website_telegram.py index 290fd1f9..c655143e 100644 --- a/indoteknik_custom/models/website_telegram.py +++ b/indoteknik_custom/models/website_telegram.py @@ -3,6 +3,7 @@ import clipboard from odoo import models, fields, api from telethon.sessions import StringSession from telethon.sync import TelegramClient +from odoo.exceptions import UserError from telethon import functions, types from telethon.tl.types import InputPeerChannel import asyncio @@ -52,11 +53,14 @@ class WebsiteTelegram(models.Model): # Iterasi untuk setiap username for name in self.user_id: - user_to_add = await client.get_entity(name.telegram_id) - result_add_user = await client(functions.channels.InviteToChannelRequest( - channel=channel, - users=[user_to_add.id], - )) + if name.telegram_id: + user_to_add = await client.get_entity(name.telegram_id) + result_add_user = await client(functions.channels.InviteToChannelRequest( + channel=channel, + users=[user_to_add.id], + )) + else: + raise UserError('ID Telegram '+ name.name + ' salah atau belum diisi') # message = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=209&model=sale.order&view_type=form&cids=1&menu_id=101' message_template = 'https://erp.indoteknik.com/web#id=' + self.id_data + '&action=357&model=sale.order&view_type=form&cids=1&menu_id=212' message2 = self.tittle + ' di print oleh ' + self.env.user.name -- cgit v1.2.3