Skip to content

Commit

Permalink
Simplify duration and delay with timeout with its own map
Browse files Browse the repository at this point in the history
  • Loading branch information
feychou committed Jul 7, 2017
1 parent f8ea07f commit 273b381
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
43 changes: 10 additions & 33 deletions src/facebook_example/bot_core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@

; SCHEMA FOR REPLIES
(s/def ::action #{"typing_on" "typing_off" "mark_seen"})
(s/def ::duration int?)
(s/def ::delay int?)
(s/def ::timeout int?)
(s/def ::message map?)

(s/def ::message-reply (s/keys :req-un [::message]
:opt-un [::delay]))

(s/def ::action-reply (s/keys :req-un [::action]
:opt-un [::duration ::delay]))
(s/def ::message-reply (s/keys :req-un [::message]))
(s/def ::action-reply (s/keys :req-un [::action]))
(s/def ::timeout-reply (s/keys :req-un [::timeout]))

(s/def ::reply (s/or :message-reply ::message-reply
:action-reply ::action-reply))
:action-reply ::action-reply
:timeout-reply ::timeout-reply))

; MATCH USER INPUT
(defn process-event [event]
Expand Down Expand Up @@ -61,39 +59,18 @@
; The order of the matching clauses is important!
(match [reply]

; The bot wants to send a message (text, images, videos etc.) after n milliseconds
[{:message message :delay timeout}]
(do
(Thread/sleep timeout)
(facebook/send-message sender-id message))

; The bot wants to send a message (text, images, videos etc.)
[{:message message}]
(facebook/send-message sender-id message)

; The bot wants to perform an action for n milliseconds after n milliseconds
[{:action action :duration duration :delay delay}]
(do
(Thread/sleep delay)
(facebook/send-sender-action sender-id action)
(Thread/sleep duration))

; The bot wants to perform an action after n milliseconds
[{:action action :delay delay}]
(do
(Thread/sleep delay)
(facebook/send-sender-action sender-id action))

; The bot wants to perform an action for n milliseconds (typing_on in most cases)
[{:action action :duration timeout}]
(do
(facebook/send-sender-action sender-id action)
(Thread/sleep timeout))

; The bot wants to perform an action (mark_seen, typing_on, typing_off)
[{:action action}]
(facebook/send-sender-action sender-id action)

; The not wants to wait n milliseconds
[{:timeout timeout}]
(Thread/sleep timeout)

:else
(throw (ex-info "You have provided an invalid pattern in your reply."
{:causes reply}))))))
25 changes: 16 additions & 9 deletions src/facebook_example/reaction.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
(:gen-class)
(:require [fb-messenger.templates :as templates]))

; You can use two kind of replies, actions or messages.
; You can use three kind of replies: actions, messages and timeout.
; Find the documentation for actions here: https://developers.facebook.com/docs/messenger-platform/send-api-reference/sender-actions
; Messages are not only text messages https://developers.facebook.com/docs/messenger-platform/send-api-reference/text-message
; but also more complex UI elements supported by the FB Messenger API, like for instance quick-replies https://developers.facebook.com/docs/messenger-platform/send-api-reference/quick-replies
; or templates https://developers.facebook.com/docs/messenger-platform/send-api-reference/templates

; Structure your actions this way:
; {:action "typing_on" :duration 3000 :delay 2000} --> this means the bot will wait 2 seconds to send a typing_on action that will last for 3 seconds.
; Structure your messages this way
; {:message (templates/text-message "Hello") :delay 3000} --> this means the bot will wait 3 seconds before sending the text message.
; :duration and :delay are always optional keys
; For instance, if you want your bot to keep typing for 3 seconds, write this:
; [{:action "typing_on"}
; {:timeout 3000}]

; If you want your bot to see a message and reply after 2 seconds, write this:
; [{:action "mark_seen"}
; {:timeout 2000}
; {:message (template/text-message "Alright!")}]

(defn some-image []
[{:message (templates/image-message "https://upload.wikimedia.org/wikipedia/commons/e/ef/Tunturisopuli_Lemmus_Lemmus.jpg")}])
Expand All @@ -21,16 +24,20 @@
[{:message (templates/text-message message-text)}])

(defn welcome []
[{:action "typing_on" :duration 3000}
[{:action "typing_on"}
{:timeout 3000}
{:message (templates/text-message "Welcome, fellow lemming =)")}
{:message (templates/image-message "https://upload.wikimedia.org/wikipedia/commons/e/ef/Tunturisopuli_Lemmus_Lemmus.jpg")}])

(defn error []
[{:message (templates/text-message "Sorry, I didn't get that! :(")}])

(defn thank-for-attachment []
[{:action "mark_seen" :delay 3000}
{:action "typing_on" :delay 3000 :duration 5000}
[{:timeout 3000}
{:action "mark_seen"}
{:timeout 3000}
{:action "typing_on"}
{:timeout 3000}
{:message (templates/text-message "Thank you for your attachment :)")}])

(defn help []
Expand Down

0 comments on commit 273b381

Please sign in to comment.