fbsamples / messenger-platform-samples

Messenger Platform samples for sending and receiving messages. Walk through the Get Started with this code. https://developers.facebook.com/docs/messenger-platform/quickstart
Other
1.72k stars 2.61k forks source link

Webhook cURL Post error #86

Closed marketingstarz closed 6 years ago

marketingstarz commented 6 years ago

Hi all, I followed these instructions to the letter (https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup) but I keep getting this error! What am I doing wrong?

Requesting side>>>

This works: curl -X GET "localhost:1337/webhook?hub.verify_token=999999999999999&hub.challenge=CHALLENGE_ACCEPTED&hub.mode=subscribe"

But not this: curl -H "Content-Type:application/json" -X POST "localhost:1337/webhook" -d '{"object": "page", "entry": [{"messaging": [{"message": "TEST_MESSAGE"}]}]}'

SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (anonymous)
at parse (C:\...\chatbot\node_modules\body-parser\lib\types\json.js:89:19) 
at C:\...\chatbot\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\...\chatbot\node_modules\raw-body\index.js:224:16)
at done (C:\...\chatbot\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\...\chatbot\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)

Node side>>>

SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at parse (C:\...\chatbot\node_modules\body-parser\lib\types\json.js:89:19)
    at C:\...\chatbot\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\...\chatbot\node_modules\raw-body\index.js:224:16)
    at done (C:\...\chatbot\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\...\chatbot\node_modules\raw-body\index.js:273:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)

issue can be reproduced when pushed up to Heroku and run in a production environment


node@8.9.4 npm@5.6.0 body-parser@1.18.2 express@4.16.2

express and body-parser installed via: npm install express body-parser --save


same issue as:

73

74


Gist Github https://gist.github.com/marketingstarz/8e8aec805adbe54e10a83e14ed9c99f7

marketingstarz commented 6 years ago

I'm starting to figure out that the actual code in the app works just fine. Everything parses fine, post and get work fine. It's something to do with the command line curl command. I was able to move on and create a successful messenger bot. That's probably why the two other guys just stop responding to their thread. This can be closed.

amuramoto commented 6 years ago

Thanks @marketingstarz. Glad you got it sorted. You might also want to check out our dev group to ask questions when bugs come up. https://www.facebook.com/groups/messengerplatform/

Theojs-bit commented 4 years ago

Hello @ marketingstarz I'm creating my webhook for my Messenger bot but i'm having the same issue as you have:

This works: curl -X GET "localhost:1337/webhook?hub.verify_token=token&hub.challenge=CHALLENGE_ACCEPTED&hub.mode=subscribe"

But not this: curl -H "Content-Type:application/json" -X POST "localhost:1337/webhook" -d '{"object": "page", "entry": [{"messaging": [{"message": "TEST_MESSAGE"}]}]}'

SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (anonymous) at parse (C:...\chatbot\node_modules\body-parser\lib\types\json.js:89:19) at C:...\chatbot\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (C:...\chatbot\node_modules\raw-body\index.js:224:16) at done (C:...\chatbot\node_modules\raw-body\index.js:213:7) at IncomingMessage.onEnd (C:...\chatbot\node_modules\raw-body\index.js:273:7) at emitNone (events.js:106:13) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1055:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) Node side>>>

SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at parse (C:...\chatbot\node_modules\body-parser\lib\types\json.js:89:19) at C:...\chatbot\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (C:...\chatbot\node_modules\raw-body\index.js:224:16) at done (C:...\chatbot\node_modules\raw-body\index.js:213:7) at IncomingMessage.onEnd (C:...\chatbot\node_modules\raw-body\index.js:273:7) at emitNone (events.js:106:13) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1055:12) at _combinedTickCallback (internal/process/next_tick.js:138:11)

What do you do to fix it? Thanks

marketingstarz commented 4 years ago

Shoot "Theojs-bit". I'm very sorry. It's been so long that I don't remember the solution.

On Fri, Nov 29, 2019 at 12:14 PM Theojs-bit notifications@github.com wrote:

Hello @ marketingstarz I'm creating my webhook for my Messenger bot but i'm having the same issue as you have:

This works: curl -X GET "localhost:1337/webhook?hub.verify_token=token&hub.challenge=CHALLENGE_ACCEPTED&hub.mode=subscribe"

But not this: curl -H "Content-Type:application/json" -X POST "localhost:1337/webhook" -d '{"object": "page", "entry": [{"messaging": [{"message": "TEST_MESSAGE"}]}]}'

SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (anonymous) at parse (C:...\chatbot\node_modules\body-parser\lib\types\json.js:89:19) at C:...\chatbot\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (C:...\chatbot\node_modules\raw-body\index.js:224:16) at done (C:...\chatbot\node_modules\raw-body\index.js:213:7) at IncomingMessage.onEnd (C:...\chatbot\node_modules\raw-body\index.js:273:7) at emitNone (events.js:106:13) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1055:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) Node side>>>

SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at parse (C:...\chatbot\node_modules\body-parser\lib\types\json.js:89:19) at C:...\chatbot\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (C:...\chatbot\node_modules\raw-body\index.js:224:16) at done (C:...\chatbot\node_modules\raw-body\index.js:213:7) at IncomingMessage.onEnd (C:...\chatbot\node_modules\raw-body\index.js:273:7) at emitNone (events.js:106:13) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1055:12) at _combinedTickCallback (internal/process/next_tick.js:138:11)

What do you do to fix it? Thanks

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fbsamples/messenger-platform-samples/issues/86?email_source=notifications&email_token=AA6LZ3ROSZD2Z62U6XKHF6LQWFLYVA5CNFSM4EN7VJX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFPMHHI#issuecomment-559858589, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6LZ3TP36HFTTR7KNKMS7LQWFLYVANCNFSM4EN7VJXQ .

--

Matthew Gregory matthew@marketingstarz.com / 480-258-1376 Skype: matthewgregory442 Book appointment: calendly.com/marketingstarz

MarketingStarz.com www.marketingstarz.com

We are what we repeatedly do.Excellence, therefore, is not an act but a habit.~ Aristotle

Theojs-bit commented 4 years ago

okay please help! remember please have you correct something in the post curl command?

riojosdev commented 4 years ago

https://github.com/fbsamples/messenger-platform-samples/issues/73#issuecomment-389123833

Check this link. This worked for me when I was having the same issue. Something to do with escaping quotes on Windows on the curl command.

This is the code I have used instead of the one on the guide from Facebook.

curl -H "Content-Type: application/json" -X POST "localhost:1337/webhook" -d {\"object\":\"page\",\"entry\":[{\"messaging\":[{\"message\":\"TEST_MESSAGE\"}]}]}

If you want to compare, here's the code from Facebook's guide when I am posting.

curl -H "Content-Type: application/json" -X POST "localhost:1337/webhook" -d '{"object": "page", "entry": [{"messaging": [{"message": "TEST_MESSAGE"}]}]}'