Schmavery / facebook-chat-api

Unofficial Facebook Chat API for Nodejs
MIT License
1.93k stars 595 forks source link

Login as Page #17

Closed appa1303 closed 9 years ago

appa1303 commented 9 years ago

In the Facebook API there is a way to read Page messages (through Subscription to conversation) and reply, but it is very slow (delay of at least 5 sec). Is there a way to use this API to connect as a Facebook page, get the messages for the Page and reply ?

Thanks

bsansouci commented 9 years ago

Hey @appa1303 does this help? You can just do setOptions({pageId: YOU_PAGE_ID}) before calling listen. You can get that id just by going to your page and replacing "www" in the url with "graph". For example: https://graph.facebook.com/FuzzBeedOfficial will show FuzzBeed's page ID.

After that listen will only receive messages sent to that page and sendMessage/sendSticker will send on the page's behalf.

appa1303 commented 9 years ago

Thanks, But, when I set the pageId option, I don't get messages for the page or the owner of the page. Looks like the messages are getting to the API there because I can see the effect on the log message "Got answer in" times.

Schmavery commented 9 years ago

Hey @appa1303, Sounds like you might be using the npm package. We hadn't published the changes to npm yet so that you'd have a chance to let us know if it's what you wanted. I just published a new release, try updating to 0.2.2 and let me know if that helps. If you still have a problem then, we'll need to go back and see what we missed ;)

appa1303 commented 9 years ago

Hi, Thanks, I already used the sources directly from GIT to overwrite the ...\node_modules\facebook-chat-api\ Just to be on the safe side, i also used the NPM just now. But with the same results. Sorry,

bsansouci commented 9 years ago

Mmh strange. Does your bot have enough rights to see the messages on the Facebook page?

appa1303 commented 9 years ago

I am logging in to the API with the user that originally created the page, so I assume it has all the required permissions, am i missing something ? I will try again from scratch and create a new page.

Schmavery commented 9 years ago

Is it possible that you're sending your test messages from the same account that the bot is logged into? If you are, the bot won't see them by default. Just a thought.

appa1303 commented 9 years ago

I am using two different accounts. Made another test, even with a completely new page (the login user is the creator and ADMIN) i am not getting any messages. Again, looks like the API is getting something because i see effect on the "Got answer in" times.

Schmavery commented 9 years ago

Can you add a console.log(v); right before the switch on line 87 of src/listen.js and let us know what the result is after receiving a message?

appa1303 commented 9 years ago

Placed the Console.log Looks like facebook is sending "alert_id: 1433948584071228" followed by HTML I loaded the HTML and it looks like: ++++++++++++++++++++++++++++++++ capture +++++++++++++++++++++++++++++++++ So facebook is asking the user if he want to get messages from the Page, Looking for a setting in the user profile i couldn't find a way to globally approve incoming messages. Am I missing something ?

Here is the complete log:

D:\Tests\FaceBook\Old Junk\Test>npm install facebook-chat-api
npm WARN deprecated CSSselect@0.4.1: the module is now available as 'css-select'

npm WARN deprecated CSSwhat@0.4.7: the module is now available as 'css-what'
facebook-chat-api@0.2.2 node_modules\facebook-chat-api
├── bluebird@2.9.27
├── npmlog@1.2.1 (ansi@0.3.0, gauge@1.2.0, are-we-there-yet@1.0.4)
├── cheerio@0.18.0 (entities@1.1.1, dom-serializer@0.0.1, htmlparser2@3.8.3, lod
ash@2.4.2, CSSselect@0.4.1)
└── request@2.57.0 (caseless@0.10.0, aws-sign2@0.5.0, forever-agent@0.6.1, strin
gstream@0.0.4, tunnel-agent@0.4.0, oauth-sign@0.8.0, isstream@0.1.2, json-string
ify-safe@5.0.1, node-uuid@1.4.3, qs@3.1.0, combined-stream@1.0.3, form-data@0.2.
0, bl@0.9.4, mime-types@2.0.14, tough-cookie@2.0.0, http-signature@0.11.0, hawk@
2.3.1, har-validator@1.7.1)

