This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andy B
committed
Dec 14, 2017
1 parent
6e346d6
commit 81847b2
Showing
16 changed files
with
305 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'rubotnik' | ||
# require_relative all files in "bot" folder or do it by hand | ||
Rubotnik::Autoloader.load('bot') | ||
|
||
# Subscribe your bot to a Facebook Page | ||
# (put access token and verify token in .env) | ||
Rubotnik.subscribe(ENV['ACCESS_TOKEN']) | ||
|
||
# Set welcome screen, "get started" button and a menu (all optional) | ||
# Edit profile.rb before uncommenting next line | ||
# Rubotnik.set_profile(Profile::START_BUTTON, Profile::START_GREETING, Profile::SIMPLE_MENU) | ||
|
||
# Read the doc to find out more about built-in UI classes | ||
LOCATION_PROMPT = UI::QuickReplies.location | ||
|
||
####################### HANDLE INCOMING MESSAGES ############################## | ||
|
||
Rubotnik.route :message do | ||
# Will work for all variations of these three greetings | ||
bind 'hi', 'hello', 'bonjour' do | ||
say 'Hello from your new bot!' | ||
end | ||
|
||
# Start a thread (and provide an opening message with optional quick replies). | ||
# You have to define start_conversation methods inside Command module | ||
# and treat user's response to your "reply_with" message there. | ||
bind 'how', 'do', 'you', all: true, to: :start_conversation, reply_with: { | ||
text: "I'm doing fine! You?", | ||
quick_replies: [['Good!', 'OK'], ['Not so well', 'NOT_OK']] | ||
# second item in nested array will be the contents of message.quick_reply, | ||
# once the user makes a selection. It will be quick reply text in ALL CAPS | ||
# if you pass strings instead of arrays | ||
# (e.g. quick_replies: ['Yes', 'No'] - payloads "YES" and "NO" are inferred) | ||
} | ||
|
||
# Use 'all' flag if you want to trigger a command only if all patterns | ||
# are present in a message (will trigger with each of them by default) | ||
bind 'what', 'my', 'name', all: true do | ||
info = get_user_info(:first_name) # helper to get fields from Graph API | ||
say info[:first_name] | ||
end | ||
|
||
# look for example of an API call with HTTParty in commands/location.rb | ||
bind 'where', 'am', 'I', all: true, to: :lookup_location, reply_with: { | ||
text: 'Let me know your location', | ||
quick_replies: LOCATION_PROMPT | ||
} | ||
|
||
# look for more UI examples in commands/ui_examples.rb | ||
bind 'image', to: :show_image | ||
|
||
# Invoked if none of the commands recognized. Has to come last, after all binds | ||
default do | ||
say "Sorry I did not get it" | ||
end | ||
end | ||
|
||
####################### HANDLE INCOMING POSTBACKS ############################## | ||
|
||
Rubotnik.route :postback do | ||
# postback from "Get Started" button | ||
bind 'START' do | ||
say "Welcome!" | ||
end | ||
end | ||
|
||
####################### HANDLE OTHER REQUESTS (NON-FB) ######################### | ||
|
||
get '/' do | ||
'I can have a landing page too!' | ||
end | ||
|
||
############################ TEST ON LOCALHOST ################################# | ||
|
||
# 1. Have both Heroku CLI and ngrok | ||
# 2. Run 'heroku local' from console, it will load Puma on port 5000 | ||
# 3. Expose port 5000 to the Internet with 'ngrok http 5000' | ||
# 4. Provide your ngrok http_s_(!) address in Facebook Developer Dashboard | ||
# for webhook validation. | ||
# 5. Open Messenger and talk to your bot! | ||
|
||
# P.S. While you have DEBUG environment variable set to "true" (default in .env) | ||
# All StandardError exceptions will go to the message dialog instead of | ||
# breaking the server. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,38 @@ | ||
module Commands | ||
# You will write all your commands as methods here | ||
# You can write all your commands as methods here | ||
|
||
# If the command is bound with reply_with specified, | ||
# you have to deal with incoming message and react on it. | ||
def ask | ||
@message.typing_on | ||
if @message.quick_reply == 'YES' | ||
say "Thank you, I'm touched!" | ||
# you have to deal with user response to the last message and react on it. | ||
def start_conversation | ||
# Quick replies are accessible through message object's quick_reply property, | ||
# by default it's the quick reply text in ALL CAPS | ||
# you can also react on the text itself | ||
message.typing_on | ||
case message.quick_reply | ||
when 'OK' | ||
say "Glad you're doing well!" | ||
stop_thread | ||
when 'NOT_OK' | ||
say "Too bad. What happened?" | ||
next_command :appear_nice | ||
else | ||
say "Guess you can't please everyone, huh?" | ||
say "🤖" | ||
# it's always a good idea to have an else, quick replies don't | ||
# prevent user from typing any message in the dialogue | ||
stop_thread | ||
end | ||
@message.typing_off | ||
stop_thread # you have to stop thread or pass control further | ||
message.typing_off | ||
end | ||
|
||
def appear_nice | ||
message.typing_on | ||
case message.text | ||
when /job/i then say "We've all been there" | ||
when /family/i then say "That's just life" | ||
else | ||
say "It shall pass" | ||
end | ||
message.typing_off | ||
stop_thread # future messages from user will be handled from top-level bindings | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
# require 'dotenv/load' # Use this if you want plain 'rackup' over 'heroku local' | ||
require 'sinatra' | ||
require 'facebook/messenger' | ||
require 'rubotnik/autoloader' | ||
require 'sinatra' | ||
|
||
require_relative './bot/main.rb' | ||
|
||
map('/webhook') do | ||
run Facebook::Messenger::Server | ||
end | ||
|
||
# run regular sinatra too | ||
# run regular Sinatra too | ||
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Commands | ||
API_URL = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.freeze | ||
|
||
# Lookup based on location data from user's device | ||
def lookup_location | ||
if message_contains_location? | ||
handle_user_location | ||
else | ||
say("Please try your request again and use 'Send location' button") | ||
end | ||
stop_thread | ||
end | ||
|
||
def handle_user_location | ||
coords = message.attachments.first['payload']['coordinates'] | ||
lat = coords['lat'] | ||
long = coords['long'] | ||
message.typing_on | ||
parsed = get_parsed_response(API_URL, "#{lat},#{long}") | ||
address = extract_full_address(parsed) | ||
say "Coordinates of your location: Latitude #{lat}, Longitude #{long}. " \ | ||
"Looks like you're at #{address}" | ||
message.typing_off | ||
end | ||
|
||
# Talk to API | ||
def get_parsed_response(url, query) | ||
response = HTTParty.get(url + query) | ||
parsed = JSON.parse(response.body) | ||
parsed['status'] != 'ZERO_RESULTS' ? parsed : nil | ||
end | ||
|
||
def extract_full_address(parsed) | ||
parsed['results'].first['formatted_address'] | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.