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

Use CAI's mobile version endpoints #104

Closed
realcoloride opened this issue Oct 7, 2023 · 35 comments
Closed

Use CAI's mobile version endpoints #104

realcoloride opened this issue Oct 7, 2023 · 35 comments

Comments

@realcoloride
Copy link
Owner

Hello,

As you may have noticed, characterAI released their mobile app and I would like to know if it would be interesting to perhaps try to see if using their endpoints or if they have useragents or endpoints that could be used for easier automation.

@Parking-Master
Copy link
Contributor

I never thought of that, but that actually sounds like a good idea

@Parking-Master
Copy link
Contributor

How would you find those endpoints though?

@realcoloride
Copy link
Owner Author

How would you find those endpoints though?

Use an android emulator and a reverse proxy like Fiddler or WireShark

@Parking-Master
Copy link
Contributor

Yeah that's what I was thinking. An emulator like https://appetize.io and inspect HTTP requests

@Parking-Master
Copy link
Contributor

For free users a session doesn't last long so you'd need to install the app quick and sign in

@Parking-Master
Copy link
Contributor

Nevermind, you can't install apps on there for whatever reason

@catvinyl
Copy link

Character AI without Puppeteer
https://gist.github.com/catvinyl/f7fa537954265f0ac3b940a114745b04
You need to install ws.
And you need to put your data to test:

How to get WebSocket token and chat id?

  1. Create chat with character.
  2. Open DevTools.
  3. Reload webpage.
  4. Write something to Character AI.
  5. Find in Network tab URL /ws/ or wss://neo.character.ai/ws/
  6. Click on that URL and open tab Response/Messages.
  7. Copy chat_id
  8. Copy robot's author_id (optional).
  9. Open tab Cookies/Payload.
  10. Copy Token from HTTP_AUTHORIZATION

@Parking-Master
Copy link
Contributor

@catvinyl you're joking, right???

How the heck did you actually make an API without cloudflare catching you?

@realcoloride
Copy link
Owner Author

@catvinyl you're joking, right???

How the heck did you actually make an API without cloudflare catching you?

its only a matter of time till the staff catches up..

@Parking-Master
Copy link
Contributor

@realcoloride lol, true... I've got to test out this script

@realcoloride
Copy link
Owner Author

Alright, keep me updated!

@Parking-Master
Copy link
Contributor

Parking-Master commented Oct 11, 2023

Ok. It seems to work but I'm getting an error because I didn't provide the right chat_id. I inspected the /ws/ request in the Network tab of Firefox, but it's empty. There's no response data from the websocket in the console. Is there any other way to get the chat id?

@catvinyl
Copy link

catvinyl commented Oct 11, 2023

@Parking-Master you need send message to AI, then check out for GET request with status 101. You can also choose filter for WS requests. Check that you're using https://beta.character.ai/chat2?char=... but not https://beta.character.ai/chat?char=...

@catvinyl
Copy link

My implementation is simple, but it can be broken with async requests, so you need to use await, and it skips to the fully generated answer. You can get a streamed answer (but it needs to be implemented).
It is also possible to create rooms with WS.

