guidone / node-red-contrib-chatbot

Visually build a full featured chat bot for Telegram, Facebook Messenger, Whatsapp and Slack with Node-RED. Almost no coding skills required.
http://red-bot.io
933 stars 189 forks source link

Facebook node and connector 'null' #165

Closed ArfyFR closed 6 years ago

ArfyFR commented 6 years ago

Hi,

I wanted to get the chatId and created a FB Node (connected/green, ngrok process OK).

But I faced:

26 Jan 14:45:23 - [info] Started flows

------ WebHooks for FACEBOOK----------------
http://localhost:1880/redbot/facebook/test - Use this to test that your SSL (with certificate or ngrok) is working properly, should answer "ok"
http://localhost:1880/redbot/facebook - Use this in the "Webhooks" section of the Facebook App ("Edit Subscription" button)

Chat server started, transport: facebook
TypeError: Cannot read property 'on' of null
    at D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\chat-platform\chat-platform.js:528:24
    at process._tickCallback (internal/process/next_tick.js:103:7)

Node version: v6.9.1 node-red-contrib-chatbot : 0.9.13

And I don't get anything from FB/the FB page I linked in the config

Did I forget something ?

guidone commented 6 years ago

Checking...

guidone commented 6 years ago

Yes, the error was there. I've fixed it 0.9.14, pushed out right now.

My facebook bot worked despite this bug, I'm not sure this will solve your problem, if not let me know.

Thanks for the feedback

ArfyFR commented 6 years ago

Hi,

No more the error thanks.

But nothing from FB in the node as if it is shown "connected". Perhaps because my dev account in FB is too young ?

guidone commented 6 years ago

Sometimes FB takes some minutes to start sending messages to the new web hook, when this happens I look at ngrok console, you should see a POST call when ever you write a message in the chat. If this doesn't happen it could be:

where you able to validate the webhook with the verify token? In that situation did you see the GET call in the ngrok console?

guidone commented 6 years ago

Just in case you didn't see it, here's the tutorial

https://github.com/guidone/node-red-contrib-chatbot/wiki/Facebook-Receiver-node

ArfyFR commented 6 years ago

Yes I followed it =)

Little remark, it's a typo the port 1088 in the doc ? ngrok http 127.0.0.1:1088 I mean, on my PC, the default port is 1880 ;)

I launched another test and I am waiting for the POST at the moment.

Last remark: since I added node-red-contrib-chatbot, I lost some nodes config several times without relevent logs. I noticed after node-red launch saying in logs "auth/api key missing" (Auth config for Rainbow nodes, key for openweather and also auth config for Facebook)

ArfyFR commented 6 years ago

There is still an error.

The POST occurred and it crashed in your code: image

http://localhost:1880/redbot/facebook/test - Use this to test that your SSL (with certificate or ngrok) is working properly, should answer "ok"
http://localhost:1880/redbot/facebook - Use this in the "Webhooks" section of the Facebook App ("Edit Subscription" button)

Chat server started, transport: facebook
TypeError: Cannot read property 'forEach' of undefined
    at D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\facebook\facebook-chat.js:52:19
    at Array.forEach (native)
    at ChatServer./redbot/facebook (D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\facebook\facebook-chat.js:50:19)
    at Layer.handle [as handle_request] (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:275:10)
    at expressInit (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:275:10)
    at query (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\middleware\query.js:44:5)
    at Layer.handle [as handle_request] (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:174:3)
    at EventEmitter.handle (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\application.js:174:10)
TypeError: Cannot read property 'forEach' of undefined
    at D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\facebook\facebook-chat.js:52:19
    at Array.forEach (native)
    at ChatServer./redbot/facebook (D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\facebook\facebook-chat.js:50:19)
    at Layer.handle [as handle_request] (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:275:10)
    at expressInit (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:275:10)
    at query (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\middleware\query.js:44:5)
    at Layer.handle [as handle_request] (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\router\index.js:174:3)
    at EventEmitter.handle (D:\Users\paborsch\AppData\Roaming\npm\node_modules\node-red\node_modules\express\lib\application.js:174:10)
ArfyFR commented 6 years ago

