diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-28 17:10:32 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-28 17:10:32 +0700 |
| commit | d72d00a740248af2116cd9085a6d7f3206e01085 (patch) | |
| tree | 89648dee2294e4f4393996be1bb1dfce7dda0c15 | |
| parent | 241df23ed0bc2e0aefdc032900753b04399d190d (diff) | |
parse wati notification
| -rwxr-xr-x | indoteknik_custom/models/crm_lead.py | 1 | ||||
| -rw-r--r-- | indoteknik_custom/models/wati.py | 36 | ||||
| -rwxr-xr-x | indoteknik_custom/views/crm_lead.xml | 20 |
3 files changed, 53 insertions, 4 deletions
diff --git a/indoteknik_custom/models/crm_lead.py b/indoteknik_custom/models/crm_lead.py index 0534eb2f..6052acfa 100755 --- a/indoteknik_custom/models/crm_lead.py +++ b/indoteknik_custom/models/crm_lead.py @@ -11,6 +11,7 @@ class CrmLead(models.Model): file_siup = fields.Binary(string="Surat Izin Usaha Perdagangan") body_html_lead = fields.Text('Body HTML', compute='compute_body_leads') # for wati only + wati_notification_id = fields.One2many('wati.notification', 'lead_id', string='Wati Notification') ticket_id = fields.Char('Ticket ID', help='Ticket ID yang ada di WATI') operator_email = fields.Char('Operator Email', help='Operator yang membalas') operator_name = fields.Char('Operator Name', help='Operator yang membalas') diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py index df467ea1..1f694380 100644 --- a/indoteknik_custom/models/wati.py +++ b/indoteknik_custom/models/wati.py @@ -9,7 +9,15 @@ _logger = logging.getLogger(__name__) class WatiNotification(models.Model): _name = 'wati.notification' + lead_id = fields.Many2one('crm.lead', string='Lead Id') + ticket_id = fields.Char(string='Ticked Id') + mobile = fields.Char(string='Mobile') + sender_name = fields.Char(string='Sender Name') + text = fields.Char(string='Text Message') + text_time = fields.Datetime(string='Text Time') + text_type = fields.Char(string='Text Type') json_raw = fields.Char(string='JSON Raw Text') + is_parsed = fields.Boolean(string='Is Parsed') is_lead = fields.Boolean(string='To Leads', help='apakah sudah ter-convert jadi leads') def _cleanup(self): @@ -23,6 +31,27 @@ class WatiNotification(models.Model): ]).unlink() _logger.info('Success Cleanup WATI Notification') + def _parse_notification(self, limit = 0): + domain = [('is_parsed', '=', False)] + notifications = self.search(domain, order='id desc', limit=limit) + notification_not_parsed_count = self.search_count(domain) + i = 0 + for notification in notifications: + i += 1 + _logger.info('[Parse Notification][%s] Process: %s/%s | Not Parsed: %s' % (notification.id, i, str(limit), str(notification_not_parsed_count))) + notification_json = json.loads(notification.json_raw) + sender_name = 'Indoteknik' + if 'senderName' in notification_json: + sender_name = notification_json['senderName'] + + notification.ticket_id = notification_json['ticketId'] + notification.mobile = notification_json['waId'] + notification.sender_name = sender_name + notification.text = notification_json['text'] + notification.text_time = datetime.fromtimestamp(float(notification_json['timestamp'])) + notification.text_type = notification_json['type'] + notification.is_parsed = True + return def _convert_to_leads(self): query = [ @@ -65,20 +94,18 @@ class WatiNotification(models.Model): current_lead.operator_email = operator_email current_lead.operator_name = operator_name current_lead.user_id = sales - wati.is_lead = True elif current_lead: # must append internal notes as a reply here current_lead.description = str(current_lead.description) + " | c:" +str(text) current_lead.operator_email = operator_email current_lead.operator_name = operator_name - wati.is_lead = True else: # create new leads contact_name = json.loads(wati.json_raw)['senderName'] phone = json.loads(wati.json_raw)['waId'] name = 'Ada pesan dari WATI '+str(phone)+' '+str(contact_name) - self.env['crm.lead'].create([{ + current_lead = self.env['crm.lead'].create([{ 'name': name, 'ticket_id': ticket_id, 'operator_email': operator_email, @@ -87,4 +114,5 @@ class WatiNotification(models.Model): 'phone': phone, 'description': "c:" +str(text) }]) - wati.is_lead = True + wati.is_lead = True + wati.lead_id = current_lead.id diff --git a/indoteknik_custom/views/crm_lead.xml b/indoteknik_custom/views/crm_lead.xml index 58c27050..6a07c760 100755 --- a/indoteknik_custom/views/crm_lead.xml +++ b/indoteknik_custom/views/crm_lead.xml @@ -7,6 +7,26 @@ <field name="inherit_id" ref="crm.crm_lead_view_form"/> <field name="arch" type="xml"> <page name="lead" position="after"> + <page string="Wati"> + <field name="wati_notification_id" widget="many2many"> + <tree> + <field name="sender_name" /> + <field name="text" /> + <field name="text_time" /> + </tree> + <form> + <group> + <group> + <field name="sender_name" /> + <field name="text_time" /> + </group> + <group> + <field name="text" /> + </group> + </group> + </form> + </field> + </page> <page string="Description" name="description"> <field name="html_description"/> </page> |
