elixir and lambda talk with a telegram bot - paolo montrasio - codemotion milan 2016

Download Elixir and Lambda talk with a Telegram bot - Paolo Montrasio - Codemotion Milan 2016

If you can't read please download the document

Upload: codemotion

Post on 12-Apr-2017

288 views

Category:

Technology


3 download

TRANSCRIPT

Click to edit the title text format

Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline Level

Elixir and Lambda talk with a Telegram botPaolo Montrasio

MILAN 25-26 NOVEMBER 2016

https://creativecommons.org/licenses/by-nc/3.0/

Freelancer

[email protected] github.com/pmontrasio

https://creativecommons.org/licenses/by-nc/3.0/

FreelancerSoftware architect

[email protected] github.com/pmontrasio

software design, APIs
team management
secure software development

FreelancerSoftware architectBackend developer

Rails Django Web2py Node

[email protected] github.com/pmontrasio

software design, APIs
team management
secure software development

FreelancerSoftware architectBackend developerMilano Chatbots

Rails Django Web2py Node

meetup.com/Milano-Chatbots-Meetup

[email protected] github.com/pmontrasio

software design, APIs
team management
secure software development

Telegram@PresenceAlertBot

It tells you how many WiFi mac address are in the air

Hellofor the menu/startto start/stopto end

webhook

receiver

When you talked to the bot Telegrams backend sent a HTTPS request to a webhook with JSON data. The webhook is a JavaScript function on AWS Lambda.

Users

webhook

It stores your user ID in a Dynamo DB table, Users.

Users

webhook

receiver

WiFi sensor

The Raspberry sends the count of unique WiFi mac addresses to another JavaScript function on AWS Lambda, once per minute.

Users

Presences

webhook

receiver

WiFi sensor

The count is timestamped and stored in another DynamoDB table, Presences.

Users

webhook

receiver

trigger

WiFi sensor

Presences

A trigger on Dynamo DB calls a third AWS Lambda function every time some data is added to Presences. That function gets all the Telegram user ids from Users and sends them a message with the count.

elixir

WiFi Sensor

The WiFi sensor on the Raspberry is implemented in Elixir.

elixir

Three layers

1. Functional

2. Object oriented

3. Process supervision

Elixir is a concurrent functional language running on the BEAM virtual machine, derived from Erlang with a Ruby like syntax.

Elixir processes exchange data by sending messages to each other, much like OO method calls. A typical Elixir application has many processes because they are cheap to create, run and send messages to. Millions of processes are not unheard of, much like millions of objects in OO languages.

But processes are concurrent and use all the cores of the machine.

Finally there is a process supervision layer to restart processes if the fail, much like upstart of systemd.

elixir

A look at the code

github.compmontrasio/wifi-sensor

elixir

elixir

The entry pointThe processesDependencies, mix deps.getCalling Erlang modulesPattern matchingHow to store state in a serverThe |> operator

The entry point is mix.exs, which by default runs the run function in lib/sensor.ex

Task.start_link creates a supervised process

ChannelHopper.hop: tail call optimization

MacAddressCounter: how to build a server

spawn fn - > creates a process. Think: new Class

:module.function is a call to an Erlang module

elixir

A more complex projectwith object orientation and process supervision

github.compmontrasio/decibel

Slides at
connettiva.eu/elixir-phoenix-raspberry-wifi (Italian)

Also check Elixir's object orientation at github.com/pmontrasio/elixir-oo

Receiver

github.compmontrasio/wifi-receiver

Everybody knows AWS Lambda?

Show the function at https://github.com/pmontrasio/wifi-receiver/blob/master/handler.js

new AWS.DynamoDB.DocumentClient() outside the function, to save time (container reuse)

Easy but we must automate the deployment and the creation of the https endpoint.

serverless.ymlserverless deployInstall Dynamo DB locallyWhere to init the clientHow to test

$ serverless create --template aws-nodejs --name receiver

serverless.yml defines how to create the environment on AWS Lambda and how to deploy the application.

docs/Serverless_Setup.md is step by step guide at how to create and deploy the receiver.

docs/DynamoDB.md to install Dynamo DB locally

test/README.md on how to test. The important point is to be able to run the assertions in the callback run at the end of the function.

Webhook and Trigger

github.compmontrasio/wifi-presence-bot

Register the webhookReceive messagesConfigure the triggerSend messages

The webhook must be registered on Telegram.

presenceAlertWebhook gets the message in event.body.message

presenceNotifier can be called once with many database Records, in the event.Records array.

The trigger must be configured manually in the AWS console. See README.md for the details

Messages are sent with a POST to Telegrams.

Dont use npm modules unless you really have to: every ms is important. Billed by 100 ms units.

News

AWS Lambda environment variables and serverless architecture

serverless 1.1$ serverless invoke local -f FUNCTION

Alternatives

Google Cloud Functions, Azure functions

Apex, Gordon

https://aws.amazon.com/blogs/aws/new-for-aws-lambda-environment-variables-and-serverless-application-model/

https://cloud.google.com/functions/ https://azure.microsoft.com/en-us/services/functions/ http://apex.run/ https://github.com/jorgebastida/gordon

[email protected]