Schmavery / facebook-chat-api

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

searchForThread could not find thread name #396

Open lyneca opened 7 years ago

lyneca commented 7 years ago

The docs for searchForThread say that it can be used to search for "chat titles", but when I search for the name of one of my group chats, it returns Could not find thread [name]. Is the documentation old or am I just using it wrong?

ravkr commented 7 years ago

You're right, I just tried to search by thread name and nothing is found... but when I'm putting e.g. name of one of the participants then I get array of threads with that user :/

lyneca commented 7 years ago

Must be a Facebook thing then - this has worked in the past. I'll poke around in the chrome dev tools and see what I can find

lyneca commented 7 years ago

Ok it has changed, Facebook now sends a call to messenger.com/api/graphqlbatch/ which returns three JSON strings - one for users that match your search, one for threads that match your search, and a paging info string.

It's a get request with form data - the important bits of data that I could see are these:

'batch_name': 'MessengerTypeaheadResultRoute',
'queries':
'{
  "q3": {
    "priority": 0,
    "q": "Query MessengerTypeaheadResultRoute {entities_named([my search]) {id,@F4}} QueryFragment F0 : User {id,name,is_memorialized,is_viewer_friend,is_viewer_coworker,profile_picture.width(32).height(32) as _profile_picture4QyiK {uri},messenger_contact {is_on_viewer_contact_list,id},url,is_messenger_user,is_work_user,viewer_affinity} QueryFragment F1 : Node {id,__typename} QueryFragment F2 : Page {id,name,profile_picture.width(32).height(32) as _profile_picture4QyiK {uri},url,is_messenger_user} QueryFragment F3 : Application {id,name,instant_game_info {icon_uri}} QueryFragment F4 : SearchableEntitiesQuery {search_results.of_type(user).include_messenger_only_results_for_web_messenger(true).first(20) as _search_resultsLr77o {edges {node {__typename,@F0,@F1},cursor},page_info {has_next_page,has_previous_page}},search_results.messenger_commerce(true).include_games(true).first(5) as _search_resultsSJD9D {edges {node {__typename,@F2,@F3,@F1},cursor},page_info {has_next_page,has_previous_page}},id}",
    "query_params": {}
  },
  "q4": {
    "priority": 0,
    "q": "Query MessengerTypeaheadResultRoute {viewer() {@F2}} QueryFragment F0 : User {is_messenger_user,id} QueryFragment F1 : MessageThread {thread_key {thread_fbid},name,image {uri},is_group_thread,other_participants.first(250) as _other_participantsDPwC6 {edges {node {messaging_actor {id,name,__typename,@F0},id},cursor},page_info {has_next_page,has_previous_page}},id} QueryFragment F2 : Viewer {message_threads.with_thread_name([my search]).last(8) as _message_threads4xmW31 {edges {node {id,@F1},cursor},page_info {has_next_page,has_previous_page}},message_threads.with_participant_ids(494192200755039).last(8) as _message_threads1Sj6ZQ {edges {node {id,@F1},cursor},page_info {has_next_page,has_previous_page}}}",
    "query_params": {}
  }
}'

Looks like they've got their own query language. I've replaced my search with [my search], and it looks like there are a bunch of numbers that we could add into a function.

The q3 and q4 bits increment every time you search, I don't think we would have to generate them.

lyneca commented 7 years ago

Yup, their query language is called GraphQL and it's apparently quite popular (I've never heard of it 😛). There's a node.js module for it here

ravkr commented 7 years ago

I haven't heard about it to... :/ Are you going to update searchForThread function? :D

lyneca commented 7 years ago

Haha I'll have a shot

lyneca commented 7 years ago

Wow, one of these requests shows the affinity between you and all of your friends - i.e. my girlfriend as an affinity of 0.99999999999973, whereas a less interacted-with friend has an affinity of 0.58893558687104

ravkr commented 7 years ago

woow :smiley: if I remember correctly getUserID() was returning something similar as score

Schmavery commented 7 years ago

Nice investigation @lyneca, we've been wondering for a while when graphql would come to haunt us. Let us know if you run into any trouble fixing this.

bsansouci commented 7 years ago

Hey @lyneca if you're interested you could look at other requests they make from messenger.com. That would be an immense help and could be super fun. If we can understand their query language for messenger we can have a much more solid API. Their graphql backend should be 100% backwards compatible, forever. They brag a lot about still supporting the early version of FB ios app thanks to graphql.

lyneca commented 7 years ago

Yeah, I reckon supporting the graphql backend would be pretty awesome. One question, which method in your API allows you to send a cookied request? Once I know where that is I can do a lot more testing

Schmavery commented 7 years ago

@lyneca sorry, never saw your question. defaultFuncs.post should be what you're looking for. You can check some other files in src for an example, almost all of them do this.

bsansouci commented 7 years ago

@lyneca Hey I just commited https://github.com/Schmavery/facebook-chat-api/blob/9df16a7727edc080cb4974a8f888bfeddab0abc0/src/getThreadHistoryGraphQL.js which is our first graphql support. If you're still interested that file might help you see how we're going about implementing this. We're open to PRs ;)

lyneca commented 7 years ago

Oh awesome! Sorry, I completely forgot about that. I'll have a look when I have some time.