Send a text message from Formant with more data
Q: I want to send a text message from Formant with more configuration/metadata than possible with Formant’s default behavior.
- I want to include additional metadata, such as the location
- I want to customize the URL it sends, to send a link to a view other than the default view
- We will expand the event functionality using a webhook. First we will create a webhook.
- Next we will create an event to trigger off a datapoint and send a POST request to the webhook when the event is fired.
- We will host a Webhook handler to listen for requests.
- The webhook handler will re-ingest a datapoint with the intended text as its payload. We will then fire a different event on the presence of this datapoint.
- Configure the event to send an SMS using the
{{datapoint_value}}
flag.
Example webhook handler (AWS Lambda)
This is not a functional example, but is intended to act as a general framework for how this webhook handler might be programmed.
import json
from formant.sdk.cloud.v2.client import Client
from formant.sdk.cloud.v2.src.resources.devices import Device
from formant.sdk.cloud.v2.src.resources.ingest import IngestionRequest
def process(event):
# Parse Payload
request = json.loads(event["body"])
payload = request["payload"]
device_id = payload["deviceId"]
timestamp = payload["timestamp"]
tags = payload["tags"]
# Get Device Information
formant_client = Client()
device = formant_client.admin.devices.get_device(device_id).parsed
location = device.tags.additional_properties['location']
device_name = device.name
# Format Payload
url = "https://app.formant.io/channel?device=%s&time=%s&streamName=front_camera&streamName=left_camera" % (device_name, timestamp)
text_payload = "%s spotted something at %s. Follow the link to see: %s" % (device_name, location, url)
# Ingest Payload
ingestion_request = IngestionRequest(device_id, tags, timestamp)
ingestion_request.add_text(text_payload)
formant_client.ingest.ingest.post(ingestion_request)
def main(event, context):
try:
process(event)
except Exception as error:
print(f"Failed to process request: {event} {error}")
return {"statusCode": 500, "body": json.dumps({})}
return {"statusCode": 200, "body": json.dumps({})}
if __name__ == "__main__":
main({}, {})
See also
If you notice an issue with this page or need help, please reach out to us! Use the 'Did this page help you?' buttons below, or get in contact with our Customer Success team via the Intercom messenger in the bottom-right corner of this page, or at [email protected].
Updated 6 months ago