From 5d4cfcb05b85add3bc08a0e161257a69e10550e2 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 29 Nov 2023 09:12:10 +0700 Subject: history of wati --- indoteknik_custom/models/wati.py | 96 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 9 deletions(-) (limited to 'indoteknik_custom/models/wati.py') diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py index 1f694380..bb6f2293 100644 --- a/indoteknik_custom/models/wati.py +++ b/indoteknik_custom/models/wati.py @@ -17,7 +17,7 @@ class WatiNotification(models.Model): 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_parsed = fields.Boolean(string='Is Parsed', default=False) is_lead = fields.Boolean(string='To Leads', help='apakah sudah ter-convert jadi leads') def _cleanup(self): @@ -33,7 +33,7 @@ class WatiNotification(models.Model): def _parse_notification(self, limit = 0): domain = [('is_parsed', '=', False)] - notifications = self.search(domain, order='id desc', limit=limit) + notifications = self.search(domain, order='id', limit=limit) notification_not_parsed_count = self.search_count(domain) i = 0 for notification in notifications: @@ -43,14 +43,59 @@ class WatiNotification(models.Model): 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'] + + ticket_id = notification_json['ticketId'] + date_wati = float(notification_json['timestamp']) + date_wati = datetime.fromtimestamp(date_wati) + wati_history = self.env['wati.history'].search([('ticket_id', '=', ticket_id)], limit=1) + if wati_history: + self._create_wati_history_line(wati_history, ticket_id, sender_name, notification_json, date_wati) + else: + new_header = self._create_wati_history_header(ticket_id, sender_name, notification_json, date_wati) + self._create_wati_history_line(new_header, ticket_id, sender_name, notification_json, date_wati) notification.is_parsed = True + + # 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 _create_wati_history_header(self, ticket_id, sender_name, notification_json, date_wati): + param_header = { + 'ticket_id': ticket_id, + 'conversation_id': notification_json['conversationId'], + 'sender_name': sender_name, + 'wa_id': notification_json['waId'], + 'text': notification_json['text'], + 'date_wati': date_wati or '', + } + new_header = self.env['wati.history'].create([param_header]) + return new_header + + def _create_wati_history_line(self, new_header, ticket_id, sender_name, notification_json, date_wati): + param_line = { + "wati_history_id": new_header.id, + "conversation_id": notification_json['conversationId'], + "data": notification_json['data'] or '', + "event_type": notification_json['eventType'] or '', + # "list_reply": notification_json['listReply'] or '', + # "message_contact": notification_json['messageContact'] or '', + "operator_email": notification_json['operatorEmail'] or '', + "operator_name": notification_json['operatorName'] or '', + "sender_name": sender_name or '', + # "source_url": notification_json['sourceUrl'] or '', + "status_string": notification_json['statusString'] or '', + "text": notification_json['text'] or '', + "ticket_id": ticket_id, + "type": notification_json['type'] or '', + "wa_id": notification_json['waId'] or '', + "date_wati": date_wati or '', + } + self.env['wati.history.line'].create([param_line]) return def _convert_to_leads(self): @@ -116,3 +161,36 @@ class WatiNotification(models.Model): }]) wati.is_lead = True wati.lead_id = current_lead.id + + +class WatiHistory(models.Model): + _name = 'wati.history' + + ticket_id = fields.Char(string='Ticket ID') + conversation_id = fields.Char(string='Conversation ID') + sender_name = fields.Char(string='Sender Name') + wa_id = fields.Char(string='WA ID') + text = fields.Char(string='Text') + date_wati = fields.Datetime(string='Date WATI') + wati_lines = fields.One2many('wati.history.line', 'wati_history_id', string='Lines', auto_join=True) + +class WatiHistoryLine(models.Model): + _name = 'wati.history.line' + + #sender + wati_history_id = fields.Many2one('ref', required=True, ondelete='cascade', index=True, copy=False) + conversation_id = fields.Char(string='Conversation ID') + data = fields.Char(string='data') + event_type = fields.Char(string='Event Type') + list_reply = fields.Char(string='List Reply') + message_contact = fields.Char(string='Message Contact') + operator_email = fields.Char(string='Operator Email') + operator_name = fields.Char(string='Operator Name') + sender_name = fields.Char(string='Sender Name') + source_url = fields.Char(string='Source URL') + status_string = fields.Char(string='Status String') + text = fields.Char(string='Text') + ticket_id = fields.Char(string='Ticket ID') + type = fields.Char(string='Type') + wa_id = fields.Char(string='WA ID') + date_wati = fields.Datetime(string='Date WATI') -- cgit v1.2.3 From 3327fbccee4c6c95df325b515a7a42b6f937d381 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 29 Nov 2023 12:10:14 +0700 Subject: add expired date on wati --- indoteknik_custom/models/wati.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models/wati.py') diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py index bb6f2293..1e6ac17f 100644 --- a/indoteknik_custom/models/wati.py +++ b/indoteknik_custom/models/wati.py @@ -77,6 +77,7 @@ class WatiNotification(models.Model): return new_header def _create_wati_history_line(self, new_header, ticket_id, sender_name, notification_json, date_wati): + text_body = notification_json['text'] or '' param_line = { "wati_history_id": new_header.id, "conversation_id": notification_json['conversationId'], @@ -89,13 +90,22 @@ class WatiNotification(models.Model): "sender_name": sender_name or '', # "source_url": notification_json['sourceUrl'] or '', "status_string": notification_json['statusString'] or '', - "text": notification_json['text'] or '', + "text": text_body, "ticket_id": ticket_id, "type": notification_json['type'] or '', "wa_id": notification_json['waId'] or '', "date_wati": date_wati or '', } self.env['wati.history.line'].create([param_line]) + new_header.last_reply_by = sender_name + new_header.last_reply_date = date_wati + new_header.last_reply_text = text_body + if sender_name == 'Indoteknik': + current_time = date_wati + # current_time_str = current_time.strftime('%Y-%m-%d %H:%M:%S') + delta_time = current_time + timedelta(days=1) + # delta_time_str = delta_time.strftime('%Y-%m-%d %H:%M:%S') + new_header.expired_date = delta_time return def _convert_to_leads(self): @@ -173,6 +183,23 @@ class WatiHistory(models.Model): text = fields.Char(string='Text') date_wati = fields.Datetime(string='Date WATI') wati_lines = fields.One2many('wati.history.line', 'wati_history_id', string='Lines', auto_join=True) + last_reply_by = fields.Char(string='Last Reply by') + last_reply_text = fields.Char(string='Last Reply Text') + last_reply_date = fields.Datetime(string='Last Reply Date') + expired_date = fields.Datetime(string='Expired Date') + + # @api.onchange('last_reply_date') + # def _compute_expired_date(self): + # if self.last_reply_by == 'Indoteknik': + # return + # else: + # print(1) + # current_time = self.last_reply_date + # # current_time_str = current_time.strftime('%Y-%m-%d %H:%M:%S') + # delta_time = current_time + timedelta(days=1) + # # delta_time_str = delta_time.strftime('%Y-%m-%d %H:%M:%S') + # self.expired_date = delta_time + # return class WatiHistoryLine(models.Model): _name = 'wati.history.line' -- cgit v1.2.3 From e7b7ccfbe3a96f81066845d12d4cc08e0720a715 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 29 Nov 2023 14:39:17 +0700 Subject: add email and company in wati history --- indoteknik_custom/models/wati.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'indoteknik_custom/models/wati.py') diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py index 1e6ac17f..af1aa7b8 100644 --- a/indoteknik_custom/models/wati.py +++ b/indoteknik_custom/models/wati.py @@ -187,6 +187,8 @@ class WatiHistory(models.Model): last_reply_text = fields.Char(string='Last Reply Text') last_reply_date = fields.Datetime(string='Last Reply Date') expired_date = fields.Datetime(string='Expired Date') + email = fields.Char(string='Email') + perusahaan = fields.Char(string='Perusahaan') # @api.onchange('last_reply_date') # def _compute_expired_date(self): -- cgit v1.2.3 From 607ad8509198265e467f3b43505e9f6f157bce35 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Thu, 30 Nov 2023 10:27:37 +0700 Subject: add order descending on wati history --- indoteknik_custom/models/wati.py | 59 ++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 11 deletions(-) (limited to 'indoteknik_custom/models/wati.py') diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py index af1aa7b8..aa63fe92 100644 --- a/indoteknik_custom/models/wati.py +++ b/indoteknik_custom/models/wati.py @@ -2,6 +2,7 @@ from odoo import fields, models, api from datetime import datetime, timedelta import logging import json +import requests _logger = logging.getLogger(__name__) @@ -54,15 +55,7 @@ class WatiNotification(models.Model): new_header = self._create_wati_history_header(ticket_id, sender_name, notification_json, date_wati) self._create_wati_history_line(new_header, ticket_id, sender_name, notification_json, date_wati) notification.is_parsed = True - - # 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 + return def _create_wati_history_header(self, ticket_id, sender_name, notification_json, date_wati): param_header = { @@ -97,15 +90,58 @@ class WatiNotification(models.Model): "date_wati": date_wati or '', } self.env['wati.history.line'].create([param_line]) + self._update_header_after_create_line(new_header, sender_name, date_wati, text_body) + return + + def _update_header_after_create_line(self, new_header, sender_name, date_wati, text_body): new_header.last_reply_by = sender_name new_header.last_reply_date = date_wati new_header.last_reply_text = text_body if sender_name == 'Indoteknik': current_time = date_wati - # current_time_str = current_time.strftime('%Y-%m-%d %H:%M:%S') delta_time = current_time + timedelta(days=1) - # delta_time_str = delta_time.strftime('%Y-%m-%d %H:%M:%S') new_header.expired_date = delta_time + if not new_header.perusahaan or not new_header.email: + self._get_attribute_wati(new_header) + return + + def _get_attribute_wati(self, new_header): + # url = 'https://live-server-2106.wati.io/api/v1/getContacts' + + # cookies = { + # 'affinity': '1701232090.884.1520.321410|ff187ffce9bc0bae13542bb446e41008', + # } + + # headers = { + # 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3MGM5ZmJhNy00MWRlLTRkMWEtYjY2NS1hM2Q5ODc2ZjhlZWIiLCJ1bmlxdWVfbmFtZSI6InR5YXNAaW5kb3Rla25pay5jb20iLCJuYW1laWQiOiJ0eWFzQGluZG90ZWtuaWsuY29tIiwiZW1haWwiOiJ0eWFzQGluZG90ZWtuaWsuY29tIiwiYXV0aF90aW1lIjoiMTEvMjkvMjAyMyAwNDoxNzo0NyIsImRiX25hbWUiOiIyMTA2IiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiQURNSU5JU1RSQVRPUiIsImV4cCI6MjUzNDAyMzAwODAwLCJpc3MiOiJDbGFyZV9BSSIsImF1ZCI6IkNsYXJlX0FJIn0.--KHv4GCOG2MM3lNW9Nm-0-d8OAVpn5kbcSX4JKqATQ', + # # 'Cookie': 'affinity=1701232090.884.1520.321410|ff187ffce9bc0bae13542bb446e41008', + # } + + # files = { + # 'pageSize': (None, '1'), + # 'pageNumber': (None, '1'), + # 'name': (None, ''), + # 'attribute': (None, '[{name: "phone", operator: "contain", value: "6285751430014"}]'), + # 'createdDate': (None, ''), + # } + + # response = requests.get(url, cookies=cookies, headers=headers, files=files) + # print(response.json()) + wati_api = self.env['wati.api'] + params = { + 'pageSize':1, + 'pageNumber':1, + 'attribute':[{'name': "phone", 'operator': "contain", 'value': "6285751430014"}], + } + wati_contacts = wati_api.http_get('/api/v1/getContacts', params) + if wati_contacts['result'] != 'success': + return + # print (wati_contacts) + json_dump = json.dumps(wati_contacts, indent=4, sort_keys=True) + contact_list = json.loads(json_dump)['contact_list'] + id = json.loads(contact_list)['id'] + print(id) + return def _convert_to_leads(self): @@ -175,6 +211,7 @@ class WatiNotification(models.Model): class WatiHistory(models.Model): _name = 'wati.history' + _order = 'id desc' ticket_id = fields.Char(string='Ticket ID') conversation_id = fields.Char(string='Conversation ID') -- cgit v1.2.3