D:\Tests\FaceBook\Old Junk\Test>node test.js
info Getting login form data
info Logging in...
info Logged in
info Initial requests...
info Request to null_state
info Request to reconnect
info Request to pull 1
info Request to pull 2
info Request to sync
info Request to thread_sync
info Getting read access.
info Getting write access.
info Getting extended access.
info Done loading.
info Got answer in  9139
info Got answer in  945
{ app_id: 145634995501895,
  is_currently_anonymous: false,
  realtime_viewer_fbid: 100003528658494,
  type: 'anonymous_login_status' }
{ app_id: 145634995501895,
  is_currently_anonymous: false,
  realtime_viewer_fbid: 100003528658494,
  type: 'anonymous_login_status' }
{ app_id: 145634995501895,
  is_currently_anonymous: false,
  realtime_viewer_fbid: 100003528658494,
  type: 'anonymous_login_status' }
info Got answer in  15857
{ alert_id: 1433948584071228,
  markup: '<li class="notification jewelItemNew" data-gt="&#123;&quot;alert_id&q
uot;:&quot;1433948584071228&quot;,&quot;app_id&quot;:&quot;2530096808&quot;,&quo
t;unread&quot;:&quot;1&quot;,&quot;time_sent&quot;:&quot;1433997287&quot;,&quot;
context_id&quot;:&quot;1458178221160371&quot;,&quot;from_uids&quot;:&#123;&quot;
1458178221160371&quot;:&quot;1458178221160371&quot;&#125;,&quot;notif_type&quot;
:&quot;fbpage_new_message&quot;&#125;" data-notiftime="1433997287" data-readness
="1" id="notification_1433948584071228"><a class="notifMainLink" href="https://w
ww.facebook.com/Chat1301-1458178221160371/manager/messages/inbox/?ref=notif&amp;
notif_t=fbpage_new_message"><div class="clearfix notif_block"><img class="-cx-PR
IVATE-uiSquareImage__root _s0 -cx-PRIVATE-uiImageBlock__image _8o -cx-PRIVATE-ui
ImageBlock__smallimage _8r lfloat -cx-PUBLIC-uiLeftRight__left _ohe -cx-PRIVATE-
uiSquareImage__large _rw img" src="https://fbcdn-profile-a.akamaihd.net/hprofile
-ak-xaf1/v/t1.0-1/c15.0.50.50/p50x50/399548_10149999285987789_1102888142_n.png?o
h=07e213a07f54e4d9c8c4015109d9b78f&amp;oe=55F6ED6A&amp;__gda__=1442055073_fe170c
b6a53c85ab0a54935cbe1604c0" alt="" /><div class="-cx-PRIVATE-uiImageBlock__small
content _8u -cx-PRIVATE-uiFlexibleBlock__flexiblecontent _42ef"><div id="notific
ation_1433948584071228_info" class="info"><span class="blueName">Chat1301</span>
 received 5 new messages <div class="clearfix metadata"><img class="staticAppIco
n -cx-PRIVATE-uiImageBlockDeprecated__image _29h -cx-PRIVATE-uiImageBlockDepreca
ted__iconimage _29i img" src="https://fbstatic-a.akamaihd.net/rsrc.php/v2/y4/r/-
PAXP-deijE.gif" alt="" style="background-image: url(https://fbstatic-a.akamaihd.
net/rsrc.php/v2/ye/r/MzuWKRmFYMZ.png);" /><span class="timestamp -cx-PRIVATE-uiI
mageBlockDeprecated__iconcontent _29j -cx-PRIVATE-uiImageBlockDeprecated__conten
t _29k fss fwn fcg"><abbr title="Wednesday, June 10, 2015 at 9:34pm" data-utime=
"1433997287" class="timestamp livetimestamp">2 seconds ago</abbr></span></div></
div></div></div></a><div class="notif_x_div"><a class="-cx-PRIVATE-fbReadToggle_
_active _5c9q" href="#" role="button"></a><a data-hover="tooltip" aria-label="Tu
rn Off" data-tooltip-alignh="center" class="notif_x uiCloseButton" href="#" role
="button" data-gt="&#123;&quot;alert_id&quot;:&quot;1433948584071228&quot;,&quot
;app_id&quot;:&quot;2530096808&quot;,&quot;unread&quot;:&quot;1&quot;,&quot;time
_sent&quot;:&quot;1433997287&quot;,&quot;context_id&quot;:&quot;1458178221160371
&quot;,&quot;from_uids&quot;:&#123;&quot;1458178221160371&quot;:&quot;1458178221
160371&quot;&#125;,&quot;notif_type&quot;:&quot;fbpage_new_message&quot;,&quot;n
egative_feedback&quot;:&quot;negative_click&quot;&#125;"></a></div><div class="c
onfirmingMsg"><table class="uiGrid -cx-PRIVATE-uiGrid__root _51mz" cellspacing="
0" cellpadding="0"><tbody><tr class="-cx-PRIVATE-uiGrid__row _51mx"><td class="-
cx-PRIVATE-uiGrid__cell _51m- pas">Turn off notifications about New Messages for
 <span class="blueName">Chat1301</span>?</td><td class="-cx-PRIVATE-uiGrid__cell
 _51m- pas confirming_table_row -cx-PRIVATE-uiGrid__rightcol _51mw"><form rel="a
sync" action="/ajax/notifications/negative_req.php" method="post" onsubmit="retu
rn window.Event &amp;&amp; Event.__inlineSubmit &amp;&amp; Event.__inlineSubmit(
this,event)" id="u_oyl60d_2"><input type="hidden" autocomplete="off" value="turn
_off" name="request_type" /><input type="hidden" autocomplete="off" value="14339
48584071228" name="notification_id" /><label class="uiButton uiButtonConfirm" fo
r="u_oyl60d_0"><input value="Turn Off" name="confirm" data-gt="&#123;&quot;alert
_id&quot;:&quot;1433948584071228&quot;,&quot;app_id&quot;:&quot;2530096808&quot;
,&quot;unread&quot;:&quot;1&quot;,&quot;time_sent&quot;:&quot;1433997287&quot;,&
quot;context_id&quot;:&quot;1458178221160371&quot;,&quot;from_uids&quot;:&#123;&
quot;1458178221160371&quot;:&quot;1458178221160371&quot;&#125;,&quot;notif_type&
quot;:&quot;fbpage_new_message&quot;,&quot;negative_feedback&quot;:&quot;negativ
e_confirm&quot;&#125;" type="submit" id="u_oyl60d_0" /></label><label class="uiB
utton" for="u_oyl60d_1"><input value="Keep On" name="cancel" data-gt="&#123;&quo
t;alert_id&quot;:&quot;1433948584071228&quot;,&quot;app_id&quot;:&quot;253009680
8&quot;,&quot;unread&quot;:&quot;1&quot;,&quot;time_sent&quot;:&quot;1433997287&
quot;,&quot;context_id&quot;:&quot;1458178221160371&quot;,&quot;from_uids&quot;:
&#123;&quot;1458178221160371&quot;:&quot;1458178221160371&quot;&#125;,&quot;noti
f_type&quot;:&quot;fbpage_new_message&quot;,&quot;negative_feedback&quot;:&quot;
negative_cancel&quot;&#125;" type="submit" id="u_oyl60d_1" /></label><div class=
"negative_loading"><img class="uiLoadingIndicatorAsync img" src="https://fbstati
c-a.akamaihd.net/rsrc.php/v2/yb/r/GsNJNwuI-UM.gif" alt="" width="16" height="11"
 /></div></form></td></tr></tbody></table></div></li>',
  time: 1433997287,
  realtime_viewer_fbid: 100003528658494,
  type: 'notification' }
