Jump to content

Dynamic Caller ID AND How To Make A Highly Available Pivot Script With AWS API GW


Recommended Posts

One thing we've been sorely missing for quite some time is the ability to quickly and easily create dynamic caller IDs for our clients. In this post I will give you a solution for how you can now do this and how to roll it out on your own! It gets a little technically dense. So I'm going to start as light as possible and then delve into the more complex parts as I go.

 

WARNING!!! These URLs are strictly for demo only! I don't recommend you use these URLs in production at all. I say this not because I foresee any  stability or scale-ability issues. Quite the opposite, this can scale to the moon and back...then back again and it bills to me. So, you have been forewarned. This costs $4 per million requests, so if you guys get too crazy with it, I may shut it down without notice. I mean it'd have to get pretty crazy, but still, at your own risk here.

 

So, here's the two functions I've implemented and how you can add them to your accounts.

.

 

Static Caller ID

https://api.telkit.tk/scid/NUMNUMBER

This feature will allow you to create callflows that calls out using alternate pre-defined numbers.

 

What you will need:

- Account ID to activate

- An auth token

- An available feature code number (check for conflicts! 80-89 seem to be safe)

- The number you'd like to "spoof"

Then, run this command from a *nix device:

Quote

curl -X PUT http://ui.zswitch.net/v2/accounts/ACCOUNTID/callflows/ -H "Content-Type:application/json" -H "X-Auth-Token:AUTHTOKEN" -d '{"data":{"flow":{"data":{"method":"GET","req_timeout":"5","req_format":"kazoo","voice_url":"https://api.telkit.tk/scid/NUMBERTOSPOOF","debug":false},"module":"pivot","children":{}},"numbers":[],"patterns":["^\\*FEATURECODE"],"contact_list":{"exclude":true}}}'

 

Takes a dial string like *80*OUTBOUNDNUMBER and calls the outbound number from the predefined spoofed number from above.

 

Fully Dynamic Caller ID

https://api.telkit.tk/dcid/

This feature will allow you to setup a feature that lets them call out under LITTERALLY any number, not just numbers that you pre-define for them.

 

What you will need:

- Account ID to activate

- An auth token

- An available feature code number (check for conflicts! 80-89 seem to be safe)

Then, run this command from a *nix device:

Quote

curl -X PUT http://ui.zswitch.net/v2/accounts/ACCOUNTID/callflows/ -H "Content-Type:application/json" -H "X-Auth-Token:AUTHTOKEN" -d '{"data":{"flow":{"data":{"method":"GET","req_timeout":"5","req_format":"kazoo","voice_url":"https://api.telkit.tk/dcid/","debug":false},"module":"pivot","children":{}},"numbers":[],"patterns":["^\\*FEATURECODE"],"contact_list":{"exclude":true}}}'


This takes a dial string like *80*SPOOFEDNUMBER*OUTBOUNDNUMBER and calls the outbound number as the spoofed number.

.

 

Getting  your auth token

If you don't know how to get an auth token, here's some instructions for that...

Run this command from a *nix box:

echo -n 'YOURUSERNAME:YOURPASSWORD' | md5sum

This will give you an MD5 for the next command...

curl -X PUT http://ui.zswitch.net/v2/user_auth -H "Content-Type:application/json" -d '{ "data" : { "credentials" : "MD5HASH", "account_name" : "ACCOUNTNAME" } }' 2> /dev/null | egrep -o '"auth_token":"([^"]|\\")*"'

This will give you an auth token.

 

Try this out, if you like it read my next three posts on how to roll each function for yourself and a few last thoughts at the end.

Edited by Rick Guyton (see edit history)
Link to comment
Share on other sites

