Skip to content

Commit c11aea4

Browse files
Twilio sample for app engine flex.
1 parent 72abbd2 commit c11aea4

File tree

11 files changed

+2588
-0
lines changed

11 files changed

+2588
-0
lines changed

appengine/flexible/twilio/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Twilio and Google App Engine Flexible Environment
2+
3+
This sample application demonstrates how to use [Twilio with Google App Engine](https://cloud.google.com/appengine/docs/flexible/php/using-sms-and-voice-services-via-twilio).
4+
5+
## Setup
6+
7+
Before running this sample:
8+
9+
1. You will need a [Twilio account](https://www.twilio.com/user/account).
10+
1. Update `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` in `app.yaml` to match your
11+
Twilio credentials. These can be found in your [account settings]
12+
(https://www.twilio.com/user/account/settings)
13+
1. Update `TWILIO_NUMBER` in `app.php` with a number you have authorized
14+
for sending messages. Follow [Twilio's documentation]
15+
(https://www.twilio.com/user/account/phone-numbers/getting-started) to set
16+
this up.
17+
18+
## Prerequisites
19+
20+
- Install [`composer`](https://getcomposer.org)
21+
- Install dependencies by running:
22+
23+
```sh
24+
composer install
25+
```
26+
27+
## Run locally
28+
29+
you can run locally using PHP's built-in web server:
30+
31+
```sh
32+
export TWILIO_ACCOUNT_SID=your-account-sid
33+
export TWILIO_AUTH_TOKEN=your-auth-token
34+
export TWILIO_NUMBER=your-twilio-number
35+
cd php-docs-samples/appengine/flexible/twilio
36+
php -S localhost:8080
37+
```
38+
39+
Now you can view the app running at [http://localhost:8080](http://localhost:8080)
40+
in your browser.
41+
42+
## Deploy to App Engine
43+
44+
**Prerequisites**
45+
46+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
47+
48+
**Deploy with gcloud**
49+
50+
```
51+
gcloud config set project YOUR_PROJECT_ID
52+
gcloud preview app deploy
53+
gcloud preview app browse
54+
```
55+
56+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
57+
in your browser.

appengine/flexible/twilio/app.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use Silex\Application;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
22+
$app = new Application();
23+
24+
$app['twilio'] = function ($app) {
25+
return new Services_Twilio(
26+
$app['twilio.account_sid'],
27+
$app['twilio.auth_token']
28+
);
29+
};
30+
31+
/***
32+
* Answers a call and replies with a simple greeting.
33+
*/
34+
$app->post('/call/receive', function () use ($app) {
35+
$response = new Services_Twilio_Twiml();
36+
$response->say('Hello from Twilio!');
37+
return new Response(
38+
(string)$response,
39+
200,
40+
['Content-Type' => 'application/xml']
41+
);
42+
});
43+
44+
45+
/***
46+
* Send an sms.
47+
*/
48+
$app->post('/sms/send', function (Request $request) use ($app) {
49+
/** @var Services_Twilio $twilio */
50+
$twilio = $app['twilio'];
51+
$sms = $twilio->account->messages->sendMessage(
52+
$app['twilio.number'], // From this number
53+
$request->get('to'), // Send to this number
54+
'Hello from Twilio!'
55+
);
56+
57+
return sprintf('Message ID: %s, Message Body: %s', $sms->sid, $sms->body);
58+
});
59+
60+
/***
61+
* Receive an sms.
62+
*/
63+
$app->post('/sms/receive', function (Request $request) use ($app) {
64+
$sender = $request->get('From');
65+
$body = $request->get('Body');
66+
$message = "Hello, $sender, you said: $body";
67+
68+
$response = new Services_Twilio_Twiml();
69+
$response->message($message);
70+
return new Response(
71+
(string) $response,
72+
200,
73+
['Content-Type' => 'application/xml']
74+
);
75+
});
76+
77+
return $app;

appengine/flexible/twilio/app.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
runtime: php
2+
vm: true
3+
4+
runtime_config:
5+
document_root: .
6+
7+
env_variables:
8+
TWILIO_ACCOUNT_SID: your-account-sid
9+
TWILIO_AUTH_TOKEN: your-auth-token
10+
TWILIO_NUMBER: your-twilio-number
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"twilio/sdk": "^4.10"
5+
},
6+
"require-dev": {
7+
"symfony/browser-kit": "^3.0",
8+
"paragonie/random_compat": "^2.0",
9+
"google/cloud-tools": "^0.3.0"
10+
}
11+
}

0 commit comments

Comments
 (0)