Following on from Mark Zuckerberg’s home automation project and integration with Facebook messenger as a chat bot, I thought I’d try to do it myself… I’ve got as far as getting facebook messenger to authorise and to send events to ST… but my coding ability is limited… and can’t work out how to process the values in the POST request being sent to ST, which someone on here I expect could work out in 5 minutes?
Here’s the basic code outline for anyone to use, you will need to validate the user ID in your code to stop just anyone sending messages in to your home:
Feel free to grab and use the code as you all want in your own apps…
definition(
name: "Facebook Bot",
namespace: "jebbett",
author: "Jake Tebbett",
description: "Facebook Bot",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png"
)
preferences {
page name: "mainPage"
}
private mainPage() {
dynamicPage(name: "mainPage", title: "", install: true, uninstall: true) {
section ("Facebook Messenger Linking") {
input "verifyToken", "password", title: "Verify Token", description:"", required: true
input "fbAccessToken", "password", title: "FB Page Access Token", description:"", required: true
paragraph "Verify Token: Used to setup link to FB bot (You make up this value)\nAccess Token: To allow ST to send messages via FB Messenger (From developers.facebook.com)"
}
}
}
mappings {
path("/m") { action: [GET: "messengerGetHandler", POST: "messengerPostHandler"] }
}
def messengerGetHandler(){
log.warn "FB MESSENGER GET EVENT: ${params}"
if (params.hub.mode == 'subscribe' && params.hub.verify_token == settings.verifyToken) {
log.debug "Validating webhook"
render contentType: "text/html", data: params.hub.challenge, status: 200
} else {
log.debug "Failed validation. Make sure the Verify Tokens match."
render contentType: "text/html", data: params.hub.challenge, status: 403
}
}
def messengerPostHandler(){
def fbJSON = request.JSON
log.debug "FB MESSENGER POST EVENT: ${params}"
log.debug "FB MESSENGER POST EVENT - JSONREQUEST: ${fbJSON}"
log.debug "FB MESSENGER POST EVENT - MESSAGE: ${fbJSON.entry.messaging.message.text[0][0]}"
log.debug "FB MESSENGER POST EVENT - SENDER_ID: ${fbJSON.entry.messaging.sender.id[0][0]}"
def params = [
uri: "https://graph.facebook.com/v2.6/me/messages?access_token=$fbAccessToken",
body: [
recipient: [id: fbJSON.entry.messaging.sender.id[0][0]],
message: [text: "hello, world!"]
]
]
try {
httpPostJson(params) { resp ->
resp.headers.each {
log.debug "${it.name} : ${it.value}"
}
log.debug "response contentType: ${resp.contentType}"
}
} catch (e) {
log.debug "something went wrong: $e"
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
// Create Access Token
if (!state.accessToken) {
try {
createAccessToken()
log.debug "Created Access Token"
} catch (e) {
log.debug "Please enable OAuth in the IDE"
}
}
// publish app ID and token to debug logging
log.debug "URL FOR USE AT DEVELOPERS.FACEBOOK.COM: ${getApiServerUrl()}/api/smartapps/installations/${app.id}/m?access_token=${state.accessToken}"
}
Could the same type of integration be done using Microsoft’s Bot Framework? I am interested in having Cortana send and receive notifications to/from ST. Ideas?
For the Facebook bot I now have it returning buttons to trigger actions, so if I send “List” it’ll return buttons for actions I can trigger, although the API limits it to 11 buttons which is a shame…
The only integration I have written is with my SmarterHome app, I can also trigger CoRE pistons with a work around although I’d like to make that official I haven’t had the time to discuss it with the project… I don’t have anything to integrate with standard ST devices… but I don’t think there’s a lot of demand based on activity on this thread
Cool would be good to see what you’ve done! Mine is available on my GitHub it’s called Barker and does both Facebook and Echo integration but again really only works today with SmarterHome, Echosistant will likely add the same functionality in the future as well… but they have other more important things going on at the moment
Could you detail a little more the required setup? I install the smartapp above but cant pass the first page while config, not sure which number i need for Access Token, got into developers.facebook.com, create an app, and there i have ID and secret key from the app in basic info, which one is the access token and fb page access token? Can you help me with this, i’m not familiarize with it. Thanks,
I did enable oauth in the smartapp settings, just click enable, didnt fill any textbox. However on the live long i got this after hit ‘done’:
4e9c25be-ad96-4aac-8926-b31199ac11c1 10:22:06 AM: error groovy.lang.MissingMethodException: No signature of method: script14975435725981329063166.logWriter() is applicable for argument types: (java.lang.String) values: [Please enable OAuth in the IDE] @ line 84
on the smartapp i fill verify token with random numbers, and for Access token i use the secret key i got from facebook, looks something like this: 945d306c16ab2b462a1dda9a23b1b299, is that the value or do i need to use secret key from smartapp?
Hey sorry, I’ve just updated my code above, I left a bit in from my code that shouldn’t have been in the code above… update and try again… should be fine then…
Firstly edit your above post to remove that link, you don’t want to share that!
Secondly that link you need to setup in Facebook dev along with the verify token that you have put in the smart app… Facebook dev will then give you the Facebook access token and then you can paste that in the app…