← back to Dw Chat Analyzer

fm-alert-setup.md

59 lines

# FileMaker on-screen alert for new HOT chats — setup (5 min)

**Design:** FileMaker *pulls* new HOT chats from the analyzer via `Insert from URL`
(no external write to FileMaker Cloud, so no Claris-ID/Cognito auth to fight).
The analyzer serves them at `GET /api/new-hot?since=<iso>` (Basic Auth admin/DW2024!).

## Prereq
FileMaker Pro must reach the analyzer. If FileMaker Pro runs **on macstudio3**, use
`http://127.0.0.1:9802`. If it runs on **another Mac**, tell me and I'll expose the
endpoint on the LAN IP (it's localhost-only right now).

## 1. (optional) WebChats table — a log of alerted leads
In `Clients` → Manage Database → new table **WebChats**, fields:
`chat_id` (text), `arrived_ts` (text), `contact` (text), `location` (text),
`landing` (text), `chat_link` (text), `seen` (number). Not required for the popup —
only if you want a running list of leads inside FileMaker.

## 2. Script: "CheckNewChats" (Manage Scripts → new)
Uses a global field or `$$LAST_HOT_TS` to remember the last alert time.

```
# first run: seed the cursor to now so you don't get the backlog
If [ IsEmpty ( $$LAST_HOT_TS ) ]
  Set Variable [ $$LAST_HOT_TS ; Value: Get(CurrentTimestampUTCMilliseconds) ]  # or a fixed ISO
End If
Set Variable [ $url ; Value:
   "http://127.0.0.1:9802/api/new-hot?since=" & GetAsURLEncoded ( $$LAST_HOT_TS ) ]
Insert from URL [ With dialog: Off ; Target: $result ; $url ;
   cURL options: "--user admin:DW2024! --max-time 15" ; Select: On ; Verify SSL: Off ]
Set Variable [ $count ; Value: JSONGetElement ( $result ; "count" ) ]
If [ $count > 0 ]
  Set Variable [ $$LAST_HOT_TS ; Value: JSONGetElement ( $result ; "latest" ) ]
  Set Variable [ $c ; Value: JSONGetElement ( $result ; "chats[0]" ) ]
  Show Custom Dialog [ "🔥 New chat lead (" & $count & ")" ;
     JSONGetElement($c;"contact") & "  ·  " & JSONGetElement($c;"location") &
     "¶Landing: " & JSONGetElement($c;"landing") ;
     Buttons: "Open chat" , "Dismiss" ]
  If [ Get(LastMessageChoice) = 1 ]
     Open URL [ With dialog: Off ; JSONGetElement($c;"zendesk_link") ]
  End If
  # (optional) create a WebChats record here via New Record/Request + Set Field
End If
```

## 3. Fire it every 60s
On the layout you keep open (e.g. the main Clients layout), Layout Setup →
Script Triggers → **OnTimerInterval? no** — instead run once from a startup script:
`Install OnTimer Script [ "CheckNewChats" ; Interval: 60 ]`
(or add `Install OnTimer` to your existing open/startup script). It then polls every
60s while that window is open and pops a dialog whenever a new HOT chat arrives, with
an **Open chat** button that jumps to `designerwallcoverings.zendesk.com/chat/agent`.

## Notes
- The endpoint returns up to 25 newest HOT chats since `since`; the dialog shows the
  newest + the count. Loop `chats[0..count-1]` if you want one dialog per lead.
- "HOT" = the visitor left an email/phone (the only reliable lead signal in this data).
- The analyzer + watcher run under pm2 (dw-chat-analyzer :9802, dw-chat-watch), so
  this keeps working across reboots.