Skip to content

Alexa skill to retrieve the next bus, using PHP on AWS Lambda

License

Notifications You must be signed in to change notification settings

cabdesigns/alexa-next-bus-php

Repository files navigation

Alexa Next Bus PHP

This project is an example serverless function that can be used to power a PHP based Alexa Skill, running on AWS Lambda, to fetch the next bus times from a number of configured bus stops (using the TransportAPI).

Steps

Prerequisites for development

Install serverless

npm install serverless -g

Install dependencies

docker run --rm -v $(pwd):/app composer/composer install

Run locally

sls invoke local --function nextBus

NOTE: You will need php 7 installed locally. Not ideal, but necessary without avoiding this and using docker for local testing instead.

Setup AWS creds

serverless config credentials --provider aws --key AKIAIOSFODNN7EXAMPLE --secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

See https://git.io/vXsdd

Deploy

sls deploy

Add trigger

Choose alexa skills kit.

Create Alexa skill

Create a new skill, applying the invocation name "bus timetables". Insert ARN of AWS Lambda we just created. Intent not necessary at this stage, as this basic skill only does one thing, and happens on the launch request.

Test!

"Alexa, open bus timetables."

Rebuilding the PHP binary

If you need different compiler flags or dependencies you will need to recompile PHP.

Prerequisites

Compile the static standalone PHP 7 binary

To do this, we have to compile the PHP 7.1.2 with statically linked libraries:

sh buildphp.sh

PHP Version

The default is to use the PHP 7.1.2 branch to compile the PHP binary. To switch the PHP version you can set the branch PHP_VERSION_GIT_BRANCH parameter in buildphp.sh line 8.

FAQs

How do I get the ATCO code for my bus stop(s)?

One of the easiest ways is to find your bus stop on OpenStreetMap. Clicking on the stop should reveal data including the all important naptan:AtcoCode. This is the value you want to use.

From experience (examining bus stops by First Leeds), this tends to just look like the SMS code but with an extra 0. You might be able to guess the ATCO code if data is not readily available on OpenStreetMap.

Alternatively, you can view the full data set from DfT here.

Thanks

Huge thanks to Robert Anderson for piecing together the serverless framework template that this PoC is based on.

Further reading

Serverless PHP

Async PHP

Releases

No releases published

Packages

No packages published