info Got answer in  767
{ nodes:
   [ { id: 'c3Rvcnk6MTAwMDAzNTI4NjU4NDk0OjE0MzM5NDg1ODQwNzEyMjg6NDUxNzYxNzExNTI2
Njg3',
       actors: [Object],
       title: [Object],
       application: [Object],
       attachments: [Object],
       feedback_context: null,
       attached_story: null,
       icon: [Object],
       seen_state: 'UNSEEN_AND_UNREAD',
       tracking: '{"notif_type":"fbpage_new_message","context_id":"1458178221160
371","alert_id":"1433948584071228","unread":1,"from_uids":{"1458178221160371":"1
458178221160371"}}',
       url: 'https://www.facebook.com/Chat1301-1458178221160371/manager/messages
/inbox/?ref=notif&notif_t=fbpage_new_message',
       creation_time: 1433997287,
       substory_count: 0,
       negative: [Object],
       alert_id: '100003528658494:1433948584071228',
       showBeep: true,
       beepUpdatesOnTop: true,
       beepRenderer: null,
       rendererData: null,
       business_ids: [],
       ajaxify_url: null,
       timestamp: [Object],
       notif_type: 'fbpage_new_message',
       isCommunity: null,
       unaggregatedTitle: [Object],
       unaggregatedActors: [Object],
       sound: false } ],
  servertime: 1433997288,
  realtime_viewer_fbid: 100003528658494,
  type: 'notification_json' }
{ data:
   { alert_id: 1433948584071228,
     content_id: 1458178221160371,
     href: '/n/100003528658494/1433948584071228?ref=m_notif&notif_t=fbpage_new_m
essage',
     body: { __html: '<span class="blueName">Chat1301</span> received 5 new mess
ages' },
     unread: 1,
     type: 'fbpage_new_message',
     app_id: 2530096808,
     time: 'Just now',
     graphql_id: 'c3Rvcnk6MTAwMDAzNTI4NjU4NDk0OjE0MzM5NDg1ODQwNzEyMjg6NDUxNzYxNz
ExNTI2Njg3',
     profile_pic: { __html: '<img src="https://fbcdn-profile-a.akamaihd.net/stat
ic-ak/rsrc.php/v2/yE/r/XuYbbmk2a4X.png" class="img profpic" height="43" width="4
3" />' },
     profile_pic_2x: { __html: '<img src="https://fbcdn-profile-a.akamaihd.net/s
tatic-ak/rsrc.php/v2/yE/r/XuYbbmk2a4X.png" class="img profpic" height="43" width
="43" />' },
     app_icon: { __html: '<img class="img" src="https://fbstatic-a.akamaihd.net/
rsrc.php/v2/ye/r/MzuWKRmFYMZ.png" alt="" />' } },
  realtime_viewer_fbid: 100003528658494,
  type: 'm_notification' }
{ n: true,
  i: 30000,
  realtime_viewer_fbid: 100003528658494,
  type: 'notifications_sync' }
info Got answer in  50728
info Got answer in  50806
info Got answer in  50883
info Got answer in  50678
info Got answer in  50830
info Got answer in  50893
info Got answer in  50782
info Got answer in  50670
info Got answer in  50845
info Got answer in  51236
info Got answer in  50830
info Got answer in  50830
info Got answer in  51056
info Got answer in  50783
info Got answer in  51190
info Got answer in  50954
info Got answer in  50868
info Got answer in  51085
info Got answer in  50715
info Got answer in  50634
info Got answer in  51060
info Got answer in  51087
info Got answer in  50810
info Got answer in  50761
info Got answer in  50721
jackwilsdon commented 9 years ago

Could you put output in a code block so it can be read easier? You could either put three grave accents (```) before and after the output or indent it all by 4 spaces / 1 tab.

Schmavery commented 9 years ago

Hey @appa1303, Any chance you could add our bot as an admin on your test page? Might help with the debugging.

Also can we see your code somewhere? This looks really strange and we can't reproduce it on our end so far.

appa1303 commented 9 years ago

Hi, I use the example code the the readme (just add pageId) Just to be on the safe side i created a new Facebook account and a new page, but the problem persists. the page id is 890324534375690 and Facebook requires you need to accept it as friend to get Admin role. the test facebook i use account is: Test'a Bot'a

If you require i will send you the user and password as well

Schmavery commented 9 years ago

Thanks for the information, we'll look at this some more as soon as we get a chance.

Schmavery commented 9 years ago

Update: We were able to reproduce the problems you were having, so hopefully we'll be able to come up with a fix in the near future.

bsansouci commented 9 years ago

@appa1303 try this and tell us if it works :)

(we changed how login gets called, you need to pass in the options object so that it knows to login as a page)

login({email: MY_EMAIL, password: MY_PASSWORD}, {pageId: MY_PAGE_ID}, function(err, api) {
    // Stuff
});
appa1303 commented 9 years ago

Thanks