You’ve been employed as a developer in a company that owns and manages a CRM software. The CRM was developed a long time back and is quite legacy in terms of its capabilities. It doesn’t support JSON, only XML and currently polls external servers to check for updates so that it can notify your clients. All of that is going to change now that you’re here. Today we'll talk about how you can use the MessageMedia Webhooks Management API to optimize and improve the existing system.
Prerequisites
Installing Webhooks Node.js SDK
First things first, we’ll have to get rid of the not-so-pretty polling method. You’ll need a code editor such as Sublime Text or Atom. We’ll be using Node.js to develop our app, so you’ll need to make sure you’ve installed it on your machine as well. Now create an empty folder for your project, let’s name it “testApp”. Now, open up your command prompt, cd to the directory and run the following command to install the Webhooks SDK:
npm install messagemedia-webhooks-sdk
Creating a webhook
We’re going to create a webhook that will notify you when a customer receives an SMS. To do this, create an index.js file inside that folder and open it up in your editor. Write down the following code. I’ve commented it, so you can understand what’s going on with each line. https://gist.github.com/IbrahimTareq/0089efa22c3971a93314f397203f280f But wait a second, the CRM does not accept JSON. Don't worry, you can choose the format you’d like the payload to be sent back to you. You can pick XML, JSON or FORM_ENCODED. For this tutorial, we’ll go with XML. https://gist.github.com/IbrahimTareq/2490764149502cab1394ddbcc98a58ef Let's try and understand what's going on in the code. We’ve created a webhook for the URL http://webhook.com that will be triggered when an SMS is received. The event we've subscribed to is RECEIVED_SMS which will trigger the webhook when an SMS is received. You can add other events such as RECEIVED_MMS or FAILED_DR which will notify you when a message has failed to deliver. You can view the full list over here. The template attribute decides what will be returned via the response. For our example, the id, account_id, sent_time and received_time of the message will be sent back. Don’t forget to replace the placeholders with your actual credentials and then call the function at the end of your file using:
createWebhook();
You can run the file using this command:
node index
This is what a successful response body should look like.
Updating a webhook
What if you wanted to add additional attributes to the webhook? Maybe change the HTTP method of the webhook? Or even encode in a different format? Easy! To update a webhook, you’ll need the id of the webhook. You can retrieve this by looking at the response of the above example which should display the id of the webhook. For my webhook, it was 841bb8e8-48ab-4d02-8469-5ce6717ab452 And the code for this section is: https://gist.github.com/IbrahimTareq/75182fceb139a479c41bc5a5c1365873 We've changed a few attributes:
- the url has been changed to https://myurl.com
- the method has been changed to GET
- the message encoding has been set to FORM_ENCODED meaning the JSON payload will be sent as a form parameter
- the service_type attribute has been added to the template
Add this line after the code to call the function:
updateWebhook();
And a successful run of the script should result in something similar being shown in the console.<screenshot>
Deleting a webhook
One of your customers decided to unsubscribe from receiving notifications. You can easily remove their webhook from the system. Similar to the updateWebhook method, you’ll need an id to be able to delete a webhook. Once you’ve got it, replace it with the WEBHOOK_ID placeholder in the file and you’re ready to go. https://gist.github.com/IbrahimTareq/27d7b030318c2dfe3f22890b65621e89 Add this line after the code to call the function:
deleteWebhook();
And a successful response should output null in the command line:
Conclusion
Well done - you're now ready to blow the execs' minds with your new found knowledge and skills on how to use the MessageMedia Webhooks Management API.
Get the code
All the code for this tutorial is available in the Webhooks Node.js Tutorial Github repo
Resources