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 +++++++++++++++++++++------- indoteknik_custom/views/website_telegram.xml | 16 +++--------- 3 files changed, 40 insertions(+), 22 deletions(-) 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 diff --git a/indoteknik_custom/views/website_telegram.xml b/indoteknik_custom/views/website_telegram.xml index 58aaabda..887d62e1 100644 --- a/indoteknik_custom/views/website_telegram.xml +++ b/indoteknik_custom/views/website_telegram.xml @@ -12,15 +12,10 @@ website.telegram - - -