from odoo import fields, models, api from datetime import datetime, timedelta import logging import json _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', default=False) is_lead = fields.Boolean(string='To Leads', help='apakah sudah ter-convert jadi leads') def _cleanup(self): current_time = datetime.now() delta_time = current_time - timedelta(days=15) delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') self.env['wati.notification'].search([ ('create_date', '<', delta_time), ('is_lead', '=', True), ]).unlink() _logger.info('Success Cleanup WATI Notification') def _parse_notification(self, limit = 0): domain = [('is_parsed', '=', False)] notifications = self.search(domain, order='id', 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'] 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): query = [ ('is_lead', '=', False) ] watis = self.env['wati.notification'].search(query, order='id') for wati in watis: _logger.info('Convert to Lead WATI Notification ID %s' % wati.id) ticket_id = json.loads(wati.json_raw)['ticketId'] text = json.loads(wati.json_raw)['text'] current_lead = self.env['crm.lead'].search([('ticket_id', '=', ticket_id)], limit=1) operator_email = json.loads(wati.json_raw)['operatorEmail'] operator_name = json.loads(wati.json_raw)['operatorName'] event_type = json.loads(wati.json_raw)['eventType'] if event_type == 'sessionMessageSent': if 'Saya *Eko*' in str(text): sales = 11 elif 'Saya *Nabila*' in str(text): sales = 20 elif 'Saya *Novita*' in str(text): sales = 377 elif 'Saya *Putri*' in str(text): sales = 10 elif 'Saya *Heriyanto*' in str(text): sales = 375 elif 'Saya *Ade*' in str(text): sales = 9 elif 'Saya *Adela*' in str(text): sales = 8 elif 'Saya *Jananto*' in str(text): sales = 376 elif 'Saya *Dwi*' in str(text): sales = 24 else: sales = 25 #System current_lead.description = str(current_lead.description)+ "| i:" + str(text) current_lead.operator_email = operator_email current_lead.operator_name = operator_name current_lead.user_id = sales 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 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) current_lead = self.env['crm.lead'].create([{ 'name': name, 'ticket_id': ticket_id, 'operator_email': operator_email, 'operator_name': operator_name, 'contact_name': contact_name, 'phone': phone, 'description': "c:" +str(text) }]) 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')