Adding SMS alerts for deployments to Octwilio
Tags: devops, twilio, octopus, twiliohackathon
Published: 2020-04-30 19:34:56

---
title: Adding SMS alerts for deployments to Octwilio
published: true
description: Building Octwilio part 4! I add a function to Octwilio to handle deployment events raised by Octopus Deploy.
tags: devops,twilio,octopus,twiliohackathon
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/u2amglosrju6r5xt4yey.jpg
series: Building Octwilio
---

## Song recommendation of the day <a name="song"></a>

Today's song recommendation is an iconic track from one of the best Spider-Man movies.
{% spotify spotify:track:5zsHmE2gO3RefVsPyw2e3T %}

## The plan <a name="the-plan"></a>

[In the last post](https://dev.to/octopus/reacting-to-messages-sent-to-twilio-1ppb), I set up a cloud function to handle messages to my Twilio number.

The `processMessage` function parses the message and calls the Octopus API to approve or reject the deployment.

Today, I set up a cloud function for handling deployment events. I also added information to the [README](https://github.com/ryanrousseau/octwilio/blob/main/README.md) on cloning the project and deploying it for your use.

## New Firebase config settings <a name="firebase-config"></a>

I added new config settings for the phone numbers used for deployment notifications. You can use the same numbers as the approval notification settings or choose new ones.

```bash
firebase functions:config:set octwilio.twilio.deployment.from_number="+15555555555"
firebase functions:config:set octwilio.twilio.deployment.to_number="+15555555555"
```

## The deploymentEventRaised function <a name="the-function"></a>

The `deploymentEventRaised` function is straightforward. I reused existing functions to create a workflow like the `approvalRaised` function.

There is an opportunity to make a generic function for handling subscription events that I will look at later.

```javascript
function convertPayloadToOptions(payload) {
    let options = {
        message: payload.Event.Message,
        toNumber: config.twilio.deployment.to_number,
        fromNumber: config.twilio.deployment.fromNumber
    }

    return options;
}

exports.deploymentEventRaised = functions.https.onRequest((req, res) => {
    return octopus.authorizeRequest(req, res)
        .then(octopus.getSubscriptionPayload)
        .then(convertPayloadToOptions)
        .then(octwilio.sendTwilioMessage)
        .then(() => { return res.status(200).send(); });
});
```

## The Octopus subscription <a name="the-subscription"></a>

I set up a new subscription to fire on `Deployment failed`, `Deployment started`, and `Deployment succeeded` events.

I set the subscription to send the event payload to my new function URL.

![The Octopus subscription](https://dev-to-uploads.s3.amazonaws.com/i/ka0xqp54sy43kbmh4gfd.png)

## Testing it out <a name="testing-it-out"></a>

I created a new release and deployed it. I now get messages when the deployment starts and finishes. I can still approve the deployments with the same number.

![Series of messages to and from Twilio](https://dev-to-uploads.s3.amazonaws.com/i/4isudwjhxa83eypm6o4e.jpg)

## Conclusion <a name="conclusion"></a>

That is it for this stretch of `Building Octwilio`. I will add any future additions to the series.

_Cover photo by [Markus Spiske](https://unsplash.com/@markusspiske) on [Unsplash](https://unsplash.com/)._