Creating The DCID AWS API Gateway Service

  1. Create the new API inside the Amazon API Gateway Service.
    • Select New API
    • Name Your API
    • Click Create API
    • image.thumb.png.a6f880e06507b981054b151b08689e39.png
  2. Create your first resource in your fresh new API
    • Click Actions
    • Then Click Resource
    • image.thumb.png.787fa34f861a7a54abed24140d118879.png
  3. Name Your Resource
    • Enter your Resource name as dcid
    • Same for Resource Path
    • Click Create Resource
    • image.thumb.png.c5dba26ee8b0c87e9fc16c989db10e4e.png 
  4. Make a new Method
    • Click Actions
    • Then, Create Method
    • image.png.d747824f71247e33b3600da62c5ff0fe.png
    • Then Select GET from the drop down
    • Click the check mark
    • image.png.ca6a371f0abc963618a49e146d474ac4.png
  5. Setup your new method
    • Next, you will see options to setup your new method, select mock as your integration type
    • Then click save
    • image.thumb.png.89acb3c9249d49e1e81b9d3554be782b.png 
  6. Setup your method's execution
    • Fortunately, Kazoo submits and accepts JSON and these are the default formats in AWS API GW. So, there's no need to mess with the Integration Request OR Method Response. The default are good for us.
    • But, we do have work to do in Method Request and Integration Response. I'll break these into separate sections though.
    •   image.thumb.png.0619f6347536a66a1ac7337f7c670890.png
  7. Setting up the Method Request
    • Click the method request (reference Step 6) 
    • Under Request Validator, select Validate query string parameters and headers.
    • Then, click the check mark.
    • Next, click Add Query String
    •  image.thumb.png.0edc068d9d4ed3720203894be0a08e88.png
    • Enter "To" as your query string name
    • Click the check mark
    • image.thumb.png.3149ff22dc197b8848d55870caa6283f.png
    • Next, click the required check box on your TO query string
    • This wraps up your Method Request, Click the <- Method Execution link to go back to the screen in step 6
    • image.thumb.png.b90c41ae57baba6905893ca9ba66380e.png
  8. Setting Up The Integration Response
    • Expand the 200 HTTP status
    • Expand Body Mapping Templates
    • Select The application/json Content Type
    • Paste The Script at the bottom of this post here
    • Click Save
    • image.thumb.png.609317535e909a87c4848435aad05bac.png 
  9. DEPLOY!!!
    • Click Actions
    • Then click Deploy API
    •  image.png.5d7753f86dc4a4ce65f3fc628d5e0206.png
    • Select New Stage for deployment stage
    • Name your stage (I picked v1, but it could be anything)
    • Finally Click Save
    • image.png.28e30f7c509ecbad3a01616aa2978784.png
  10. Finally, you will be re-directed to your stage editor. You can now use your invoke URL for your very own pivot DCID!

DCID Template:

## This API assumes a format of *SOMEFEATURECODE*SpoofedCID*DestNum
## Here we split out the CID to spoof and the destination number
#set($SplitCIDString = $input.params('To').split("\*"))
## And, now we return it to Kazoo in a format it knows how to use
{
  "children": {
    "_": {
      "data": {
        "to_did": "$SplitCIDString[3]",
        "use_local_resources": false
      },
      "module": "resources"
    }
  },
  "data": {
    "caller_id_number": "$SplitCIDString[2]"
  },
  "module": "set_cid"
}

 

Edited by Rick Guyton (see edit history)
Link to comment
Share on other sites

