Last active
July 22, 2022 13:37
-
-
Save byteofprash/ec84bde43bb45b505a967d7a4d58ac07 to your computer and use it in GitHub Desktop.
Async Twilio Studio and serverless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
// This is your new function. To start, set the name and path on the left. | |
exports.handler = async function(context, event, callback) { | |
// Here's an example of setting up some TWiML to respond to with this function | |
const twiml = ` | |
<?xml version="1.0" encoding="UTF-8"?> | |
<Response> | |
<Play>http://com.twilio.sounds.music.s3.amazonaws.com/ClockworkWaltz.mp3</Play> | |
</Response> | |
`.trim(); | |
axios.get(`https://defaultservice-4013.twil.io/slowFunc?callSID=${event.callSID}`); | |
const client = context.getTwilioClient(); | |
client.calls(event.callSID).update({ twiml }) | |
.then(call => { | |
console.log(call, " is the updated call") | |
return callback(null, "twiml") | |
}) | |
.catch(err => { | |
console.log("I ended in an error:", err) | |
}) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flowSid = "FWc3f0cae1142d2c6e36733b2231044062" | |
const axios = require('axios') | |
exports.handler = async function(context, event, callback) { | |
axios.get("https://deelay.me/5000/http://echo.jsontest.com/score/1000") | |
.then(resp =>{ | |
console.log("score is", resp.score, resp); | |
const twiml = ` | |
<?xml version="1.0" encoding="UTF-8"?> | |
<Response> | |
<Redirect>https://webhooks.twilio.com/v1/Accounts/${context.ACCOUNT_SID}/Flows/${flowSid}?FlowEvent=return&score=${resp.data.score}</Redirect> | |
</Response> | |
`.trim(); | |
const client = context.getTwilioClient(); | |
client.calls(event.callSID).update({ twiml }) | |
.then(call =>{ | |
console.log("Done"); | |
return callback(null, "") | |
}) | |
}) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"description": "A New Flow", | |
"states": [ | |
{ | |
"name": "Trigger", | |
"type": "trigger", | |
"transitions": [ | |
{ | |
"event": "incomingMessage" | |
}, | |
{ | |
"next": "GreetUser", | |
"event": "incomingCall" | |
}, | |
{ | |
"event": "incomingConversationMessage" | |
}, | |
{ | |
"event": "incomingRequest" | |
}, | |
{ | |
"event": "incomingParent" | |
} | |
], | |
"properties": { | |
"offset": { | |
"x": 0, | |
"y": 0 | |
} | |
} | |
}, | |
{ | |
"name": "GreetUser", | |
"type": "say-play", | |
"transitions": [ | |
{ | |
"next": "redirect_1", | |
"event": "audioComplete" | |
} | |
], | |
"properties": { | |
"voice": "alice", | |
"offset": { | |
"x": -370, | |
"y": 250 | |
}, | |
"loop": 1, | |
"say": "Hello. Thanks for calling. Please wait while we process your score", | |
"language": "en-US" | |
} | |
}, | |
{ | |
"name": "say_play_3", | |
"type": "say-play", | |
"transitions": [ | |
{ | |
"event": "audioComplete" | |
} | |
], | |
"properties": { | |
"voice": "alice", | |
"offset": { | |
"x": 350, | |
"y": 730 | |
}, | |
"loop": 1, | |
"say": "Thanks for waiting. Your score is {{widgets.redirect_1.score}}", | |
"language": "en-US" | |
} | |
}, | |
{ | |
"name": "redirect_1", | |
"type": "add-twiml-redirect", | |
"transitions": [ | |
{ | |
"next": "say_play_3", | |
"event": "return" | |
}, | |
{ | |
"event": "timeout" | |
}, | |
{ | |
"event": "fail" | |
} | |
], | |
"properties": { | |
"offset": { | |
"x": -50, | |
"y": 460 | |
}, | |
"method": "GET", | |
"url": "https://handler.twilio.com/twiml/EH4e929aa9c31353aa0a764bdd5d1481bf?callSID={{trigger.call.CallSid}}", | |
"timeout": "14400" | |
} | |
} | |
], | |
"initial_state": "Trigger", | |
"flags": { | |
"allow_concurrent_calls": true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Response> | |
<Redirect>https://defaultservice-4013.twil.io/process?callSID={{callSID}}</Redirect> | |
</Response> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment