This script, ObjHookHCScoreWA.py, is a webhook handler designed to process customer scoring data received from a WhatsApp channel. It takes a customer's data, normalizes it, inserts it into a central scoring table, triggers a scoring workflow, and then sends a confirmation SMS.
The script follows these steps:
Param1) that uniquely identifies the scoring request from the WhatsApp channel.webhook_code to "HCSCORE" to ensure that a general scoring workflow is triggered.bloom_hcscorewa_payload using the provided GUID. The data is then transformed and inserted into the main bloom_hcscore table. Key transformations include:
Acctclass, Acctclasstype, and Applicationchannel.Dob) and Gender from their 13-digit South African ID number.Firstname and Surname.Department code.Leadorigin code of '14' to indicate the WhatsApp channel.HCSCORE workflow by calling the Run method of the ObjWorkflow class, passing the GUID to it.bloom_hcscorewa_payload table. It then makes a POST request to an external API to send an SMS to the customer.stage_service table to mark the process as "DONE" (though this is commented out) and returns the string "DONE".The script is designed to be called with the following parameters:
payload: The raw payload from the webhook (not directly used in the Process method's logic).webhook_code: A code identifying the webhook source (e.g., "HCSCOREWA").Param1: The GUID for the specific transaction.Param2 - Param8: Additional optional parameters, which are not used in the current implementation.The script interacts with the following database tables:
bloom_hcscorewa_payload: The source table where raw data from the WhatsApp channel is initially stored.bloom_hcscore: The destination table where the normalized and standardized customer data is inserted.stage_service: A table for tracking the status of services. The script contains commented-out code to manage the status of the scoring task.The script makes a POST request to an external API to send an SMS notification:
https://hcawsgateway.technocore.co.za/webhook.py{"context":"homechoicedirect"}Content-Type: application/jsonUser-Agent: Whatsapp{
"content": "{idno} | Whatsapp",
"smsLocation": "{shortcode}",
"cellNumber": "{cellno}",
"context": "homechoicedirect"
}
The script relies on the following modules:
ossysrequestsObjDataObjWorkflow# (c) TechnoCore - All Rights Reserved.
# NOTICE: All information contained herein is, and remains
# the property of TechnoCore Automate.
#
# The intellectual and technical concepts contained
# herein are proprietary to TechnoCore Marketing and
# dissemination of this information or reproduction of this material
# is strictly forbidden unless prior written permission is obtained
# from TechnoCore Automate.
import os
import sys
import requests
Path = os.getcwd()
sys.path.append(Path)
Path = os.getcwd()+"/factory.core"
sys.path.append(Path)
Path = os.getcwd()+"/factory.service"
sys.path.append(Path)
Path = os.getcwd()+"/factory.webhook"
sys.path.append(Path)
Path = "/var/www/axion/objects/factory.service/"
sys.path.append(Path)
import ObjData
import ObjWorkflow
class ObjHook(ObjData.ObjData):
def __init__(self, DB=0, Page=0):
ObjData.ObjData.__init__(self, DB)
self._IsA = "ObjHookScore"
self._Version = "1.4"
self._Updatedate = ""
self.kafka = 0
self.payload = ""
def Process(self, payload = "", webhook_code="", Param1 = "",
Param2 = "", Param3 = "", Param4 = "",
Param5= "", Param6= "", Param7= "", Param8 =""):
Guid = Param1
#if webhook_code == "HCSCOREAO":
webhook_code = "HCSCORE"
self.debug("Workflow to run ",webhook_code)
# Test guid : 20240906_0828_JBoK6i2Dv2gydqtRhd7J3C
sql = """
-- Inserting normalized WA scoring payload into bloom_hcscore
INSERT INTO `bloom_hcscore` (
`_guid`, `Guid`, `Acctclass`, `Acctclasstype`, `Applicationchannel`,
`Cellno`, `Cellotherno`, `Ctryno`, `Deceasedflag`, `Dob`,
`Firstname`, `Gender`, `Homeno`, `Idnumber`, `Bloom_idnumber`,
`Idtypeno`, `Msca`, `Passportflag`, `Rtprofile`, `Fcprofile`,
`Scorecardno`, `Scorecategory`, `Surname`, `Workno`, `Department`, `Leadorigin`
)
SELECT
_guid AS `_guid`,
Guid AS `Guid`,
'0' AS `Acctclass`, -- Default: 0
'0' AS `Acctclasstype`, -- Default: 0
'1' AS `Applicationchannel`, -- Default: Online/WebApp
CellNo AS `Cellno`,
'' AS `Cellotherno`, -- No secondary contact
'1' AS `Ctryno`, -- Default: South Africa
'False' AS `Deceasedflag`, -- Placeholder flag
IF(LENGTH(IDNumber) = 13,
CONCAT(IF(LEFT(IDNumber,2) < '23', '20', '19'), LEFT(IDNumber,6)),
'') AS `Dob`, -- Infer DOB from RSA ID format
'x' AS `Firstname`, -- Placeholder
IF(LENGTH(IDNumber) = 13,
IF(MID(IDNumber,7,1) < 5, 'F', 'M'),
'F') AS `Gender`, -- Infer gender from ID format
'' AS `Homeno`,
IDNumber AS `Idnumber`,
IDNumber AS `Bloom_idnumber`,
'1' AS `Idtypeno`, -- RSA ID type
'99999' AS `Msca`, -- NEW Account default
'N' AS `Passportflag`,
'0' AS `Rtprofile`,
'0' AS `Fcprofile`,
'1' AS `Scorecardno`, -- Default: NEW scorecard
'N' AS `Scorecategory`, -- Default: New category
'x' AS `Surname`, -- Placeholder
'' AS `Workno`,
IF (Shortcode = '800000', '46', '46') AS Department, -- Hardcoded override
'14' AS `Leadorigin` -- WA channel origin
FROM `bloom_hcscorewa_payload`
WHERE `_guid` = '$guid$';
"""
sql = sql.replace("$guid$",Param1)
self.sql_execute(sql)
self.kafka = 0
work = ObjWorkflow.Workflow(self.DB)
work.Run(webhook_code, Param1)
self.debug("HCScore hook connected", Guid)
sql = """
SELECT
CellNo as CellNo,
IDNumber as IdNumber,
ShortCode
FROM bloom_hcscorewa_payload
where _guid = '$guid$'
"""
sql = sql.replace("$guid$",Param1)
try:
cellno, idno, shortcode = self.sql_get_values(sql)
except:
self.error("Cannot get cellno and idno", sql)
url = "https://hcawsgateway.technocore.co.za/webhook.py"
querystring = {"context":"homechoicedirect"}
payload = {
"content": f"{idno} | Whatsapp",
"smsLocation": shortcode,
"cellNumber": cellno,
"context": "homechoicedirect"
}
headers = {
"Content-Type": "application/json",
"User-Agent": "Whatsapp"
}
response = requests.request("POST", url, json=payload, headers=headers, params=querystring)
Sql = "Update stage_service Set Status = 'DONE' where Guid = '"+Guid+"'"
#self.Execute(Sql)
Ret = "DONE"
return Ret
if __name__ == "__main__":
Obj = ObjHook()
print(Obj.Process("","","20240815_1216_H9Ywmris68dFwNThF8mMeU"))