I just uncommented your log code:


        console.log(prettyjson.render(req.body));```
to have more details... the next times there is a POST
guidone commented 6 years ago

Question: are you running your node-red instance with the command node-red or with some kind of wrapper?

Some of these wrapper don't add the bodyParser middleware and for this reason the POST payload is not parsed (json = null)

ArfyFR commented 6 years ago

Reply: node-red directly

About the error, I found how to send a test message 2 minutes ago =) image

Here are the logs with the JSON.stringify of the req.body

>>>>
{"entry":[{"changes":[{"field":"messages","value":{"page_id":"1067280970047460"}}],"id":"0","time":1517005945}],"object":"page"}
TypeError: Cannot read property 'forEach' of undefined
    at D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\facebook\facebook-chat.js:52:19

In the test message from FB, there is no element 'messaging' in the entry

guidone commented 6 years ago

Ok, let me check with this button, I've never tried that button, but at first sight the payload send by an actual chat message is different

ArfyFR commented 6 years ago

Bad copy paste, sorry, was the message from Page webhook Here is the message from User webhook

>>>>
{"entry":[{"time":1517006818,"changes":[{"field":"message_sends","value":{"to":{"data":[{"email":"bobbert@tehfacebook.com","id":20061985,"name":"Bobbert Cruz"}]},"message":"I'm changing jobs for the next 4+ years. Wanna be CEO?","from":{"email":"zuck@tehfacebook.com","id":44444444,"name":"Lizard Zuckerman"},"id":"mid.86753098675309:uuddlrlrbas"}}],"id":"0","uid":"0"}],"object":"user"}
TypeError: Cannot read property 'forEach' of undefined
    at D:\Users\paborsch\.node-red\node_modules\node-red-contrib-chatbot\lib\facebook\facebook-chat.js:52:19
guidone commented 6 years ago

Yes it's different, the payload from an actual message is something like that

{"object":"page","entry":[{"id":"494365110754397","time":1517008430668,"messaging":[{"sender":{"id":"1216752105043392"},"recipient":{"id":"494365110754397"},"timestamp":1517008429991,"message":{"mid":"mid.$cAAHBn0bzxENnZgTrp1hNL-jWpkbF","seq":109082,"text":"test 2"}}]}]}

The key messaging is missing and that is breaking the code. I can add a guard in the code preventing it to break in case someone uses a different test payload but this doesn't solve your problem. You should try to connect a page to your facebook app and try to send a message from the page, that will produce payloads that redbot is able to understand.

Where did you get this payload?

ArfyFR commented 6 years ago

Hmm, this morning I RTFM from Facebook but I didn't find something (https://developers.facebook.com/docs/messenger-platform/getting-started and inside https://developers.facebook.com/docs/messenger-platform/webhook). After that I went back to the app dev page, removed the one from yesterday and try a new one.

Still nothing from FB (nothing sent if I msg my Page) until I tried at the bottom the part (I thought it was only for "production") : App Review for Messenger : developers.facebook.com/apps//messenger/settings/ I had to fill an icon, information about privacy, ... and applied for pages_messaging. Some minutes later, I got the message in FB:

This is a test message from the Facebook team. Please respond from your app within 2 days to confirm that the integration is functioning

And in node-red: (cool) 27 Jan 08:43:52 - [warn] [chatbot-debug:e7c6de4d.5dd7e8] This is a test message from the Facebook team. Please respond from your app within 2 days to confirm that the integration is functioning.

But... I would have to reply to the message with the chatId. But I only had the FB IN + the Debug. I did this (cancelled subscription and did a new one) image but nothing went for the moment ... will see in one hour (I hope the chatId is shared ... sigh I didn't get the "memory" context)

ArfyFR commented 6 years ago

Cool, it worked: image

Thanks for your time !

Do you want me to create another issue for improving the doc about FB which now wants the app to reply ?

ArfyFR commented 6 years ago

Little update also for future developpers.

After the process I described to validate the sub to message, if you want a FB account, BEFORE PROD, to be allowed to test to send a msg and replies to your page and be notified in the node-red-contrib-chatbot, you'll need to browse to your FB App Roles developers.facebook.com/apps/<appid>/roles/roles/ and add it in Testers: image

And FB App should also be in "green state" like: image

ArfyFR commented 6 years ago

You noticed also for the doc, the nodes Flow I had to create for FB to

In comment: https://github.com/guidone/node-red-contrib-chatbot/issues/165#issuecomment-360970300