5

There is How can I construct a link to view a message on facebook.com if I have the message id question, but it is unanswered. What if I got the id of the thread using /me/inbox API endpoint and need to redirect user to the Facebook itself showing this thread? How do I construct the URL. There seem to be URLs like http://www.facebook.com/messages/?action=read&tid=id.143666952390138 where thread id is the number in the end. But there are also some stranger URLs like http://www.facebook.com/messages/?action=read&tid=27726d81656e4c07ae5654116cccb724 where the previous rule doesn't work. Is there any solution to getting thread URL using Graph API or FQL?

vian
  • 811
  • 2
  • 12
  • 27

1 Answers1

8

If the id you have is a string object (probably a guid), this is from Facebook's older message system storage structure. Now they've updated to a new storage structure that requires the old ones to be migrated into the new

So you have a fairly easy check:

If thread id is a long (Int64/BigInt), then you have a new thread and can use http://www.facebook.com/messages/?action=read&tid=id.THREAD_ID

If thread id is a string then you have a older thread and can use

http://www.facebook.com/messages/?action=read&tid=THREAD_ID

many programming languages have their own form of checking the type of a value.

var threadId = (string)data.thread_id;
var longVal = 0L;    
var isLong = Int64.TryParse(threadId, out longVal);
var threadUrl = (isLong) ? 
  "http://www.facebook.com/messages/?action=read&tid=id." + threadId :
  "http://www.facebook.com/messages/?action=read&tid=" + threadId;
DMCS
  • 31,720
  • 14
  • 71
  • 104
  • 1
    It would be great if everything was this simple, but when requesting /me/inbox from graph api for the 27726d81656e4c07ae5654116cccb724 (how the id should be to properly open facebook thread page) thread I get a 64 bit "id": "1956133107397". And if I use the id from /me/inbox (the 64 bit one) to construct thread page url I get "No message found" error. I can't retrieve the longer hex id from facebook using Graph API. – vian Feb 04 '12 at 08:14
  • 1
    I don't know what you did to the values, but everything I tried always, always, always worked using the code I posted above. Of course you have to have a **valid USER access token with `read_mailbox` permissions**. See: https://developers.facebook.com/docs/reference/api/permissions – DMCS Feb 04 '12 at 09:39
  • I have a read_mailbox permission and I checked it working, but for some reason I can get the long hex string id only when I'm using /me/threads connection, when I use /me/inbox I get a nonworking id. Using /me/threads instead would be easier if it didn't require the user to be the developer of the application. – vian Feb 07 '12 at 09:36
  • I would file a bug with Facebook at http://developers.facebook.com/bugs and see if they will add a hyperlink property to the message/thread objects. Happy coding! – DMCS Feb 07 '12 at 14:14
  • 1
    @DMCS - Congratulations! 10K FTW ;) – Lix Feb 11 '12 at 16:55
  • 1
    Thanks :) Worked hard. Learned a tonne and had fun :D – DMCS Feb 11 '12 at 18:09
  • Learning - yes... but the [fun](http://a1.twimg.com/profile_images/712273317/17f39f3bc624b.png) is had at [meta](http://meta.stackoverflow.com/) :P – Lix Feb 11 '12 at 21:37
  • Can you clarify? You use C# SDK and you can get the string ids successfully. How do you form your request to /me/inbox? Can I do the same with raw JavaScript AJAX calls. – vian Feb 23 '12 at 05:08
  • Open Graph API for /me/inbox most often doesn't return a hex id for old system threads. It returns only an int value which can't be used. So the above code doesn't solve the problem unless you're lucky enough to get given the right id by Open Graph. – sobri Apr 19 '12 at 19:59
  • As you know Facebook constantly changes things. Are you saying facebook no longer allows: `http://www.facebook.com/messages/?action=read&tid=id.` ? – DMCS Apr 23 '12 at 17:23