Facebook chatbota z wielu słów kluczowych

głosy
0

Zrobiłem tutorial jak skonfigurować Facebook chatbota dla fanpage. Działa idealnie i również może zmienić słowo kluczowe, a wiadomość, że pojawia się w przypadku, gdy użytkownik wpisze dokładne słowa kluczowego.

Teraz jedyną rzeczą jest to, że nie dowiedzieć się, jak dodać wiele słów kluczowych z odpowiedziami.

var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
var app = express()

app.set('port', (process.env.PORT || 5000))

// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))

// Process application/json
app.use(bodyParser.json())

// Index route
app.get('/', function (req, res) {
    res.send('Hello world, I am a chat bot')
})

// for Facebook verification
app.get('/webhook/', function (req, res) {
    if (req.query['hub.verify_token'] === 'token') {
        res.send(req.query['hub.challenge'])
    }
    res.send('Error, wrong token')
})

// Spin up the server
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'))
})


// API End Point - 

app.post('/webhook/', function (req, res) {
    messaging_events = req.body.entry[0].messaging
    for (i = 0; i < messaging_events.length; i++) {
        event = req.body.entry[0].messaging[i]
        sender = event.sender.id
        if (event.message && event.message.text) {
            text = event.message.text
            if (text === 'hi') {
                sendGenericMessage(sender)
                continue
            }
            sendTextMessage(sender, parrot:  + text.substring(0, 200))
        }
        if (event.postback) {
            text = JSON.stringify(event.postback)
            sendTextMessage(sender, Postback received: +text.substring(0, 200), token)
            continue
        }
    }
    res.sendStatus(200)
})

var token =  enter token here

// function to echo back messages

function sendTextMessage(sender, text) {
    messageData = {
        text:text
    }
    request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {access_token:token},
        method: 'POST',
        json: {
            recipient: {id:sender},
            message: messageData,
        }
    }, function(error, response, body) {
        if (error) {
            console.log('Error sending messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}


// Send an test message back as two cards.

function sendGenericMessage(sender) {
    messageData = {
        attachment: {
            type: template,
            payload: {
                template_type: generic,
                elements: [{
                    title: title 1,
                    subtitle: subtitle,
                    image_url: url of image,
                    buttons: [{
                        type: web_url,
                        url: https://www.facebook.com/,
                        title: Facebook
                    }, {
                        type: web_url,
                        url: https://www.reddit.com,
                        title: Reddit
                    },{
                        type: web_url,
                        url: https://twitter.com/,
                        title: Twitter
                    }],
                }, {
                    title: Title 2 ,
                    subtitle: Subtitle,
                    image_url: imgurl,
                    buttons: [{
                        type: postback,
                        title: title,
                        payload: payload,
                    },{
                        type: postback,
                        title: title,
                        payload: payload,
                    }, {
                        type: postback,
                        title: title,
                        payload: payload,
                    }],
                },  {
                    title: title,
                    subtitle: subtitle,
                    image_url: http://www.google.com,
                    buttons: [{
                        type: postback,
                        title: title,
                        payload: payload,
                    },{
                        type: postback,
                        title: title,
                        payload: payload,
                    }, {
                        type: postback,
                        title: title,
                        payload: payload,
                    }],
                }]  
            } 
        }
    }
    request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {access_token:token},
        method: 'POST',
        json: {
            recipient: {id:sender},
            message: messageData,
        }
    }, function(error, response, body) {
        if (error) {
            console.log('Error sending messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}

Utwórz 25/06/2017 o 16:19
źródło użytkownik
W innych językach...                            


1 odpowiedzi

głosy
0

Czy chcesz powiedzieć - trzeba obsłużyć wiele kluczowych dla samej odpowiedzi? Jeśli tak jest, to facebook wysyła wiadomość jako tekst na webhook. Trzeba go złapać i wysłać wiadomość z powrotem do facebook odpowiednio.

Jak w kodzie wysłałeś, [if (tekst === „cześć”)], trzeba przykładowe komunikaty (cześć w tym przypadku), które mają być przechowywane w systemie i map odpowiedź.

Odpowiedział 18/09/2017 o 07:36
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more