Hubot Adapter for Discourse

(Jeff Wong) #1

I built a Hubot adapter for Discourse.

This should work moderately well for communities that desire bot integrations. There are a few gotchas (see the readme) but for the most part it has been pretty incredible to tinker with so far.

Something my community desired was a dice roller, which I knew existed as a hubot plugin. While there have been other attempts at integrating dice rollers into discourse, if I was going to run a bot against my Discourse instance, I wanted the full power of hubot plugins at my disposal. My aim here was to have there be an easy way to integrate such reply features into a Discourse community using an existing and well known bot.

This adapter is about adding a bot that directly listens in to topics and replies in Discourse itself. It differs from a previous Hubot integration topic as that seemed to be aimed at querying a discourse community in an existing integration (irc, slack, etc) to get discourse stats, and possibly post to a remote Discourse instance.

The bot now polls for messages over Discourse’s message bus.

I created the repository as hubot-discourse-integration, as hubot-discourse was already taken… Sadly this also had no documentation, code, or announcements here. I wasn’t inclined to trust it, so I built this.

(This kind of thing might belong in #plugin:extras – if so please move!)

Create Own Bots?
Reply bot for Discourse?
(Sam Saffron) #2

You really should use the message bus here, its both faster and significantly less resource intensive.

(Jeff Wong) #3

Ah right, thanks for the tip! I’ll look into that today – I’ve been putting off learning more about the message bus in general, just to get things working.

(Jeff Wong) #4

Alright, I have practically torn apart your message-bus javascript client and adapted it for node and it’s working great! Polling is now done over message bus, so it should be a lot more responsive now! :thumbsup:

One more question - Is there any way for me to get the current user, given a user API key? It would be great if a 3rd party app could figure out their own username + id without having to configure it separately.

(Jan P.) #5

So, hey, hi, you sent me here in my topic asking for bots.

This bot can jump in if he is @ mentioned in a topic and then do *** magic *** which can then ouput in a post in that topic. Correct?

(Thank god for by the way: hubot-discourse-adapter/discourse.js at master · janpio/hubot-discourse-adapter · GitHub [my terrible JS is better than my non-existant Coffescript])

Some absolute newbie questions:

  • Can you show how you for example would configure it to do the dice rolling thing you mentioned above?
  • Is the adapter also able to receive/read all new topics being created? (or would I have to modify it to do this?)
  • Is there a diagram/flowchart/list online somewhere which methods of your adapter are called in which order when?

(Jeff Wong) #6

Hubot can be configured with both pre-written plug-in scrips, as well as ad-hoc scripts. You can get started with hubot pretty quickly over commandline for local barebones testing of responses. This adapter is only here to allow it to “speak” and respond over discourse.

I ended up splitting up my bot from my custom configuration, so that I could run tests against my custom scripts.

The dice roller can be seen here and I have an additional (and much simpler) “if you here anything about ‘the deals’ respond in kind” script for fun.

You can set it up that way - The adapter responds via discourse’s native notifications. Basically, if there’s a discourse notification of a message, the bot will see it. In the bot’s Discourse profile, you can edit and add to the watched categories to allow it to automatically see new topics/replies/etc without a direct @.

EG: for your use case, you would probably want to add your interested categories to “watching first post” and set the “when I post in a topic set that topic to” normal. That way, the bot gets notified on new topics, but does not continue to monitor/reply afterwards.

Not currently - It follows the same patterns from other hubot adapters - aside from setting up what hubot “hears” in discourse, you really shouldn’t have to mind much about the internals. Mostly you’d be interested in the API, and the difference between function hooks like robot.hear vs robot.respond. See the docs for examples!

(Jan P.) #7

Thanks for replying, will take a deeper look later.

While reading I got an idea: Can your adapter already send private messages instead of post to topics? “Moderating in private” might be an interesting concept to have to call out the people in public but nudge them to add the information in private.

(Jeff Wong) #8

Not currently; it was out of my use case, but it would be possible to extend/ modify how the bot replies to send private messages rather than replying inline.

(Jeff Wong) #9

Little bit of work on this tonight –

It’s admittedly a little bit of a custom workaround as hubot doesn’t currently have a concept of PM replying, so I’m relying on a “pm” attribute to be set in res.envelope to imply that we want the bot to respond privately.

Here’s an example:

robot.hear /private hello/i, (res) -> = true
    res.send "I will reply hello privately!"

The new PM is titled “About your post in {topic title}”, and has the first line as the URL to the topic in question.

If the bot is already hearing a message within a PM, it replies as normal.

It would be nice if Hubot had the concept of a whisper, but that’s unfortunately out of my hands at the moment.

I also added the ability to arbitrarily add messages to other topics, or trigger sending of messages by way of the robot.messageRoom action to be able to generate PMs or reply to arbitrary topics by topic number.

Hope this helps!