Text Notification

Hello,

My requirement is to send text notification to user whenever there has been an update on the document. I see that this can be possible with database triggers and twilio service.
Recent mongodb docs says, third party services (including twilio services) were deprecated.
I would like to know what is the other way that I can work to meet my requirement.

Thank you,
Sunita

Hi, our current recommendation is just to use the twilio sdk to make your calls within App Services functions (instead of us having a dedicated twilio service).

See here for more details:

  1. https://www.mongodb.com/docs/atlas/app-services/functions/dependencies/
  2. Realm Logs: Alert Logging System with Twilio | by Josman Pérez Expóstio | Realm Blog | Medium

I already have a database collection. So, in order to send document update notifications to twilio number, do we need to build realm application, as mention in your second blog?

Im not sure what you mean by "database collection. I think the simplest version of what you want to do is:

  1. You have a cluster with a collection and you want updates to any documents in that collection to trigger a text message
  2. You create an App Services app
  3. You create a value with your twilio api key
  4. You create a function and upload the twilio sdk as a dependency (like you would in npm)
  5. You create a database trigger on that collection and make it run that function

Done! Does that sound right to you?

Understood. I did the way you explained. Thank you so much.

When I created simple function, I am getting error as

ran at *****
took
error:
Error: password is required

Function is as below:
exports = function(changeEvent) {

const accountSid = context.values.get(“Twilio_SID_Value”);
const authToken = context.values.get(“Auth_Token_Twilio”);
const client = require(‘twilio’)(accountSid, authToken);

client.messages.create({
from: ‘+11111111’,
to: ‘+22222222’,
body: ‘Document with name was updated.’,
});

}

Not sure why I am getting this error. Could you please let me know where I am doing wrong?

Can you send a link to your app in the UI? Also, not that you should run it like this in production, but can you try just pasting the paintext into the function editor to see if that gets it to work?

I am looking at this and it seems like that is an error from the Twilio SDK: javascript - twilio error 'username required' - Stack Overflow

Thank you. I am doing this in function editor only. Will try one more time today. Thank you for your time and support.

Hi Tyler,
I was able to fix the issue with the help of environment variables and send message to client via twilio. Thanks for all your support.
Now I trying to send specific message like instead of “Customer record got updated” trying to send “Customer with customer id got update”.
Message is being delivered as “Customer with id [object Object] was updated”.

Code is as below:
const body = Customer with id ${change_event._id} was updated;
Message delivered as-Customer with id [object Object] was updated
const body = Patient with id ${change_event.fullDocumentBeforeChange} was updated;
Message delivered as-Patient with id undefined was updated.

All I am trying to do is, capture specific field in the document that got updated.
Any idea how I can fix this?

Hi, this is just a normal javascript issue, however, the basics is that you are trying to print something that is an object and not a string. I would reccomend using console.log() to figure out what you are passing in and re-reading the Change Event documentation.

I suspect you might want something like “change_event.documentKey._id.toHexString()” or “change_event.fullDocumentBeforeChange._id.toHexString()”

They didn’t work. I’ll look into it. Thank you for your response.