{"command":"create_chat","request_id":"a","payload":{"chat":{"chat_id":"b","creator_id":"c","visibility":"VISIBILITY_PRIVATE","character_id":"d","type":"TYPE_ONE_ON_ONE"},"with_greeting":true}
{"command":"abort_generation","request_id":"x","origin_id":"y"}
{"command":"generate_turn_candidate","request_id":"a","payload":{"character_id":"b","turn_key":{"turn_id":"c","chat_id":"d"}},"origin_id":"e"}

It is possible that the WS API has more commands.

https://beta.character.ai/chat2?char=...
This uses WS and doesn't have image uploading or generation. So you need to use this to get IDs, tokens, etc.
Rating is done using POST.

https://beta.character.ai/chat?char=...
POST request.

@Parking-Master
Copy link
Contributor

@Parking-Master you need send message to AI, then check out for GET request with status 101. You can also choose filter for WS requests. Check that you're using https://beta.character.ai/chat2?char=... but not https://beta.character.ai/chat?char=...

Oh, I got it. That's why. I was only using the first chat

@Parking-Master
Copy link
Contributor

Yep, I'm still getting an empty response

@catvinyl
Copy link

@Parking-Master I upgraded the script. Now it can create chat. Try only putting in a token. You can also try "debug = true" to see what's wrong.

@Parking-Master
Copy link
Contributor

Nice! I'll try it when I get a chance. I was able to get the token pretty easily

@Parking-Master
Copy link
Contributor

@catvinyl it works awesome, and responds really fast! One thing you should add though is like what we currently have, createOrContinueChat, because I'd like to continue the current chat. Other than that, it works great

@catvinyl
Copy link

@Parking-Master you can continue chat. You need to replace

const chat = await characterAI.createChat(character_id);
const chat_id = getChatId(chat);

with

const chat_id = 'xxxxyyyy-bbbb-cccc-dddd-eeeeffff1111';

@Parking-Master
Copy link
Contributor

Yep, but the problem is, I don't know the chat id lol. Besides, even if I did, I want to continue the "original" chat (like "/chat?char=...") and I don't think that one has an id. I've tried inspecting multiple socket requests on the page on Chrome and Firefox and they both say it's empty. I don't know what else I can do.

@Parking-Master
Copy link
Contributor

I even asked the AI himself and he gave me a valid-looking chat_id but it's still invalid

@catvinyl
Copy link

catvinyl commented Oct 12, 2023

I don't think AI have information about chat_id. When you run script it says

Ready!
user_id (some numbers)
chat_id 9e5fe1a7-7878-42a5-a43e-e1421cefe027
...

Then you need put that to chat_id.
const chat_id = '9e5fe1a7-7878-42a5-a43e-e1421cefe027';

"?char=YntB_ZeqRq2l_aVf2gWDCZl4oBttQzDvhj9cXafWcF8" This is not chat_id.
That is character_id.

@Parking-Master
Copy link
Contributor

@catvinyl ok, so I did in fact find the chat ID on the page. It worked, but the stream stops every time the AI stutters, so I found a way to return the response soon as the AI is done talking. It works great, less resources, no puppeteer, and lightning fast! Thank you so much for making this small script. I've adapted it to my own needs.

@Mr-TIME-c
Copy link

@catvinyl ok, so I did in fact find the chat ID on the page. It worked, but the stream stops every time the AI stutters, so I found a way to return the response soon as the AI is done talking. It works great, less resources, no puppeteer, and lightning fast! Thank you so much for making this small script. I've adapted it to my own needs.

do you mind uploading the package, because rn with puppeteer it takes too long to get a response and mostly failed response

@Parking-Master
Copy link
Contributor

@Mr-TIME-c I don't mind uploading a package for this, but I don't want to take credit for realcoloride's work. I will upload a package soon but I will also try it help with this package to make it without puppeteer and make it faster.

@realcoloride
Copy link
Owner Author

Hello again, this looks good and could help a lot in the process.
I will try this when I'll be home.

However, I would like to probably make a way to use the old puppeteer method and this new one. But judging by how it looks like it works, this would require much more effort to convert and find all new endpoints

@realcoloride
Copy link
Owner Author

Could you just list them all from the current code base? (see chat and message and client)
I also need to be aware of how different API responses and so on

@Parking-Master
Copy link
Contributor

I've published my own package!

@parking-master/node_characterai

https://github.com/Parking-Master/characterai

@realcoloride
Copy link
Owner Author

Worth the look but looks very messy in my opinion.
I'll stick this as a reminder but I would still like to know if it is possible to scrap the mobile version of the endpoints.

@Parking-Master
Copy link
Contributor

@realcoloride okay, just a recommendation haha :)

I hope there's an easy way to get the mobile version endpoints though... have you experimented yet?

@realcoloride
Copy link
Owner Author

@realcoloride okay, just a recommendation haha :)

I hope there's an easy way to get the mobile version endpoints though... have you experimented yet?

I wish I could talk in-depth about my searches (mainly reverse engineering) of the mobile app but I fear of if it complies legally or with their ToS.
So for now I'll just do my searches, and even if i found some interesting things, I am not sure if I can legally show it here

@Parking-Master
Copy link
Contributor

Yep, I always do a deep search in the company's ToS before making unofficial APIs like this, because I don't want to get in trouble. I'll do some research too.

@Jeffgrey91
Copy link

If you got the mobile version endpoints make sure to get the new group chat endpoints as well as it uses a different and improved chat model that's not available through the browser.

@realcoloride
Copy link
Owner Author

The mobile endpoints use new the endpoints. See #180

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@Parking-Master @realcoloride @Mr-TIME-c @Jeffgrey91 @catvinyl and others