SMS Messaging

#1

Greetings all,

Currently my design uses mDashNotify(Email", …) to send email alerts from my ESP32 device. That in mind, my customers would also like to option to receive an SMS from the device instead of just email.

Is there a mechanism where one could POST to the Twillio API to originate an SMS from within the mDash infra?

Thanks!
-AD

#2

mDash handles HTTP.Request notification:

mDashNotify("HTTP.Request", "{%Q:%Q, %Q: %Q, %Q: %Q}",
   "url", "https://a.b.c/d",
   "method", "POST",
   "body", "aa=bb&cc=dd");

See https://mdash.net/docs/api/lib.md#mdashnotify-send-arbitrary-notification

1 Like
#3

Great, thanks for the tip!

-AD

#4

Following up on this thread

…I’m having difficulty formatting an example POST to match the format that mDashNotify is looking for.

How would I format the following curl example for use with mDashNotify?

curl 'https://api.twilio.com/2010-04-01/Accounts/ssid/Messages.json' -X POST \
--data-urlencode 'To=+18005556666' \
--data-urlencode 'From=+18005551212' \
--data-urlencode 'Body=Hello' \
-u ssid:authtoken

Could you show an my example mDashNotify() call that would be functionally equivalent to this curl example?

Thanks again!
-AD

#5

Off top of my head - not tested.

mDashNotify("HTTP.Request", "{%Q:%Q, %Q: %Q, %Q: %Q}",
   "url", "https://SSID:AUTHTOKEN@api.twilio.com/2010-04-01/Accounts/ssid/Messages.json",
   "method", "POST",
   "body", "To=18005556666&From=+18005551212&Body=Hello");
#6

lsm,

Thanks for the response.

Looks like you’re using username:password in the URL which is a deprecated authentication method.

Is there a way to use an equivalent of the curl username option (e.g. “–u ssid:authtoken”) via mDashNotify?

-AD

#7

Not at the moment - current API does not allow to set custom headers. It would be easy to add an extra param with headers though.

Note that the username/pass in the URL is deprecated for browsers.
When mDash makes HTTP request, it does not pass user/pass in the URL. Instead, it correctly constructs Authorization: Basic ... header. So my example would work fine, there is nothing deprecated about it.

#8

To clarify, mDash rewrites user/pass in the URL to the Auth header:

           http://user:pass@foo.com            http://foo.com
                                               Authorization: Basic XXXXXXX
                                              
DEVICE  ------------------------------>  mDash ---------------------------> foo.com
#9

Thanks for the explanation on the authentication, this makes more sense.

Having said all that, we’re still struggling to get mDashNotify() to work for HTTP POST requests. We can get things to work with the ESP-IDF HTTP client and with curl.

It would appear there is either a problem with mDashNotify() or a problem with the way we’re using it. Are there any debug options to see what final HTTP message is being generated from the mDash side and the HTTP response?

We’d prefer to avoid adding the unnecessary overhead of the ESP-IDF HTTP client, but may have not choice if we can’t get mDashNotify() to work.

Working ESP-IDF Example:

	  snprintf(url, sizeof(url), "https://%s/%s/Messages.json", g_config.alertSmsURL, g_config.alertSmsSID);

	  snprintf(message, sizeof(message), "Hello!");

	  snprintf(parameters, sizeof(parameters), "To=%s&From=%s&Body=\"%s\"",
			  g_config.alertSmsToNumber, g_config.alertSmsFromNumber, message);

	  esp_http_client_config_t config = {
			  .url = url,
		      .username = g_config.alertSmsSID,
		      .password = g_config.alertSmsAuthToken,
		      .auth_type = HTTP_AUTH_TYPE_BASIC,
			  .method = HTTP_METHOD_POST,
			  .transport_type = HTTP_TRANSPORT_OVER_TCP,
			  .timeout_ms = 5000,
			  .buffer_size = 1000,
	  };

	  esp_http_client_handle_t client = esp_http_client_init(&config);

	  esp_http_client_set_post_field(client, parameters, sizeof(parameters));

	  esp_err_t err = esp_http_client_perform(client);

Failing mDashNotify() Example

	  snprintf(url, sizeof(url), "https://%s:%s@%s/%s/Messages.json", g_config.alertSmsSID,
			  g_config.alertSmsAuthToken, g_config.alertSmsURL, g_config.alertSmsSID);

	  snprintf(message, sizeof(message), "Hello!");

	  snprintf(parameters, sizeof(parameters), "To=\"%s\"&From=\"%s\"&Body=\"%s\"",
			  g_config.alertSmsToNumber, g_config.alertSmsFromNumber, message);

	  mDashNotify("HTTP.Request", "{%Q:%Q, %Q:%Q, %Q:%Q}",
			  "url", url,
			  "method", "POST",
			  "body", parameters);
#10

Could you show the resulting url and body please?

#11

Here you go:

url:
https://2is3mwsKdFj54JHDeKD1gy3BzvPKGgF5tb:awg4glHKDIqmvubtSOqFN3DqB12WnmRT1B@api.twilio.com/2010-04-01/Accounts/2is3mwsKdFj54JHDeKD1gy3BzvPKGgF5tb/Messages.json

(note: the ssid/authtoken values in the url above are not valid but are the correct length)

body:
To="+15005550111"&From="+15005550006"&Body="Hello!"

We have tried with and without quotes as well as percent encoding for the values To, From, Body. None of those options have worked.

-AD

#12

I think + chars should be url-encoded.
Try this (notice %2b instead of + in the body):

mDashNotify("HTTP.Request", "{%Q:%Q, %Q: %Q, %Q: %Q}",
   "url", "https://SSID:AUTHTOKEN@api.twilio.com/2010-04-01/Accounts/ssid/Messages.json",
   "method", "POST",
   "body", "To=%2B18005556666&From=%2B18005551212&Body=Hello");
#13

Unfortunately no success with the %2B encoding. Calling mDashNotify() with hard-coded arguments (no printf) does not work either.

Since we’re still spinning our wheels here, it there a way to determine what final HTTP message mDash is generating upon reception of an mDashNotify() request from a device?

#14

Start a listening netcat on some server that is publicly accessible:

$ nc -l 1234

Then, make your device to request that URL:

mDashNotify("HTTP.Request", "{%Q:%Q, %Q: %Q, %Q: %Q}",
   "url", "https://SSID:AUTHTOKEN@YOUR_WORKSTATION_IP:1234/blah",
   "method", "POST",
   "body", "To=%2B18005556666&From=%2B18005551212&Body=Hello");

Make sure that mDash can see YOUR_WORKSTATION_IP, so it must not be behind a NAT.

You can use that netcat method to see what request is generated by curl command, then compare.