JavaChat / OakBot

A Java-based chat bot for Stackoverflow Chat
45 stars 32 forks source link

Support for StackExchange #31

Open cereda opened 8 years ago

cereda commented 8 years ago

Hi there, congrats for the cool project! :wink:

Is there any plan for supporting StackExchange-based chatrooms? I did a couple of tests and it looks doable: although I could not make the bot successfully login, it intercepts messages and parses them accordingly, so it seems we are almost there. :wink:

Just a quick info: maybe the fkey regex should be redone as the format looks quite different in SE sites (as it incorporates arbitrary length and hyphens). An alternative solution is to rely on jsoup for getting data from HTML sources.

Keep up the good work! :cake:

All the best,

Paulo

mangstadt commented 8 years ago

Hi Paulo,

If the format of the fkey is the only difference, it sounds like supporting StackExchange would be relatively straightforward. Can you tell me how the fkey looks for a StackExchange chat room?

Thank you, Mike

cereda commented 8 years ago

Hi Mike!

Sadly, I could not spend much time in the code, as I have a thesis to write (ouch). :wink: To the extent of my checking, the StackExchange fkey seems to keep the usual 32 hexadecimal digits with the addition of four hyphens acting as separators. I suspect the regex could be expressed along the lines of value=\"([0-9a-f\-]{36})\" (untested).

Still, the login part looks a bit tricky, as it is done via StackExchange OpenID (and the site sometimes redirects connections). I was looking at ChatExchange for some hints, and it seems the OpenID part requires some tweaking indeed. But, apart from this nuisance, the port looks potentially doable (I am trying to be cautious on saying strong statements :wink: ).

I offer my help to tackle the login part, although my time is quite restricted nowadays. And please, feel free to ignore this request if, at the end of the day, tweaking the code would cause a lot of headaches. :wink:

All the best!

Paulo

mangstadt commented 8 years ago

Thank you for the information, Paulo. It sounds like it's not as simple as originally thought. :( However, this would surely be a nice feature to add. Maybe I will work on it later. Good luck with your thesis!

LunarWatcher commented 7 years ago

Repoking this issue: I have added support for Stack Exchange in my bot (based on OakBot). Simply changing the domain is enough. There are however some pitfalls to be aware of:

Those should be fairly self-explanatory. Once you change the site, user ID's change. Room ID's too. Passing the domain allows you to get the appropriate property from bot.properties (for an instance so_admins and mse_admins. Check the domain, set admins to the right one). Passing domain to Rooms (class) you can append that to the name (for an instance stackoverflow.com-rooms, meta.stackoverflow.com-rooms) which saves the appropriate room IDs to the appropriate database entry.

I'm still working on implementing this, primarily connected with meta stackexchange but it appears to be universal. any *.stackexchange.com has chat at chat.stackexchange.com. Custom domains (stackoverflow.com, superuser.com, etc) have at their own domains. Checked the fkey regex, and looked for the fkey in Shadows Den (on meta stackexchange chat) and it matched. 32 hex.

As for login, changing the domain in StackOverflowWSEvents (added a second constructor, domain defaults to stackoverflow if not defined) and nothing else allows login.

It is primarily admin ID's, banned ID's and bot ID that causes issues. Still working on implementing fixes in my bot

mangstadt commented 7 years ago

Cool, thanks for the additional info. :)

cereda commented 5 years ago

Hi friends!

Sorry for the huge hiatus (note to self: PhD takes more time than expected). Any updates on this? I tried the code a couple of minutes ago and I failed to make it work on a StackExchange room. Perhaps @LunarWatcher could give me some hints. :wink:

All the best!

mangstadt commented 5 years ago

No, sorry. :( Haven't worked much on Stack Exchange compatibility. Also: ducks!

cereda commented 5 years ago

Hi Mike!

No worries, pal. :wink:

Duck

Quack! :wink:

LunarWatcher commented 5 years ago

Just as a heads-up, SE openID is deprecated, which can result in problems with login to SE later. SO login, as originally done in the bot, is fortunately not affected. But login into SE chat still uses OpenID. Meta.SE login is virtually the same as into SO, but SE is still complicated because it uses different endpoints.

First you POST to https://stackexchange.com/users/login#log-in, and get a URL from the body. You use that URL to get the fkey, which you then POST along with username and password to https://openid.stackexchange.com/affiliate/form/login/submit, in addition to affId set to 11. Finally, using that response, you create a final response using a URL in some JS code called var target. GET that, no arguments used here, and then check the response for the status on the login.

cereda commented 5 years ago

Hi @LunarWatcher! Thank you very much for the heads up, I will try to delve into the code. :wink:

Quack! :duck:

mangstadt commented 5 years ago

@LunarWatcher Do you have any code you could post for the SE login? Or know where I can find some? I spent about an hour trying to get something to work, with no luck.

The hard part is that the SE login page uses JavaScript to submit the credentials. The login URL that you posted wouldn't quite work with the bot at face value because the anchor ("#log-in") causes some JavaScript to run in the browser.

mangstadt commented 5 years ago

Never mind. I think I figured it out! I was trying to use Firefox's F12 developer menu, but it was clearing the list of network requests on every click, so I couldn't see the URLs the browser was sending requests to. Edge actually worked better. 😵

  1. Send GET request to https://stackexchange.com/users/signin.
  2. A plain-text URL will be returned. Send a GET request to this url.
  3. An HTML page containing a login form will be returned. Extract the form URL, affId, and fkey from this form.
  4. Send a POST request to the form's URL. Include the following parameters: email, password, affId, fkey.
  5. An HTML page will be returned. If the login credentials are correct, the page will contain a JavaScript "target" variable, which is set to a URL. Extract this URL.
  6. Send a GET request to the above URL.
  7. You are now logged in,
mangstadt commented 5 years ago

Ok, I think I got it working! Thanks for your tips @LunarWatcher!

Let me know how it works if you try it out! 🎉

cereda commented 5 years ago

Brilliant, friends! It's working like a charm! Time to revive our beloved Psmith! All the best!

cereda commented 5 years ago

Friends, sorry to bump this issue, but I do believe recent changes in SE broke the support. I will try to take a closer look during this week and report back...

Happy Easter! :wink:

mangstadt commented 5 years ago

Ugh, just great. 😕 Thanks for letting us know! 😀

cereda commented 5 years ago

Deeply sorry @mangstadt... :cry: