# Send an One Time Password (OTP) via the MessageBird SMS gateway

**URL:** <https://community.f5.com/t/send-an-one-time-password-otp-via-the-messagebird-sms-gateway/69101>\
**Category:** Community CodeShare\
**Tags:** irules, big-ip-access-policy-manager-apm, nodejs, 2fa, application-delivery\
**Created:** [January 12, 2018, 4:01pm UTC](https://community.f5.com/t/send-an-one-time-password-otp-via-the-messagebird-sms-gateway/69101 "2018-01-12T16:01:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niels\_van\_Sluis](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/niels_van_sluis/32/3759_2.png) [@Niels\_van\_Sluis](https://community.f5.com/u/Niels_van_Sluis)\
**Post date:** [January 12, 2018, 4:01pm UTC](https://community.f5.com/t/send-an-one-time-password-otp-via-the-messagebird-sms-gateway/69101/1 "2018-01-12T16:01:21Z")

</div>

**Problem this snippet solves:**

This snippet makes it possible to send an One Time Password (OTP) via the MessageBird SMS gateway. This snippet uses iRuleLX and the node.js messagebird package to interact with the MessageBird API.

**How to use this snippet:**

**Prepare the BIG-IP**

- Provision the BIG-IP with iRuleLX.
- Create LX Workspace: messagebird
- Add iRule: messagebird\_irule
- Add Extension: messagebird\_extension
- Add LX Plugin: messagebird\_plugin → From Workspace: messagebird

**Install the node.js messagebird module**

```tcl
# cd /var/ilx/workspaces/Common/messagebird/extensions/messagebird_extension
# npm install messagebird --save  
messagebird@2.1.4 node_modules/messagebird
#

```

_irule_

To make it works, you need to install the irule on the Virtual Server that publish your application with APM authentication.

_access profile_

If you already have an existing access profile, you will need to modify it and include some additionnal configuration in your VPE. If you have no access profile, you can starts building your own based on the description we provide below.

**Configuring the Visual Policy Editor**

The printscreen below is a minimal Visual Policy Editor used to make MessageBird OTP Authentication works properly :

 ![0151T000003d9DPQAY.PNG](https://d20hrnpixdzcsd.cloudfront.net/original/2X/0/0cd54c855d1d67ab96531e8815c71692f21cdbe1.png)

##### [For a larger version of this image please download here.](https://cdn.f5.com/websites/devcentral.f5.com/downloads/ap_messagebird.png)

_Irule Event – MessageBird_

This is an irule event with the ID set to ‘MessageBird’. This will trigger the messagebird\_irule to come into action.

_MessageBird Status_

This is an empty action with two branches. The branch named “successful” contains the following expression :

```tcl
expr { [mcget {session.custom.messagebird.status}] contains "successful" }

```

_Message Box_

This is a message box that will inform the user that there was a failure sending the One Time Password.

**messagebird\_irule**

```auto
###
### Name : messagebird_irule
### Author : Niels van Sluis, <niels@van-sluis.nl>
### Version: 20180721.001
### Date : 2018-07-21
###

when ACCESS_POLICY_AGENT_EVENT {
    if { [ACCESS::policy agent_id] eq "MessageBird" } {

        # Set MessageBird access key    
        set accessKey "<ACCESS_KEY>"

        # Set user-friendly message which will be send prior to the OTP itself
        set message "Your OTP is: "

        # Set username used for logging purposes only
        set username "[ACCESS::session data get session.logon.last.username]"

        # Set OTP generated by BIG-IP APM which will be added to the SMS message.
        set generatedOTP "[ACCESS::session data get session.otp.assigned.val]"

        # Set phonenumber to send the SMS to
        set telephoneNumber "[ACCESS::session data get session.ad.last.attr.telephoneNumber]"

        # Set the sender of the message. This can be a telephone number (including country code) or an alphanumeric string.
        # In case of an alphanumeric string, the maximum length is 11 characters.
        set sender "MessageBird"

        if {[info exists username] && ($username eq "")} {
            log local0. "Error: username variable is empty; no OTP sent."
            return
        }

        if {[info exists generatedOTP] && ($generatedOTP eq "")} {
            log local0. "Error: generatedOTP variable is empty; no OTP sent for user $username."
            return
        }

        if {([info exists telephoneNumber] && $telephoneNumber eq "")} {
            log local0. "Error: telephoneNumber variable is empty; no OTP sent for user $username."
            return
        }

        set rpc_handle [ILX::init messagebird_plugin messagebird_extension]
        if {[catch { ILX::call $rpc_handle sendOTP $generatedOTP $telephoneNumber $sender $message $accessKey } result] } {
            log local0. "sendOTP failed for telephoneNumber: $telephoneNumber, ILX failure: $result"
            return
        }

        log local0. "MessageBird status for user $username ($telephoneNumber): $result"
        ACCESS::session data set session.custom.messagebird.status $result
    }
}

```

**Code :**

```auto
/**
*** Name : messagebird_extension
*** Author : Niels van Sluis, <niels>
*** Version: 20180721.001
*** Date : 2018-07-21
**/

// Import the f5-nodejs module.
var f5 = require('f5-nodejs');

// Create a new rpc server for listening to TCL iRule calls.
var ilx = new f5.ILXServer();

ilx.addMethod('sendOTP', function(req, res) {
    var generatedOTP = req.params()[0];
    var telephoneNumber = req.params()[1];
    var sender = req.params()[2];
    var message = req.params()[3];
    var accessKey = req.params()[4];
    
    var params = {
        'originator': sender,
        'recipients': [telephoneNumber],
        'body': message + generatedOTP
    };
    
    var messagebird = require('messagebird')(accessKey);
    
    messagebird.messages.create(params, function (err, response) {
        if (err) {
            //console.log(err);
            return res.reply('failed');
        }
        //console.log(response);
        return res.reply('successful');
    });
    
});

// Start listening for ILX::call and ILX::notify events.
ilx.listen();</niels>

```

**Tested this on version:**

13.0

---

<div class="post-metadata">

**Author:** ![Manuel\_Cristob2](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Manuel\_Cristob2](https://community.f5.com/u/Manuel_Cristob2)\
**Post date:** [March 8, 2018, 2:55pm UTC](https://community.f5.com/t/send-an-one-time-password-otp-via-the-messagebird-sms-gateway/69101/2 "2018-03-08T14:55:12Z")

</div>

Hi Niels, very useful information but I am trying to use Twilio instead of MessageBird as it is a customer’s requirement.

Do you know how/where can I get the node.js for Twilio?

I have found this link but it looks confusing to me. Do I have to build the node.js or can i download it and install it in the F5?

[https://www.twilio.com/docs/guides/how-to-send-sms-messages-in-node-jssign-up-for-a-twilio-account](https://www.twilio.com/docs/guides/how-to-send-sms-messages-in-node-jssign-up-for-a-twilio-account)

Much appreciated

Also.. could you please paste the APM policy a little bigger? It is hard to read and follow

Regards

Manuel

---

<div class="post-metadata">

**Author:** ![Niels\_van\_Sluis](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/niels_van_sluis/32/3759_2.png) [@Niels\_van\_Sluis](https://community.f5.com/u/Niels_van_Sluis)\
**Post date:** [March 9, 2018, 5:06pm UTC](https://community.f5.com/t/send-an-one-time-password-otp-via-the-messagebird-sms-gateway/69101/3 "2018-03-09T17:06:27Z")

</div>

Hi Manuel,

¬†

See here, a new snippet that will get you going.

¬†

[https://devcentral.f5.com/s/articles/send-an-one-time-password-otp-via-the-twilio-sms-gateway-1132](https://devcentral.f5.com/s/articles/send-an-one-time-password-otp-via-the-twilio-sms-gateway-1132)

¬†

And about the APM policy picture, try using the ‘zoom’ function of your browser. The details are in there.

¬†

Kind regards,

¬†

Niels

¬†

---

<div class="post-metadata">

**Author:** ![Manuel\_Cristob2](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Manuel\_Cristob2](https://community.f5.com/u/Manuel_Cristob2)\
**Post date:** [March 9, 2018, 5:34pm UTC](https://community.f5.com/t/send-an-one-time-password-otp-via-the-messagebird-sms-gateway/69101/4 "2018-03-09T17:34:57Z")

</div>

Very much appreciated …

This information will help for sure.

Regards Manuel
