Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem to list convo and send message #2775

Closed
Cristuker opened this issue Sep 2, 2024 · 4 comments
Closed

Problem to list convo and send message #2775

Cristuker opened this issue Sep 2, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@Cristuker
Copy link

Describe the bug

Well Im trying to create a bot who send message on DM to save posts. I tried using HTTP request like the below code and I receive 404 always following the doc and also I tried using the package for typescript but I received this error XRPCError: XRPC Not Supported #1954
but this solution don't work for me.

To Reproduce

Steps to reproduce the behavior:

  1. Create a node.js project
  2. add the following code
 const config = {
    method: "get",
    maxBodyLength: Infinity,
    url: `https://public.api.bsky.app/xrpc/chat.bsky.convo.getConvoForMembers?members=${did}`,
    headers: {
      Accept: "application/json",
      Authorization: `Bearer ${token}`,
    },
  };

  const response = await axios(config);
  console.log(JSON.stringify(response.data));
  return { data: response.data };

the other approach


  await agent.login({
    identifier: ultraConfidentialUser,
    password: ultraConfidentialPassword
  })
  const res = await agent.chat.bsky.convo.getConvoForMembers({
    members: [did]
  })
  console.log(res)

Expected behavior

list convos for the did that I send
Details

  • Operating system: linux
  • Node version: 18
@Cristuker Cristuker added the bug Something isn't working label Sep 2, 2024
@eduwr
Copy link

eduwr commented Sep 3, 2024

I was having the same issue.

the problem is that you need to use service proxy to forward the request to the chat service. To do this, you need to add the following header:

      "Atproto-Proxy": "did:web:api.bsky.chat#bsky_chat",

However, in order to use the service proxy, you cannot send the request to the PDS Entryway service (i.e. https://bsky.social). Instead, you must send the request to your own PDS, which acts as the home for your account. You can obtain the PDS when you create your account or by creating a session.

Here is my implementation:

import axios from "axios";

const SOCIAL_BASE_URL = "https://bsky.social/xrpc";
export const blueSkySocialAPI = axios.create({ baseURL: SOCIAL_BASE_URL });

export type Session = {
  did: string;
  accessJwt: string;
  refreshJwt: string;
  service: {
    id: string;
    type: string;
    serviceEndpoint: string;
  }[];
};

export const createSession = async (): Promise<Session> => {
  const { data } = await blueSkySocialAPI.post(
    "com.atproto.server.createSession",
    {
      identifier: process.env.BLUESKY_USERNAME,
      password: process.env.BLUESKY_APP_PASSWORD,
    },
    {
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
      },
    },
  );

  blueSkySocialAPI.defaults.headers.common["Authorization"] =
    `Bearer ${data.accessJwt}`;

  return {
    did: data.did,
    accessJwt: data.accessJwt,
    refreshJwt: data.refreshJwt,
    service: data.didDoc.service,
  };
};

export const listConvos = async (accountPDS: string) => {
  const url = "chat.bsky.convo.listConvos";

  const resp = await blueSkySocialAPI.get(url, {
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
      "Atproto-Proxy": "did:web:api.bsky.chat#bsky_chat",
    },
    baseURL: `${accountPDS}/xrpc`,
  });
  return resp.data;
};

const session = await createSession();
const convos = await listConvos(session.service[0].serviceEndpoint);
console.log(convos);

I'm not sure if this is the most straightforward way, but it works.

@Cristuker
Copy link
Author

I'm sorry but my implementation was wrong

@Lermatroid
Copy link

@Cristuker how was your implementation wrong? I am running into a similar issue.

@Cristuker
Copy link
Author

@Lermatroid I don't remember exactly what I refactored, but this is the link to the file. I did many refactors after this post but if you search into the commits it can help you https://github.com/Cristuker/save-to-later/blob/main/src/sendMessage.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants