Comments (12)
Yeah this seems to be a test issue. I think we should be using some parts of twilio-run for the tests actually @philnash. I was thinking about that the other day but haven't had time to work on it. I'll file an issue for this.
But yeah for your case you'd be fine with your implementation and set the testing to pending for now. The only nitpick is that the default should be <Response />
with a capital R
from function-templates.
Annotated Source Code
based on a snapshot of Echo TwiML source code (echo.php)
sent to me by Twilio support by email on 2019-07-08
This is PHP code.
<?php
It is shared by Twilio under the MIT license.
/*
Copyright (c) 2012 Twilio, Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
Send Content-Type header for an XML document (TwiML is XML).
// send XML header
header('Content-type: text/xml');
Send the contents of the Twiml
parameter as response.
// echo the TwiML passed into the URL
echo $_GET['Twiml'];
(there is no check that we received valid TwiML at this point, invalid TwiML will fail further in the flow)
from function-templates.
Test Case
[Echo-1] Successful Echo
Input
Parameter | Value |
---|---|
Twiml |
<Response><Say>echo okay</Say></Response> |
Output
$ curl -s https://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3Eecho%20okay%3C%2FSay%3E%3C%2FResponse%3E \
| xmllint --format -
<?xml version="1.0"?>
<Response>
<Say>echo okay</Say>
</Response>
from function-templates.
Implementation
I created a first version of the Echo Funlet.
I started by developing it online by creating a new Twilio Function in my Twilio account:
exports.handler = function(context, event, callback) {
// echo the TwiML passed into the `Twiml` property/parameter
callback(null, event.Twiml || "<response/>" );
};
This implementation works but does not set the Content-Type
to text/xml
:
content-type: text/plain; charset=utf8
I found in the documentation how to construct a response with custom headers, using an instance of Twilio.Response
:
exports.handler = function(context, event, callback) {
let response = new Twilio.Response();
response.appendHeader("Content-Type", "text/xml");
// echo the TwiML passed into the `Twiml` property/parameter
response.setBody( event.Twiml || "<response/>" );
callback(null, response);
};
This works and returns the expected Content Type:
content-type: text/xml; charset=utf8
But when trying to do the same, including unit tests, in the environment of this project, I managed to define the first implementation, but the second one, using Twilio.Response
fails:
TypeError: Twilio.Response is not a constructor
7 |
8 | // Create a custom response in XML format
> 9 | let response = new Twilio.Response();
@dkundel is there something that I should be aware of?
from function-templates.
I donβt think this is something I implemented for the custom test harness, which you can see here: https://github.com/twilio-labs/function-templates/blob/master/test/test-helper.js. Will have to look into this for the more complicated responses like this (ironic for such a simple twimlet!). You can probably set the tests as pending for now if you want to carry on with implementation.
from function-templates.
You can probably set the tests as pending for now if you want to carry on with implementation.
I'll do that. Thanks.
from function-templates.
Unit Tests
PASS funlet-echo/funlet-echo.test.js
β [ECHO-INPUT-TWIML-1] Read Twiml from Event (3ms)
β [ECHO-INPUT-TWIML-2] Read Twiml from Context
β [ECHO-INPUT-TWIML-3] Read Default Twiml from Script
β [ECHO-OUTPUT-ECHO-1] echo() shall return its input (1ms)
β skipped 1 test
OK for now: the skipped test is [ECHO-1] Full Response
, which depends on the missing Twilio.Response
to run.
from function-templates.
Integration Tests
After deploying the funlet to my Twilio account, with the Check for valid Twilio signature
unchecked, and setting DOMAIN
to my runtime domain, running this script allows to check that we get the same output with the Funlet as with the Twimlet:
$ ./test-echo.sh "https://$DOMAIN.twil.io/echo"
[Echo-1] Successful Echo
<?xml version="1.0"?>
<Response>
<Say>echo okay</Say>
</Response>
The comparison is not actually done in the script, but can be done as a second step, in case of doubt:
$ ./test-echo.sh https://twimlets.com/echo > /tmp/echo.twimlet.txt
$ ./test-echo.sh "https://$DOMAIN.twil.io/echo" > /tmp/echo.funlet.txt
$ diff /tmp/echo.twimlet.txt /tmp/echo.funlet.txt
from function-templates.
Ooooooh I love that π
from function-templates.
I donβt think this is something I implemented for the custom test harness, which you can see here: https://github.com/twilio-labs/function-templates/blob/master/test/test-helper.js.
@philnash I was not using the test helper. I have added it to the unit tests for the Echo Funlet, and with minimal modifications of my tests, it works fine for my purpose.
In retrospect, my confusion stems from the fact that I expected the Twilio
object to be the Twilio Node Helper library imported using require()
. I have now noticed that it is actually a special global
object set up by the Twilio runtime environment, and none of the other Twilio Function templates import it.
Is there a GitHub project where I can find the definition of this special global
Twilio
object used in Twilio Functions?
from function-templates.
The Twilio
object is the Node helper library with a Response
object, that is only used in Functions, also defined. twilio-run
defines that here and this is the implementation of Response
,and the public definition is in the docs here.
from function-templates.
Thanks! I see the light π‘!
from function-templates.
Related Issues (20)
- cannot install the plugin - error [email protected]: The engine "node" is incompatible with this module HOT 3
- Unclear SMS Notifications UI
- Link to `LICENSE` in PR template is a 404 HOT 2
- The template "video" doesn't exist HOT 6
- Debugging help request (simulring funlet) HOT 3
- Verify sample app error HOT 5
- SIP Quickstart: SIP credential password requirements not obvious
- Passcode error on the video sample HOT 5
- SIP Quickstart: /admin/index.html unable to login (error 403 unauthorised) HOT 1
- Unable to find the css code for the classes used in the "verify" function-template HOT 2
- Assign Reviews action doesn't work
- Sid: event.Sid -> Sid: event.SmsSid HOT 3
- Install Issue HOT 1
- Simulring Error Line 146
- iPhone video problem using the code exchange Basic Video Chat (severless) HOT 1
- Migration of 'forward-message-sendgrid' to @sendgrid/mail due to ES Module incompatibility with Twilio Functions HOT 1
- // Install the C# / .NET helper library from twilio.com/docs/csharp/install using System; using Twilio; using Twilio.Rest.Verify.V2.Service; class Program { static void Main(string[] args) { // Find your Account SID and Auth Token at twilio.com/console // and set the environment variables. See http://twil.io/secure string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"); string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN"); TwilioClient.Init(accountSid, authToken); var verification = VerificationResource.Create( to: "+15017122661", channel: "sms", pathServiceSid: "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ); Console.WriteLine(verification.Status); } } HOT 1
- How to retrieve date and time HOT 1
- Notify Services Depreciated HOT 2
- Install CLI twillio fails using apt HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from function-templates.