diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-10-04 14:31:33 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-10-04 14:31:33 +0700 |
| commit | ec1e2331be248a505d89c00244d6b0fe5bd61c26 (patch) | |
| tree | cadc19aee4c3780761e03ae416c14825a7cea1f6 | |
| parent | cc862f2bb8a6a751612bab7fb2d07f7306f9a4fd (diff) | |
<iman> add get text from channel
| -rw-r--r-- | indoteknik_custom/models/website_telegram.py | 21 | ||||
| -rw-r--r-- | indoteknik_custom/views/website_telegram.xml | 1 |
2 files changed, 21 insertions, 1 deletions
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}") diff --git a/indoteknik_custom/views/website_telegram.xml b/indoteknik_custom/views/website_telegram.xml index 9698bfc4..58aaabda 100644 --- a/indoteknik_custom/views/website_telegram.xml +++ b/indoteknik_custom/views/website_telegram.xml @@ -21,6 +21,7 @@ <field name="python_code"/> <button name="copy_chat_id" type="object" string="Copy Code" icon="fa-clipboard"/> <button name="test_send" type="object" icon="fa-paper-plane" string="Send Test"/> + <button name="receive_messages" type="object" icon="fa-paper-plane" string="GET Test"/> </tree> </field> </record> |
