summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/website_telegram.py
blob: 8fc6a8ea42425ca29d101c77ac4f9c81be49c570 (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
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})