1+ import { getConfig , getFeatures } from "../../../server/api/lib/config" ;
2+ import { cacheableData } from "../../../server/models" ;
3+
4+ const DEFAULT_AUTO_OPTIN_REGEX_LIST_BASE64 =
5+ "W3sicmVnZXgiOiAiXlNUQVJUJCIsICJyZWFzb24iOiAic3RhcnQifV0=" ;
6+
7+ // DEFAULT_AUTO_OPTIN_REGEX_LIST_BASE64 decodes to:
8+ // [{"regex": "^START$"", "reason": "start"}]
9+
10+ export const serverAdministratorInstructions = ( ) => {
11+ return {
12+ description : `Can add people to opt in list depending on the reply` ,
13+ setupInstructions : `
14+ Add "auto-optin" to message hanlder environment variable.
15+ Can change default optin langauge by adding AUTO_OPTIN_REGEX_LIST_BASE64 to
16+ \`.env\`. This variable should be a JSON object encoded in base64. See line 8 for
17+ current structure.
18+ ` ,
19+ environmentVariables : [ "AUTO_OPTIN_REGEX_LIST_BASE64" ]
20+ }
21+ }
22+
23+ export const available = organization => {
24+ const config = getConfig ( "AUTO_OPTIN_REGEX_LIST_BASE64" , organization ) ||
25+ DEFAULT_AUTO_OPTIN_REGEX_LIST_BASE64 ;
26+
27+ if ( ! config ) return false ;
28+ try {
29+ JSON . parse ( Buffer . from ( config , "base64" ) . toString ( ) ) ;
30+ return true ;
31+ } catch ( exception ) {
32+ console . log (
33+ "message-handler/auto-optin JSON parse of AUTO_OPTIN_REGEX_LIST_BASE64 failed" ,
34+ exception
35+ ) ;
36+ return false ;
37+ }
38+ }
39+
40+ export const preMessageSave = ( { messageToSave, organization } ) => {
41+ if ( messageToSave . is_from_contact ) {
42+ const config = Buffer . from (
43+ getConfig ( "AUTO_OPTIN_REGEX_LIST_BASE64" , organization ) ||
44+ DEFAULT_AUTO_OPTIN_REGEX_LIST_BASE64 ,
45+ "base64"
46+ ) . toString ( ) ;
47+ const regexList = JSON . parse ( config || "[]" ) ;
48+ const matches = regexList . filter ( matcher => {
49+ const re = new RegExp ( matcher . regex ) ; // Want case sensitivity, probably?
50+ return String ( messageToSave . text ) . match ( re ) ;
51+ } )
52+ if ( matches . length ) {
53+ console . log (
54+ `auto-optin MATCH ${ messageToSave . campaign_contact_id } `
55+ ) ;
56+ const reason = matches [ 0 ] . reason || "auto_optin" ;
57+
58+ // UNSURE OF THIS RETURN
59+ return {
60+ contactUpdates : {
61+ is_opted_in : true
62+ } ,
63+ handlerContext : {
64+ autoOptInReason : reason ,
65+ } ,
66+ messageToSave
67+ } ;
68+ }
69+ }
70+ }
71+
72+ export const postMessageSave = async ( {
73+ message,
74+ organization,
75+ handlerContext,
76+ campaign
77+ } ) => {
78+ if ( ! message . is_from_contact || ! handlerContext . autoOptInReason ) return ;
79+
80+ console . log (
81+ `auto-optin.postMessageSave ${ message . campaign_contact_id } `
82+ ) ;
83+
84+ let contact = await cacheableData . campaignContact . load (
85+ message . campaign_contact_id ,
86+ { cahceOnly : true }
87+ ) ;
88+ campaign = campaign || { organization_id : organization . id } ;
89+
90+ // Save to DB
91+ await cacheableData . optIn . save ( {
92+ cell : message . contact_number ,
93+ campaign,
94+ assignmentId : ( contact && contact . assignment_id ) || null ,
95+ reason : handlerContext . autoOptInReason
96+ } ) ;
97+ }
0 commit comments