Creating The SCID AWS API Gateway Service

  1. We will be using the same API as before, so go into your Demo API (or whatever you called it) and click on resources. We'll start there. You should see your dcid resource from the previous section. We won't be doing anything more with dcid, but I just want to be sure you are in the right place.
    • image.thumb.png.cd008b4076673f604fec9cbd2f2c726d.png
  2. Create your second resource
    • Click Actions
    • Then Click Resource
    • image.thumb.png.4ad849db01e292715836b83f12389a3c.png
  3. Name Your Resource
    • Enter your Resource name as dcid
    • Same for Resource Path
    • Click Create Resource
    • image.thumb.png.de4ca1a4f992fbe1d3ab1629654a335c.png 
  4. Create a child resource under the scid resource
    • Make sure scid is highlighted
    • Click actions
    • Then click create resource
    • image.thumb.png.76b4ab947da785147e1693dd0abecd71.png
  5. Name Your Child Resource
    • Enter your Resource name as number
    • VERY IMPORTANT!! Enter your Resource Path as {number}. EXACTLY!
    • Click Create Resource
    • image.thumb.png.8793ef276767c006f8feeb98b9e1de94.png
  6. Make a new Method
    • Click Actions
    • Then, Create Method
    • image.png.d747824f71247e33b3600da62c5ff0fe.png
    • Then Select GET from the drop down
    • Click the check mark
    • image.png.ca6a371f0abc963618a49e146d474ac4.png
  7. Next, follow steps 4-8 in the above post on dcid. Obviously use the code block from this post when you get to step 8. You should end up with something that looks like this:
    • image.thumb.png.3556c3947251d47f1dc273b610ac8a44.png 
  8. Re-deploy your API!
    • Click Actions
    • Then click Deploy API
    •  image.png.6a07dd7fb864168e29efbe6f3a6e8bc2.png
    • Select your existing stage you created in the previous post
    • And hit deploy!
    • image.png.76d9b7c48a919e1fe241ffb340bb1c76.png

SCID Template:

## The destination number we get from Kazoo still has the feature
## code attached to it. So, we need to strip it off.
## The feature code must end with a *. So, here, we split the
## the string on *s, take the array size and subtract one.
## this will give us the vaule after the final * sign, our number
## to dial.
#set($SplitNum = $input.params('To').split("\*"))
#set($DestNumIndex = $SplitNum.size() - 1)
#set($NumToCall = $SplitNum[$DestNumIndex])
## After we have that, it gets pretty easy.
## Now we just present it back to Kazoo in a way it expects.
{
  "children": {
    "_": {
      "data": {
        "to_did": "$NumToCall",
        "use_local_resources": false
      },
      "module": "resources"
    }
  },
  "data": {
    "caller_id_number": "$input.params('number')"
  },
  "module": "set_cid"
}

Thats it! There's a few things you might still want to do, like test your APIs and change the URL. I'll touch on those in my last post.

Edited by Rick Guyton (see edit history)
Link to comment
Share on other sites

Wrap Up

Why AWS API Gateway?

Well, API gateway is incredibly reliable and has nearly infinite horizontal scalability. It's also stupidly cheap. Just $4 per MILLION transactions when using Mock integrations. But, equally important to me was the Lambda functions you can trigger. With Lambda, you can execute C#, Java, Node.js or Python scripts in response to API calls. No servers, no maintenance, nearly infinite scalability and you only pay for your AWS GW fee and the time (to the second) that your code snipit takes to run. It's awesome.

Why the mock integration?

As far as I can tell, mock is free to use. As opposed to running a lambda script. Sure, the lambda script is super cheap. But, why do that when you can have free? For these API functions, all the data we need is given to us by Kazoo. It's just a minor reformatting of the data that we need. And this is a perfect use case for mock.

Testing your APIs

If you click on the GET method of either of these API functions I've provided you with, you will see a blue lightning bolt icon. Click that, can you will be able to provide your API functions with test data to see how they behave. This works with any AWS API query, not just mock ones. Very useful when developing new functions!

Custom Domain

If you want to use your own custom domain like I did with telkit.tk, follow these instructions: http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html. Otherwise, you can just use the URI provided by AWS. It will work equally well either way.

Last Note

The real intention behind this post is to cut the FUD behind using pivots in general and in using AWS GW. I'm very much hoping this tutorial makes it easier for you all to develop your own AWS API GW functions. Maybe even share them! Pivots are an insanely powerful tool that I think don't get enough attention. Have fun!!

Edited by Rick Guyton (see edit history)
Link to comment
Share on other sites

  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...