Twilio inbound message

I am trying to integrate Twilio SMS into hass.io. I have the outbound SMS working, but can’t seem to figure out the inbound.

I am using a basic automation, to trigger when an inbound message comes in. I have already setup the Twilio integration, and plugged the generated webhook into my Twilio phone number.

The goal is to add an item to my shopping list on an inbound sms. I will setup conditions, etc, once I get it working. I just need it to work.

The webhooks are hitting my hass.io server, and it is adding to my shopping list, but it’s not adding the body of the inbound SMS.

Does anyone know how to get the body of the SMS and use it as a parameter to call a service?

Here is my configuration -

- id: '1570226835161'
  alias: Add to grocery list
  trigger:
  - event_data: {}
    event_type: twilio_data_received
    platform: event
  condition: []
  action:
  - data:
      name: '{{ trigger.data.Body }}'
    service: shopping_list.add_item

Right now, it literally adds {{ trigger.data.Body }}
image

Change this:

  - data:

to this:

  - data_template:

Thanks for the help! That did solve my problem, however Twilio sends the data in a strange way, so I will have to write some kind of translation layer for it to send JSON to hass.io.

For anyone else that may come across this thread -

To fix this, I changed 2 things -

  1. Changed data: to data_template: (Thanks @123)
  2. Created a custom function in Twilio on inbound messages to do a bit of processing, then send json to a custom webhook I created in hass.io.

I’ve run across the same problem - would you be willing to share the Twilio function code that you used?

@infinity312 Sure! See my Twilio function code below.

I added a few things to this. My goal was to be able to send a message inbound to my Twilio number like “Add milk”, which would add “Milk” to my grocery list. So, in my code below, I do a bit of cleansing & parsing. Feel free to use what you need!

Let me know if you have any questions

exports.handler = function(context, event, callback) {
    const got = require('got');
    const message = (event.Body).toLowerCase().split(' ').map((s) => s.charAt(0).toUpperCase() + s.substring(1)).join(' ');
    if ((/^add/i).test(message)) {
	    var url = "http://public.hassio.domain/api/webhook/add-to-grocery-list";
	    var payload = {body : message.replace(/^add/i, '')}
    }
    if (url && payload) {
	    got.post(url, {body : payload, json : true}).then((res) => {
	        console.log(res);
	        callback(null);
	    })
    } else {
        console.log("ERROR! No url/payload");
        console.log("URL: " + url);
        console.log("Payload: " + payload);
        callback("Error: no url or payload!");
    }
};
2 Likes

Hey @CodyFraley thanks for your post! I’ve been trying to setup something a little more basic than yours, but am still having trouble.

I’m trying to get Home Assistant to trigger different automations based on the Body of the SMS. Even when I configure Twilio to HTTP Post the Twilio Webhook that HA generates, I’m seeing the following error in my HA logs:

2019-12-13 15:55:31 ERROR (MainThread) [hass_nabucasa.iot] Error handling message
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/hass_nabucasa/iot.py", line 86, in _async_handle_handler_message
    result = await handler(self.cloud, message["payload"])
  File "/usr/local/lib/python3.7/site-packages/hass_nabucasa/iot.py", line 146, in async_handle_webhook
    return await cloud.client.async_webhook_message(payload)
  File "/usr/src/homeassistant/homeassistant/components/cloud/client.py", line 181, in async_webhook_message
    response_dict = utils.aiohttp_serialize_response(response)
  File "/usr/src/homeassistant/homeassistant/components/cloud/utils.py", line 9, in aiohttp_serialize_response
    body = response.body
AttributeError: 'str' object has no attribute 'body'
2019-12-13 15:55:31 DEBUG (MainThread) [hass_nabucasa.iot] Publishing message:
{'error': 'exception', 'msgid': 'xxx'}

2019-12-13 15:55:31 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'dict object' has no attribute 'data';

I’ve tried using your function as well but ran into trouble adding the ‘got’ dependency. I also tried using Twilio studio, and was able to get the SMS into HA, but got the same error as above.

Do you have any tips on getting me on the right track?

Hmm. I’m not super familiar with that error, but it looks like it’s not getting the data correctly.

What was the issue with adding the ‘got’ dependency in Twilio? Did you get an error there?

My only other suggestion would be to setup some kind of “middle man” that Twilio calls, it will process the data, then forward to HA in the correct format.

Hello
Great post. Im trying to decode an inbound Twilio SMS. I have setup webhook but how do i enable the twilio_data_received event in my home assistant?
Thanks!