WESLEY CHUN: Hi.
This is Wesley Chun from Google.
You may know me as a longtime member of the Python community,
but in this episode, I'll try to actually use
some of those skills to show you how you can change
the world by building a globally accessible app starting
with just 10 lines of code.
Yes, I'm giving you an idea for your next startup.
Assuming you want a wildly successful business,
time to market is of the essence,
so you need to move quickly and use
tools that get your service out there sooner.
One idea is to come up with an MVP, or Minimally Viable
Product-- quickly to see whether we can build
a service without a web or mobile app, both of which
can come later.
Instead, we can simply create a bare bones service
that users can reach.
How are we going to do that?
Well, what's one thing that everyone online has access to?
That's right-- email.
We've all got an email from a person or a computer,
and you've probably sent email to a person.
But have you ever sent email to a computer?
Well, you're going to learn it now,
because that's what's allowing you
to skip native implementation for V1, because email
is compatible across all devices, mobile or desktop.
This also means that we're looking
at building an app that receives email.
Now why is an app that receives email a big deal?
Well, many services send email, but that's less interesting,
because it's just a single function call.
Now being able to receive and process those messages, that's
more intriguing, and as you can tell with the big bang here,
I think much more powerful.
Your users email your service, and your app
responds to those requests.
Think about it.
You typically email a person, but can you just as easily
email an application?
Of course.
On top of that, users don't really
need to download another app since email is already
likely available on their devices.
By allowing users to send commands to your service,
you've opened up the door to many possibilities,
such as updating user settings, transferring money
to a relative, reordering all that prescription medicine,
making a small purchase, or turning on or off the TV,
heater, or alarm at home or a business,
all from an email message that's coded correctly and sent
to your service.
Imagine emailing your mom and then emailing your house,
say to shut off the lights, because you left so early
for the airport this morning.
Doesn't that sound crazy?
Well, automated processing is also more scalable,
enabling the ability to process hundreds or thousands of email
messages and performing service requests
in a very short period of time.
Now, it's very much of a client server model
if you're familiar with that.
So anyway, how can we possibly do this in 10 lines of code?
By leveraging Google App Engine, so you
don't have to worry about things like the hardware,
the operating system, network programming,
databases, and most importantly, email servers.
Similarly, Python lets you get a lot
done with very few lines of code.
The syntax is pretty easy, so even if you don't know Python,
you can still follow along in this tutorial and mentally map
to your favorite programming language.
However, while this video is beginner-friendly,
we're not going to introduce you to developing on App Engine,
so if you are new, just do the Getting Started tutorial
that's online, and you'll be set.
When you're ready, create a new Hello World app either
using the Launcher UI that comes with App Engine when
you download it or copy the code from the tutorial,
because we're going to start from there.
We only need to edit the app.yaml config
file and the main main.py Python controller file.
Believe it or not, building this app only requires a few steps.
One, create the skeleton app.
Done.
Step two, enable receiving email.
It's just a config change.
Three, bring in the library code that supports email handling.
Four, add the code that uses that library, and five,
route all inbound email messages to the handle
that you wrote in step four.
Step two is just a config change, right?
So add these two red lines of code
that you see here to your app.yaml config file.
It's a new section called Inbound Services,
and it goes right above the Handler section.
App Engine supports several inbound services,
but the one we care about is email.
So that's what we do.
And that's it.
Your app can now receive email.
All the other code changes will take place
in the controller, which is main.py, so let's go there.
Now let's bring in the App Engine component
that handles receiving email.
Oh, by the way, we took out the comments
that were auto-generated at the top,
so that it's easier to see the code.
So now add these two red lines of code.
Line 1 is import logging.
What we're doing in the prototype
is to log the senders of the email messages
that our app receives.
Obviously, you can do more, but we're just getting started.
Line 3 imports the App Engine code
that handles the receipt of email,
as you can probably guess.
Well, why are the import lines in this order?
Well, it's a Python best practice.
System imports like logging come first, followed by third party
library codes, such as webapp2.
Next are any local SDK Tools, such as the App Engine APIs,
and finally, if we had any local modules, those would go last.
Now let's handle incoming email.
This is done by adding this three-line handler class.
The receive method pulls out the sender's address and logs it.
Now there are other email message
attributes that you can access, because to change the world,
you're going to have to use everything
else like the email subject and body
and the sender and receiver email addresses.
All right, the final step-- redirect all the inbound email
requests to the handler that we just added.
When someone points their browser at your app,
and sees Hello World, how do you think that actually happens?
Line 14, it forwards the request for slash,
which is your homepage, to the main handler
on lines 5 through 7.
Think of the app variable on lines 13 through 16
as the dispatch table of where we forward requests.
Each route comes in the form of a path and handler 2-tuple
so we need to add something similar for inbound email.
When App Engine handles inbound email,
messages will be posted to your app at that special path,
which is /_ah/mail/emailaddress.
Since we're going to handle all emails with the same handler,
we'll use a regular expression-- or regex--
matching all such addresses, hence why we have dot plus.
That redirects to email handler.
Oh, by the way, don't forget that trailing comma
at the end of line 14, which is why it's kind of red too.
Otherwise, you'll get a runtime error.
Hey, I just helped you pass an interview question,
so feel good about it.
And that's it.
Ignoring the imports and blank lines,
we're left with about 10 lines of code that do all the work.
Now if you're really pedantic and recognize
that the routing table is actually just one single Python
statement, well, you can cut that down to seven,
and if you're only counting the number of lines that we added,
well, it's down to six.
Amazing!
Believe it or not, this is a fully working app
that does the basics.
We don't have a web app, unless you count the Hello World bit,
and neither did we create an Android or iOS mobile client,
right?
But you could.
And easily, by using Google Cloud Endpoints.
You can click on the video there and check it out.
All right.
Now let's run it.
The App Engine SDK comes with a development server, or dev
server, which lets you run your app
locally to test with before you deploy live to Google.
When you do this by either clicking run on the launcher UI
or using the command line tool, make sure, by the way,
that you note the port numbers for both the main app as well
as the admin console, because we're going to use both.
Point your browser at the main app at local host 8080.
You should see the main handler in action rendering Hello
World.
Since we're only in dev, you can only simulate sending email.
And to do that, you need to get to the dev server admin
console.
So now point your browser at the other port, which is local host
8000.
Once you're there, what we really want
is the inbound mail link in the left nav, so click that.
You'll see this form to send a fake email with.
Before we do that, let's talk about what we're sending first.
Recognize you're going to have to validate every message,
because you're going to need to make sure that every request is
real, not spam, not fake, and most importantly,
not sent by someone on behalf of someone else.
Invalid messages coming from unrecognized users that
don't follow your security protocol
like a secret passphrase or two factor auth
or just aren't structured correctly
can simply be dropped or logged as desired.
As you can see in the message body here,
we've got a lot of stuff that can be potentially used
for this purpose if we were creating
more than just a prototype.
All right.
Enough business logic.
Let's talk about sending email.
So App Engine apps receive email sent to any address
of the format xxx@APP_ID.appspotmail.com,
where xxx is a recipient, and your app ID is the host.
Be sure to use appspotmail in the domain name
rather than appspot.com.
In this demo, let's pretend I created this awesome service
for our press team, allowing them to issue new press
releases with a single command.
We're using the Publish command for the recipient.
Receiving email is our fake app ID.
By the way, that's really someone else's app,
so don't send anything there if you're
going to run in production.
It doesn't really matter in the dev server
as no emails are actually sent.
Once you click Send and see Request Succeeded message,
check your logs and verify that your handler was called.
Click the Logs button in the launcher
or check out the command line output sent to the console.
You should see Received a Message
From whatever fake sender address you used.
Running this live in production requires only a few more steps.
Make sure you've got that unique ID plugged
into your app.yaml file.
Then go to the launcher, and click
Deploy to send your code to Google after authenticating.
Or you can also use the command line tool.
Once the app's uploaded, check that it's
up by going to the Hello World page in the browser
just like we did for the dev server,
only you'll be pointing at yourappID.appspot.com.
Now pull up your favorite, warm, cozy-up-by-the-fireplace email
client to send a real email message to your app.
Again, it should go to some xxx@yourappid.appspotmail.com.
After sending, go to your Apps Admin Console
at cloud.google.com/console, selecting your app,
and clicking App Engine in the left nav bar.
In the App Engine sub nav bar, click on logs.
In the middle of the admin screen,
you should see exactly what you wanted-- your email handler's
log message.
Booyah!
What do we do next?
Well, if you want to skip the typing
and get straight to the source, click the link
to download the zip file.
If for some reason, you want a slower step-by-step,
a full treatment can be found in the Google App Engine
chapter of Core Python Applications Programming.
Otherwise, that's it.
We've just built a Python App Engine app together.
It can receive email.
It's less than or equal to 10 lines of code.
Now it's up to you to change the world.
Seriously, you've got the makings of a new business here.
Go out, and be successful on the Google Cloud Platform.
Thanks for tuning in.