summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/website_telegram.py
blob: 5684eff8392d31f8a42c1aa9a5a33fc0a2b84f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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}")