1

I need my bot (me) to login without myself involved in it. I've set up my node server, but now I can't figure out how to login. I'll need fkey to connect to socket, and for that I feel I'll need to login.

secondly, I don't know if I need to login only to stackexchange.com or I'll need to login each site individually if a chat room belongs to different SE.

For example, I've logged in with meta SE, and I'm chatting in The Friendly Cafe room, but then I want to connect to any other room which belongs to different SE site, will I need a different fkey and authentication to connect to socket?

Is this api and login process shared somewhere?

8
  • There is no API, but you could look at one of the libraries mentioned here: stackapps.com/q/4446/34061
    – Glorfindel
    Commented Jun 11, 2021 at 13:59
  • @Glorfindel I understand there's no official api, but I have access to chat and events and ws-auth apis. So I need something like that for login too. That will help. Isn't it possible?
    – Rosie
    Commented Jun 11, 2021 at 14:03
  • Yes, but either by reverse engineering or by copying from others who've done that. There's no step by step guide.
    – Glorfindel
    Commented Jun 11, 2021 at 14:05
  • @Glorfindel oh, so I can copy from libraries and see how it works?
    – Rosie
    Commented Jun 11, 2021 at 14:11
  • 1
    Depends on the license, but probably yes. At least it'll tell you the right URLs and parameters.
    – Glorfindel
    Commented Jun 11, 2021 at 14:16
  • @Glorfindel I checked the cabbage JS api but looks like it's very old. I'm getting page not found error in API response.
    – Rosie
    Commented Jun 11, 2021 at 18:30
  • In fact looks like all of them are outdated
    – Rosie
    Commented Jun 11, 2021 at 18:32
  • The Python ChatExchange library is in use by several bots, including Smoke Detector. The login logic is not exactly easily penetrable, but you might at least try to validate your findings once you understand what you are doing by comparing to the logic in there. github.com/Manishearth/ChatExchange/blob/master/chatexchange/…
    – tripleee
    Commented Jun 12, 2021 at 9:39

1 Answer 1

3

You can not. Because there is no API. You need to reverse engineer the calls a webbrowser would make when a normal user performs a log in. Once you've collected those calls you need to implement them in your favorite tool stack.

Here is what you need for reverse engineering:

Here is how you proceed (I assume you have a Stack Exchange (Username/password) Account)

  1. Open Firefox and the Developer Console.
  2. In the Network tab make sure Persist Logs is marked
  3. In the Network tab filter on HTML (and optional XHR)
  4. Execute below steps and verify each step in the network console.

(I use this kind of CSS rules notation to indicate what you need from the HTML: a[textContent='log in'].href means: find the <a> that has textContent of log in and then return the value of its href attribute. Do note that the use of textContent is not allowed in proper CSS. I abused it here to keep the notation concise)

action url + data Method Cookies Html / HTTP headers
visit https://chat.stackexchange.com/ GET - next = a[textContent='log in'].href
Click next GET - next = Location header
redirect next GET Prov next = form#login-form.action
fkey_val = input[name='fkey'].value
ssrc_val = input[name='ssrc'].value
cdl_val = input[name='cdl'].value
submit next
fkey=fkey_val&
ssrc=ssrc_val&
cdl=cdl_val&
email=your_email&
password=your_password
POST acct
fkey
uauth
next = a[textContent='Click here if you are not redirected'].href
click next GET - next = Location header
redirect next GET sechatusr (chat main page, logged in)

Implement above steps in your toolstack of choice. Components / classes / functions you minimal need:

And debugging skills.

Be aware of the Etiquette of Screen-scraping Stack Overflow? and pay attention to the User Agent header

8
  • Oops. It's indeed very difficult. I'll manually do login in this case whenever I want the bot to run, and use other apis using userscript for sending messages/sockets etc., as they are easy. Anyway, does chatbot do screen-scraping? How do I know if I'm screen-scraping? I mean I'm not storing any information from SE to my pc. I'm just reading the events and sending text messages. That's it. That doesn't mean screen-scraping right?
    – Rosie
    Commented Jun 12, 2021 at 7:08
  • You are scraping if you parse html and post with a script / code. So yes, any bot active on the SE chatservers does screen scraping. By definition if there is no formal API for interacting with a website, screen-scraping is all that is left if you automate interactions with such websites.
    – rene
    Commented Jun 12, 2021 at 7:10
  • oh. In that case I've lots of doubts how I can avoid being blocked/suspended. Should I leave comments on the question/answer you've mentioned? Or ask a new specific question?
    – Rosie
    Commented Jun 12, 2021 at 7:31
  • Just chill out. Don't make more then 90 calls per minute and pay respect to the Chat rate limit. And if you do get an error back from the SE servers, do not retry your call immediately but voluntarily back-off for a minute or so and increase that back-off time if the error remains. See also How many requests are too many?. We've all screwed up at least once. No permanent damage has been done.
    – rene
    Commented Jun 12, 2021 at 7:45
  • thanks :D Also I've written code to stop spam also. If any user try to spam with my bot, it will stop responding for some time. It's basic spam control but i'll improve it. I'll keep in mind to make very reasonable calls per minute, like a normal user. also, I don't instantly reply a chat message. It already takes a few seconds to respond.
    – Rosie
    Commented Jun 12, 2021 at 7:58
  • But still I would like to know what to add in User agent, any way for safety. I read the other answer but I didn't understand what they meant. Since it's a separate doubt, should I create a new question?
    – Rosie
    Commented Jun 12, 2021 at 7:59
  • 1
    Yeah, please do. I'll have an answer for that as well. In your new question please refer to questions/answers you've read / studied and what your understanding is and what you doubts about.
    – rene
    Commented Jun 12, 2021 at 8:04
  • 1
    @Rosie i've added examples of user agents to the FAQ post: stackapps.com/a/8965/18722
    – rene
    Commented Jun 12, 2021 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .