SlideShare uma empresa Scribd logo
1 de 62
Baixar para ler offline
SERVERLESS MESSENGER BOT
WORKSHOP
Mikael Puittinen, Chief Technology Officer
Eetu Tuomala, Cloud Application Architect
1
16.11.2016
SC5 BRIEFLY
CLOUD
SOLUTIONS
BUSINESS
APPLICATIONS
DIGITAL
DESIGN
10
YEARS
60+
CUSTOMERS
200+
PROJECTS
HEL
JKL
85
HACKERS
DESIGNERS
~7
MEUR
2016 (FC)
VISIT OUR WEB SITE FOR MORE INFO: HTTPS://SC5.IO
INTELLIGENT
APPLICATIONS
RECOGNIZED SERVERLESS CONTRIBUTOR
PARTNERSHIPS CONTRIBUTIONS
§ Author of sc5-serverless-boilerplate
§ Author of serverless-authentication-boilerplate
§ Author of serverless-messenger-boilerplate
§ Author of serverless-mocha-plugin
§ Active serverless workshop & event organizer
(e.g. bot workshop at ServerlessConf London)
§ Founder and main organizer of Serverless
Finland (serverless.fi) community (175 members)
AWS Consulting Partner with trained and
certified architects & developers
Official Serverless Framework partner
(1 of 3 worldwide)
INTRODUCTION / PRE-REQUISITES
§ Introduction to AWS & Serverless Framework :
http://serverless.fi/docs/aws-serverless-intro.pdf
§ Technical pre-requisites for workshop:
http://serverless.fi/docs/workshop-preps/
§ This presentation:
http://serverless.fi/docs/messenger-workshop.pdf
INTRODUCTION TO
MESSENGER BOT COMPONENTS
5
MESSENGER BOT ARCHITECTURE
RECEIVE
SEND
CONTEXT
BACKEND(S)
NATURAL LANGUAGE
PROCESSING
CONVERSATION
FLOW
MESSENGER PLATFORM
7
Messenger Platform Beta Launched April 2016
Over 1Bn monthly users
Over 33k bots
RICH UI ELEMENTS: TEXT
RICH UI ELEMENTS: BUTTONS
RICH UI ELEMENTS: CAROUSEL
RICH UI ELEMENTS: WEB VIEWS
EXAMPLE: KLM MESSENGER
MESSENGER BOT SETUP QUICK GUIDE
1. Create a Facebook page
2. Register a Facebook application
3. Create an endpoint for your bot and hook it to the Facebook app
4. Listen to messages from Messenger
5. Post back responses to the Messenger Platform
6. Get your Facebook application approved to reach the public
Messenger Platform API documentation available at:
https://developers.facebook.com/docs/messenger-platform/
WIT.AI
16
CONVERSATION WORKFLOW
(DETERMINISTIC)
”I want a
coffee”
”Small or
Large?”
”Small”
”Large”
Serve small
coffee
Serve large
coffee
”I don’t
know””You must
specify a size
”Actually,
can I
have tea
instead?”
WIT.AI APPROACH
§ Stories to define ”rules”/”heuristics” for the conversation
§ Context to define current state
=> No need for hard coded flows. A combination of stories with rules
based on context allow to define complex converstations
CONTEXTS
”I want a
coffee”
”I want a
large
coffee”
{
”user_id”: 123435,
”name”: ”John Smith”,
”intent”: ”purchase”,
”order”: {
”type”: ”coffee”
”qty”: 1
}
}
{
”user_id”: 123435,
”name”: ”John Smith”,
”intent”: ”purchase”,
”order”: {
”type”: ”coffee”
”size”: ”large”
”qty”: 1
}
}
NATURAL LANGUAGE PROCESSING
”How is the
weather
tomorrow?”
{
”user_id”: 123435,
”name”: ”John Smith”,
”intent”: ”weather”,
”date”: ”2016-10-27T08:11:42.222Z”
}
”What’s the
weather in
London?”
{
”user_id”: 123435,
”name”: ”John Smith”,
”intent: ”weather”,
”location”: ”London"
}
SAMPLE WIT.AI CONVERSATION (1)
REQUEST RESPONSE
{
"type": "merge",
"entities": {
"location": [{
"body": "Brussels",
"value": {
"type": "value",
"value": "Brussels",
"suggested": true},
"start": 11,
"end": 19,
"entity": "location"}
]
},
"confidence": 1
}
What’s the weather in Brussels?
SAMPLE WIT.AI CONVERSATION (2)
REQUEST RESPONSE
{
“type”: “action”,
“action”: “fetch-forecast”
}
{
”loc”: ”Brussels”
}
SAMPLE WIT.AI CONVERSATION (3)
REQUEST RESPONSE
{
“type”: “msg”,
“msg”: “It’s gonna be sunny in Brussels”
}
{
”loc”: ”Brussels”,
”forecast”: ”Sunny”
}
The node-wit module provides higher level method
runActions() that hides the logic of iterating the
contexts with Wit.ai.
Wit.ai HTTP API documentation available at:
https://wit.ai/docs/http
Node.js SDK available at
https://github.com/wit-ai/node-wit
OUR BOILERPLATE
26
serverless-messenger-boilerplate and its documentation
available at
https://github.com/SC5/serverless-messenger-boilerplate
BOILERPLATE ARCHITECTURE
Messenger Bot Service
3rd
Party
Svcs
facebookBot
fbMessengerTopic
witAiTopic
witAiHandlersessions
BOILERPLATE STRUCTURE
example.env Template for .env (Facebook / Wit.ai secrets) <= COPY TO .env
facebook-bot/ facebookBot function
fb-messenger.js Facebook Messenger interop
handler.js Function entrypoint
lib/ Common libraries used by both functions
messageQueue.js Library for reading / publishing to SNS
session.js Library for reading / updating user session (DynamoDB)
package.json ADD NODE MODULES REQUIRED BY YOUR BOT HERE
serverless.yml Service configuration (functions, endpoints, resources)
test/ Serverless-mocha-plugin tests
facebookBot.js Tests for facebookBot function
witAiHandler.js Tests for witAiHandler function
webpack.config Configuration for Webpack deployment (serverless-webpack plugin)
wit-ai/ witAiHandler function
handler.js Entrypoint for function
my-wit-actions.js Your bot logic (Wit.ai actions) <= IMPLEMENT YOUR BOT LOGIC HERE
wit-ai.js Bot logic built interop on Wit.ai
.ENV FILE (FROM EXAMPLE.ENV)
FACEBOOK_BOT_VERIFY_TOKEN User defined verification token for the
Facebook App
FACEBOOK_BOT_PAGE_ACCESS_TOKEN Facebook-generated access token for
the page
WIT_AI_TOKEN API token for Wit.ai
FACEBOOK_ID_FOR_TESTS Facebook ID used to post messages in
tests. Available from the sessions table
SERVERLESS_PROJECT Service name (keep in sync with
service name in serverless.yml)
DECOUPLING WITH SNS
§ Lambda SNS subscriptions: see serverless.yml
§ Posting and decoding messages done with messageQueue.js
§ Sample endpoint at facebookBot/handler.js
§ In production environments, SNS could be strengthened e.g. with
SQS to guarantee delivery
LOCAL DEVELOPMENT AND TESTING
(SERVERLESS-MOCHA-PLUGIN)
§ serverless-mocha-plugin included in boilerplate with predefined tests for
facebookBot and witAiHandler functions in the test/ directory
§ Run all tests : sls invoke test
§ Run tests for facebookBot : sls invoke test –f facebookBot
§ Run tests for witAiHandler: sls invoke test –f witAiHandler
§ (Create new tests with sls create test –f functionName)
§ Comment out line that sets process.env.SILENT in test/*.js if you want
messages to be sent during testing
OPTIMIZED DEPLOYMENT
(SERVERLESS-WEBPACK)
§ serverless-webpack plugin included to streamline deployment
package and accelerate cold start of Lambda functions
§ Pre-configured in webpack.config
BUILDING A WEATHER BOT
34
SETUP PROJECT
35
1. CREATE SERVERLESS PROJECT
> sls install -u https://github.com/SC5/serverless-messenger-boilerplate
> mv serverless-messenger-boilerplate weather-bot
§ Change service name to e.g. “weather-bot” in serverless.yml, then
install node modules and copy example.env to .env
> npm install
> cp example.env .env
§ Generate a random verification token to VERIFY_TOKEN in .env
§ Deploy the service and memorize the URL
> sls deploy
SETUP FACEBOOK PAGE AND
APPLICATION
CREATE A FACEBOOK PAGE
§ Go to
http://facebook.com/pages/create
§ Create new page
§ Skip all following steps
CREATE FACEBOOK APPLICATION
§ Go to https://developers.facebook.com/quickstarts/?platform=web
§ Click ”Skip and Create App ID”
§ Fill in info
§ Create app ID
SETUP MESSENGER TOKEN
§ ”Get Started” for Messenger
§ Generate a token for your page
§ Copy token to .env
(FACEBOOK_BOT_PAGE_ACCES
S_TOKEN)
§ Save changes and deploy your
service
> sls deploy
SETUP MESSENGER WEBHOOK
§ Initiate ”Setup Webhooks”
§ Enter the endpoint URL that
you got during deployment
§ Enter your verify token from
.env
(FACEBOOK_VERIFY_TOKEN)
§ Verify and Save
SUBSCRIBE THE WEBHOOK TO THE FACEBOOK
PAGE
1. Under ”Webhooks”, select your
page and subscribe
TRY IT
§ Send message to your
Facebook page
FACEBOOK APPLICATION ACCESS
Access to unapproved applications is limited to developers only.
If you want to give access to other users, you should provide
”Developer” / ”Testers” roles to them from the ”Roles” panel
(accessible from the left sidebar)
SET UP OPENWEATHER API
45
REGISTER TO OPENWEATHERMAP
1. Go to
https://openweathermap.org/appid
2. Sign up
3. Copy API key and set it to
WEATHER_API_TOKEN in .env
SETUP WIT.AI
REGISTER + CREATE WIT.AI APP
§ Go to https://wit.ai
§ Register (if not already done)
§ Create a new App
CONNECT THE ENDPOINT WITH WIT.AI
§ Go to Settings
§ Copy the Server Access Token
ID to .env (WIT_AI_TOKEN)
§ Deploy your service
> sls deploy
LET THE BOT INTRODUCE ITSELF
§ Create a new story
§ Add user input and intent
§ Add a response with ”Bot
Sends”
§ Remember to save the story
TRY IT!
ADD LOGIC TO THE STORY
§ Add a new story
§ Type in your user input
§ Add entity intent with value weather
§ Select the location and add new entity
wit/location
§ Select the date and add new entity
wit/datetime
§ Add action getWeather with ”Bot Executes”
§ Set context key location && datetime &&
description && temperature
§ Add a response using ”Bot sends”
§ Save
IMPLEMENT LOGIC FOR FETCHING WEATHER
§ Copy snippet ”getWeather.js”
from examples/weather-bot to
my-wit-actions.js
§ Copy file weather.js from
examples/weather-bot to the
wit-ai directory
> sls deploy function -f witAiHandler
TRY IT!
§ An inquiry with date and
location information works!
§ But if we drop out the date, we
do not get the right response.
Let’s fix that!
FORK THE STORY WITHOUT A DATE
§ Click the fork icon next to the
context keys
§ Create new context key
location && description &&
temperature (without datetime)
§ Create new response
§ Save
TRY IT!
§ The current weather is now
given if no date is provided
§ But if we do not provide a
location, the bot should
probably ask for it!
ASK ADDITIONAL QUESTIONS FROM THE USER
§ Click the bookmark icon next to
the getWeather action and create
a bookmark getWeather
§ Create new fork with context key
missingLocation
§ Add a bot response for asking the
location
§ Add a user response and set
intent and wit/location entities
§ Add Jump to getWeather
bookmark created earlier
TRY IT!
IT WORKS!
TRAINING THE BOT
WIT.AI UNDERSTANDING
§ Use Wit.ai understanding to
train model
§ Enter expressions, set entities
and validate to improve
accuracy
WIT.AI INBOX
§ Use Wit.ai inbox to train model
based on incoming messages
§ Validate expressions that have
been entered by users
§ Preview API output for
expressions
THANK YOU!
mikael.puittinen@sc5.io / @mpuittinen
eetu.tuomala@sc5.io / @hopeatussi

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

ESPC15 - Introduction to AngularJS in an Office 365 context
ESPC15 - Introduction to AngularJS in an Office 365 contextESPC15 - Introduction to AngularJS in an Office 365 context
ESPC15 - Introduction to AngularJS in an Office 365 context
 
SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...
SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...
SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...
 
O365Con18 - PowerApps build custom forms for SharePoint with Azure Maps - Bra...
O365Con18 - PowerApps build custom forms for SharePoint with Azure Maps - Bra...O365Con18 - PowerApps build custom forms for SharePoint with Azure Maps - Bra...
O365Con18 - PowerApps build custom forms for SharePoint with Azure Maps - Bra...
 
Chris Anderson and Yochay Kiriaty - Serverless Patterns with Azure Functions
Chris Anderson and Yochay Kiriaty - Serverless Patterns with Azure FunctionsChris Anderson and Yochay Kiriaty - Serverless Patterns with Azure Functions
Chris Anderson and Yochay Kiriaty - Serverless Patterns with Azure Functions
 
Azure App Service Architecture. Web Apps.
Azure App Service Architecture. Web Apps.Azure App Service Architecture. Web Apps.
Azure App Service Architecture. Web Apps.
 
Continuous delivery with azure app service
Continuous delivery with azure app serviceContinuous delivery with azure app service
Continuous delivery with azure app service
 
Office Web Apps Server 2013
Office Web Apps Server 2013Office Web Apps Server 2013
Office Web Apps Server 2013
 
Azure app services API apps
Azure app services API appsAzure app services API apps
Azure app services API apps
 
O365 saturday: How to (remote) control office 365 with windows azure-slideshare
O365 saturday: How to (remote) control office 365 with windows azure-slideshareO365 saturday: How to (remote) control office 365 with windows azure-slideshare
O365 saturday: How to (remote) control office 365 with windows azure-slideshare
 
Azure App Service at Let's Dev This
Azure App Service at Let's Dev ThisAzure App Service at Let's Dev This
Azure App Service at Let's Dev This
 
Azure App Service
Azure App ServiceAzure App Service
Azure App Service
 
ECS19 - Damir Dobric - Designing and Operating modern applications with Micro...
ECS19 - Damir Dobric - Designing and Operating modern applications with Micro...ECS19 - Damir Dobric - Designing and Operating modern applications with Micro...
ECS19 - Damir Dobric - Designing and Operating modern applications with Micro...
 
SharePoint Office Web Apps 2013 presentation
SharePoint Office Web Apps 2013 presentationSharePoint Office Web Apps 2013 presentation
SharePoint Office Web Apps 2013 presentation
 
[Roine] Serverless: Don't Take It Literally
[Roine] Serverless: Don't Take It Literally[Roine] Serverless: Don't Take It Literally
[Roine] Serverless: Don't Take It Literally
 
Connector API Apps
Connector API AppsConnector API Apps
Connector API Apps
 
Extending Piwik At R7.com
Extending Piwik At R7.comExtending Piwik At R7.com
Extending Piwik At R7.com
 
Serverless Architecture
Serverless ArchitectureServerless Architecture
Serverless Architecture
 
Brewing Beer with Windows Azure - NDC2013
Brewing Beer with Windows Azure - NDC2013Brewing Beer with Windows Azure - NDC2013
Brewing Beer with Windows Azure - NDC2013
 
2013 - Back to the Future with Client/Server Development
2013 - Back to the Future with Client/Server Development 2013 - Back to the Future with Client/Server Development
2013 - Back to the Future with Client/Server Development
 
Workflow Automation with Logic Apps
Workflow Automation with Logic AppsWorkflow Automation with Logic Apps
Workflow Automation with Logic Apps
 

Semelhante a Serverless Finland Meetup 16.11.2016: Messenger Bot Workshop

Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
KMS Technology
 
Best Practices with WSO2 Developer Studio
Best Practices with WSO2 Developer Studio Best Practices with WSO2 Developer Studio
Best Practices with WSO2 Developer Studio
WSO2
 
Firefox os-introduction
Firefox os-introductionFirefox os-introduction
Firefox os-introduction
zsoltlengyelit
 

Semelhante a Serverless Finland Meetup 16.11.2016: Messenger Bot Workshop (20)

Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 appsChris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
 
European SharePoint Conference 2018 - Build an intelligent application by con...
European SharePoint Conference 2018 - Build an intelligent application by con...European SharePoint Conference 2018 - Build an intelligent application by con...
European SharePoint Conference 2018 - Build an intelligent application by con...
 
Coding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using SparkCoding 102 REST API Basics Using Spark
Coding 102 REST API Basics Using Spark
 
Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
 
AI: Mobile Apps That Understands Your Intention When You Typed
AI: Mobile Apps That Understands Your Intention When You TypedAI: Mobile Apps That Understands Your Intention When You Typed
AI: Mobile Apps That Understands Your Intention When You Typed
 
Finland Azure User Group #8 DevOps Mobile Client Releases
Finland Azure User Group #8 DevOps Mobile Client Releases Finland Azure User Group #8 DevOps Mobile Client Releases
Finland Azure User Group #8 DevOps Mobile Client Releases
 
Mobile for SharePoint with Windows Phone
Mobile for SharePoint with Windows PhoneMobile for SharePoint with Windows Phone
Mobile for SharePoint with Windows Phone
 
Cisco Spark & Tropo API Workshop
Cisco Spark & Tropo API WorkshopCisco Spark & Tropo API Workshop
Cisco Spark & Tropo API Workshop
 
Day One Keynote
Day One KeynoteDay One Keynote
Day One Keynote
 
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
 
2014 SharePoint Saturday Melbourne Apps or not to Apps
2014 SharePoint Saturday Melbourne Apps or not to Apps2014 SharePoint Saturday Melbourne Apps or not to Apps
2014 SharePoint Saturday Melbourne Apps or not to Apps
 
GAB2017 - Azure function to build serverless SharePoint apps
GAB2017 - Azure function to build serverless SharePoint appsGAB2017 - Azure function to build serverless SharePoint apps
GAB2017 - Azure function to build serverless SharePoint apps
 
Webex APIs for Administrators - CL20B - DEVNET-2610
Webex APIs for Administrators - CL20B - DEVNET-2610Webex APIs for Administrators - CL20B - DEVNET-2610
Webex APIs for Administrators - CL20B - DEVNET-2610
 
Best Practices with WSO2 Developer Studio
Best Practices with WSO2 Developer Studio Best Practices with WSO2 Developer Studio
Best Practices with WSO2 Developer Studio
 
Firefox os-introduction
Firefox os-introductionFirefox os-introduction
Firefox os-introduction
 
SharePoint 2013 App Provisioning Models
SharePoint 2013 App Provisioning ModelsSharePoint 2013 App Provisioning Models
SharePoint 2013 App Provisioning Models
 
Day One Keynote
Day One KeynoteDay One Keynote
Day One Keynote
 
Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008Silverlight 2 for Developers - TechEd New Zealand 2008
Silverlight 2 for Developers - TechEd New Zealand 2008
 
Windows Azure: Connecting the Dots for a Mobile Workforce
Windows Azure: Connecting the Dots for a Mobile WorkforceWindows Azure: Connecting the Dots for a Mobile Workforce
Windows Azure: Connecting the Dots for a Mobile Workforce
 
Con3036 soaring-through-the-clouds-oow2016-160920214845
Con3036 soaring-through-the-clouds-oow2016-160920214845Con3036 soaring-through-the-clouds-oow2016-160920214845
Con3036 soaring-through-the-clouds-oow2016-160920214845
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Último (20)

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Serverless Finland Meetup 16.11.2016: Messenger Bot Workshop

  • 1. SERVERLESS MESSENGER BOT WORKSHOP Mikael Puittinen, Chief Technology Officer Eetu Tuomala, Cloud Application Architect 1 16.11.2016
  • 3. RECOGNIZED SERVERLESS CONTRIBUTOR PARTNERSHIPS CONTRIBUTIONS § Author of sc5-serverless-boilerplate § Author of serverless-authentication-boilerplate § Author of serverless-messenger-boilerplate § Author of serverless-mocha-plugin § Active serverless workshop & event organizer (e.g. bot workshop at ServerlessConf London) § Founder and main organizer of Serverless Finland (serverless.fi) community (175 members) AWS Consulting Partner with trained and certified architects & developers Official Serverless Framework partner (1 of 3 worldwide)
  • 4. INTRODUCTION / PRE-REQUISITES § Introduction to AWS & Serverless Framework : http://serverless.fi/docs/aws-serverless-intro.pdf § Technical pre-requisites for workshop: http://serverless.fi/docs/workshop-preps/ § This presentation: http://serverless.fi/docs/messenger-workshop.pdf
  • 8. Messenger Platform Beta Launched April 2016 Over 1Bn monthly users Over 33k bots
  • 10. RICH UI ELEMENTS: BUTTONS
  • 11. RICH UI ELEMENTS: CAROUSEL
  • 12. RICH UI ELEMENTS: WEB VIEWS
  • 14. MESSENGER BOT SETUP QUICK GUIDE 1. Create a Facebook page 2. Register a Facebook application 3. Create an endpoint for your bot and hook it to the Facebook app 4. Listen to messages from Messenger 5. Post back responses to the Messenger Platform 6. Get your Facebook application approved to reach the public
  • 15. Messenger Platform API documentation available at: https://developers.facebook.com/docs/messenger-platform/
  • 17. CONVERSATION WORKFLOW (DETERMINISTIC) ”I want a coffee” ”Small or Large?” ”Small” ”Large” Serve small coffee Serve large coffee ”I don’t know””You must specify a size ”Actually, can I have tea instead?”
  • 18. WIT.AI APPROACH § Stories to define ”rules”/”heuristics” for the conversation § Context to define current state => No need for hard coded flows. A combination of stories with rules based on context allow to define complex converstations
  • 19. CONTEXTS ”I want a coffee” ”I want a large coffee” { ”user_id”: 123435, ”name”: ”John Smith”, ”intent”: ”purchase”, ”order”: { ”type”: ”coffee” ”qty”: 1 } } { ”user_id”: 123435, ”name”: ”John Smith”, ”intent”: ”purchase”, ”order”: { ”type”: ”coffee” ”size”: ”large” ”qty”: 1 } }
  • 20. NATURAL LANGUAGE PROCESSING ”How is the weather tomorrow?” { ”user_id”: 123435, ”name”: ”John Smith”, ”intent”: ”weather”, ”date”: ”2016-10-27T08:11:42.222Z” } ”What’s the weather in London?” { ”user_id”: 123435, ”name”: ”John Smith”, ”intent: ”weather”, ”location”: ”London" }
  • 21. SAMPLE WIT.AI CONVERSATION (1) REQUEST RESPONSE { "type": "merge", "entities": { "location": [{ "body": "Brussels", "value": { "type": "value", "value": "Brussels", "suggested": true}, "start": 11, "end": 19, "entity": "location"} ] }, "confidence": 1 } What’s the weather in Brussels?
  • 22. SAMPLE WIT.AI CONVERSATION (2) REQUEST RESPONSE { “type”: “action”, “action”: “fetch-forecast” } { ”loc”: ”Brussels” }
  • 23. SAMPLE WIT.AI CONVERSATION (3) REQUEST RESPONSE { “type”: “msg”, “msg”: “It’s gonna be sunny in Brussels” } { ”loc”: ”Brussels”, ”forecast”: ”Sunny” }
  • 24. The node-wit module provides higher level method runActions() that hides the logic of iterating the contexts with Wit.ai.
  • 25. Wit.ai HTTP API documentation available at: https://wit.ai/docs/http Node.js SDK available at https://github.com/wit-ai/node-wit
  • 27. serverless-messenger-boilerplate and its documentation available at https://github.com/SC5/serverless-messenger-boilerplate
  • 28. BOILERPLATE ARCHITECTURE Messenger Bot Service 3rd Party Svcs facebookBot fbMessengerTopic witAiTopic witAiHandlersessions
  • 29. BOILERPLATE STRUCTURE example.env Template for .env (Facebook / Wit.ai secrets) <= COPY TO .env facebook-bot/ facebookBot function fb-messenger.js Facebook Messenger interop handler.js Function entrypoint lib/ Common libraries used by both functions messageQueue.js Library for reading / publishing to SNS session.js Library for reading / updating user session (DynamoDB) package.json ADD NODE MODULES REQUIRED BY YOUR BOT HERE serverless.yml Service configuration (functions, endpoints, resources) test/ Serverless-mocha-plugin tests facebookBot.js Tests for facebookBot function witAiHandler.js Tests for witAiHandler function webpack.config Configuration for Webpack deployment (serverless-webpack plugin) wit-ai/ witAiHandler function handler.js Entrypoint for function my-wit-actions.js Your bot logic (Wit.ai actions) <= IMPLEMENT YOUR BOT LOGIC HERE wit-ai.js Bot logic built interop on Wit.ai
  • 30. .ENV FILE (FROM EXAMPLE.ENV) FACEBOOK_BOT_VERIFY_TOKEN User defined verification token for the Facebook App FACEBOOK_BOT_PAGE_ACCESS_TOKEN Facebook-generated access token for the page WIT_AI_TOKEN API token for Wit.ai FACEBOOK_ID_FOR_TESTS Facebook ID used to post messages in tests. Available from the sessions table SERVERLESS_PROJECT Service name (keep in sync with service name in serverless.yml)
  • 31. DECOUPLING WITH SNS § Lambda SNS subscriptions: see serverless.yml § Posting and decoding messages done with messageQueue.js § Sample endpoint at facebookBot/handler.js § In production environments, SNS could be strengthened e.g. with SQS to guarantee delivery
  • 32. LOCAL DEVELOPMENT AND TESTING (SERVERLESS-MOCHA-PLUGIN) § serverless-mocha-plugin included in boilerplate with predefined tests for facebookBot and witAiHandler functions in the test/ directory § Run all tests : sls invoke test § Run tests for facebookBot : sls invoke test –f facebookBot § Run tests for witAiHandler: sls invoke test –f witAiHandler § (Create new tests with sls create test –f functionName) § Comment out line that sets process.env.SILENT in test/*.js if you want messages to be sent during testing
  • 33. OPTIMIZED DEPLOYMENT (SERVERLESS-WEBPACK) § serverless-webpack plugin included to streamline deployment package and accelerate cold start of Lambda functions § Pre-configured in webpack.config
  • 36. 1. CREATE SERVERLESS PROJECT > sls install -u https://github.com/SC5/serverless-messenger-boilerplate > mv serverless-messenger-boilerplate weather-bot § Change service name to e.g. “weather-bot” in serverless.yml, then install node modules and copy example.env to .env > npm install > cp example.env .env § Generate a random verification token to VERIFY_TOKEN in .env § Deploy the service and memorize the URL > sls deploy
  • 37. SETUP FACEBOOK PAGE AND APPLICATION
  • 38. CREATE A FACEBOOK PAGE § Go to http://facebook.com/pages/create § Create new page § Skip all following steps
  • 39. CREATE FACEBOOK APPLICATION § Go to https://developers.facebook.com/quickstarts/?platform=web § Click ”Skip and Create App ID” § Fill in info § Create app ID
  • 40. SETUP MESSENGER TOKEN § ”Get Started” for Messenger § Generate a token for your page § Copy token to .env (FACEBOOK_BOT_PAGE_ACCES S_TOKEN) § Save changes and deploy your service > sls deploy
  • 41. SETUP MESSENGER WEBHOOK § Initiate ”Setup Webhooks” § Enter the endpoint URL that you got during deployment § Enter your verify token from .env (FACEBOOK_VERIFY_TOKEN) § Verify and Save
  • 42. SUBSCRIBE THE WEBHOOK TO THE FACEBOOK PAGE 1. Under ”Webhooks”, select your page and subscribe
  • 43. TRY IT § Send message to your Facebook page
  • 44. FACEBOOK APPLICATION ACCESS Access to unapproved applications is limited to developers only. If you want to give access to other users, you should provide ”Developer” / ”Testers” roles to them from the ”Roles” panel (accessible from the left sidebar)
  • 46. REGISTER TO OPENWEATHERMAP 1. Go to https://openweathermap.org/appid 2. Sign up 3. Copy API key and set it to WEATHER_API_TOKEN in .env
  • 48. REGISTER + CREATE WIT.AI APP § Go to https://wit.ai § Register (if not already done) § Create a new App
  • 49. CONNECT THE ENDPOINT WITH WIT.AI § Go to Settings § Copy the Server Access Token ID to .env (WIT_AI_TOKEN) § Deploy your service > sls deploy
  • 50. LET THE BOT INTRODUCE ITSELF § Create a new story § Add user input and intent § Add a response with ”Bot Sends” § Remember to save the story
  • 52. ADD LOGIC TO THE STORY § Add a new story § Type in your user input § Add entity intent with value weather § Select the location and add new entity wit/location § Select the date and add new entity wit/datetime § Add action getWeather with ”Bot Executes” § Set context key location && datetime && description && temperature § Add a response using ”Bot sends” § Save
  • 53. IMPLEMENT LOGIC FOR FETCHING WEATHER § Copy snippet ”getWeather.js” from examples/weather-bot to my-wit-actions.js § Copy file weather.js from examples/weather-bot to the wit-ai directory > sls deploy function -f witAiHandler
  • 54. TRY IT! § An inquiry with date and location information works! § But if we drop out the date, we do not get the right response. Let’s fix that!
  • 55. FORK THE STORY WITHOUT A DATE § Click the fork icon next to the context keys § Create new context key location && description && temperature (without datetime) § Create new response § Save
  • 56. TRY IT! § The current weather is now given if no date is provided § But if we do not provide a location, the bot should probably ask for it!
  • 57. ASK ADDITIONAL QUESTIONS FROM THE USER § Click the bookmark icon next to the getWeather action and create a bookmark getWeather § Create new fork with context key missingLocation § Add a bot response for asking the location § Add a user response and set intent and wit/location entities § Add Jump to getWeather bookmark created earlier
  • 60. WIT.AI UNDERSTANDING § Use Wit.ai understanding to train model § Enter expressions, set entities and validate to improve accuracy
  • 61. WIT.AI INBOX § Use Wit.ai inbox to train model based on incoming messages § Validate expressions that have been entered by users § Preview API output for expressions
  • 62. THANK YOU! mikael.puittinen@sc5.io / @mpuittinen eetu.tuomala@sc5.io / @hopeatussi