junhoyeo / threads-api

Unofficial, Reverse-Engineered Node.js/TypeScript client for Meta's Threads. Web UI Included.
https://threads.junho.io
MIT License
1.58k stars 134 forks source link

possible to get ThreadID? #179

Open Aerglonus opened 11 months ago

Aerglonus commented 11 months ago

In #149 it was added the possibility to add parentPost to the publish method by using getPostIDfromURL and getPostIDfromThreadID either the URL or ThreadID.

Like this:

# Passing ThreadID
const parentID = 'Cuvp-XTuami'
const postID = await threadsAPI.getPostIDfromThreadID(parentID) 

# Passing URL
const parentURL = 'https://www.threads.net/t/CuspS2Qrb76/'
const parentPostID = threadsAPI.getPostIDfromURL(parentURL);

When making multiple posts like this(image below) what they do is just a main post and the rest are replies. (Ex: Thread-1 is the main for Thread-2 and its added as a reply to Thread-1, and Thread-3 is a reply to Thread-2)

looking at the requests the app makes it just 3 different POST requests for each thread.

I don't know if I'm misunderstanding how to use the getPostIDfromThreadID because how can I get the threadID from a main post to make a reply to it without hardcoding the threadID, because the publish method it only returns media.id

https://github.com/junhoyeo/threads-api/blob/6fddd56a6def97bf43d07c4e04ad65aac7f11718/threads-api/src/threads-api.ts#L1017-L1020

And looking at the response of the POST request to threads the media.code would be the threadID , if that value was accessible it could make the getPostIDfromThreadID easier to work with or is this not possible due to limitations when making the POST request?

Extra: even the media.caption.text could be useful for someone.

junhoyeo commented 11 months ago

From #166, we're retrieving the threadID by decoding postID with base64 and alphabet. I think we can encode it back.

sooluh commented 11 months ago

Hi, may I join in? I simply do it this way:

Split it with _ and then take the 0th index, because what the publish function returns is POSTID_USERID. So, when we want to publish with a parentPostID, just separate them and take the first index. I hope this helps!

You can see the implementation code here.

Aerglonus commented 11 months ago

Maybe this approach within the publish method response could work?

export type PublishResult = {
  postID: string;
  threadID: string;
  captionText: string;
  userID: string;
};
publish = async (rawOptions: ThreadsAPIPublishOptions | string): Promise<PublishResult | undefined> => {
       // ....
      // rest of the code
    // .....
    if (res.data?.status === 'ok') {
      const { id: mediaId, code: threadId, caption: { text: captionText, user_id: userID } = { text: '', user_id: '' } } = res.data.media ?? {};
      const postId = mediaId?.split('_')[0];

      if (postId && threadId) {
        const result: PublishResult = {
          postID: postId,
          threadID: threadId,
          userID: userID,
          captionText: captionText || '',
        };
        return result;
      }
    }
    return undefined;
}

Response

Response: [
  {
    postID: '3148673758193179125',
    threadID: 'CuyVhnGstn1',
    userID: 60634900825,
    captionText: 'Hello World!'
  }
]

Allowing user to access the required values after executing publish

 const response = await threadsAPI.publish({
      text: ' Hello World!? ',
    })
const threadID = response.threadID
//Response: CuyVhnGstn1

If this is ok i can make the PR