# Common event system for chatrooms - a specification

**URL:** <https://meta.discourse.org/t/common-event-system-for-chatrooms-a-specification/59245>\
**Category:** Feature\
**Created:** [16 במרץ,‏ 2017,‏ 1:01pm UTC](https://meta.discourse.org/t/common-event-system-for-chatrooms-a-specification/59245 "2017-03-16T13:01:04Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [16 במרץ,‏ 2017,‏ 1:01pm UTC](https://meta.discourse.org/t/common-event-system-for-chatrooms-a-specification/59245/1 "2017-03-16T13:01:05Z")

</div>

One of the GSOC ideas on GitHub is creating a “[Common Event System for Chatrooms](https://github.com/discourse/discourse/wiki/Summer-of-Code-2017-Ideas-List#common-event-system-for-chatrooms)”

> [@](#):
>
> Ideally we’d have a set of plugins for various chatrooms, like discourse-hipchat, discourse-slack, etc, that would all expose the same event system.
> 
> Then we could do code like DiscourseEvent.trigger(:notify\_chat, “some chatroom text”) and have it work regardless of your chat software.

I like the sound of this idea, so this is an attempt at a more comprehensive description of how it could be implemented. Hopefully this will give a starting point for some discussion about what people would like to get out of a feature like this.

* * *
Edit 17/03/2017: I've added and ~~removed~~ some stuff based on comments below.
* * *

# What we already have

[https://meta.discourse.org/t/the-official-discourse-slack-plugin/51412](https://meta.discourse.org/t/the-official-discourse-slack-plugin/51412)

The Discourse-Slack plugin is a great way of getting real-time notifications of Discourse posts. It can post to ‘group chats’ in Slack, or post to individuals. Notifications can be setup for specific categories (tags [coming soon](https://github.com/discourse/discourse-slack-official/pull/15)), either for “new topics”, or “all replies”.

# Common event system

### The idea

Creating plugins which provide similar functionality to the slack plugin, but for other communication platforms, is totally possible right now. However, this will result in a massive amount of code duplication, as a lot of the logic will be the same between different chat systems. Therefore a ‘shared’ chat system (as a plugin), would be an efficient & maintainable way to expand to multiple platforms.

### Scope

As an initial implementation, I wouldn’t expect functionality to go much further than the current functionality of the Slack plugin, however there is scope to add more functionality such as actually making posts on Discourse from your chosen messaging platform, but that’s material for a future discussion.

### Structure

The “Common event system” would take care of managing “subscriptions” (which categories/tags to subscribe to for a given “chat”). At its simplest level, it would maintain a table with rows like this. Yes, it would need to be more complicated than 3 text fields, but hopefully this illustrates the functionality.

```plaintext
| CHAT_SYSTEM | CHAT_ID | SUBSCRIPTIONS |
|-------------|-----------------|---------------------------------|
| telegram | @davidtaylorhq | c/meta,c/plugins,t/pr-welcome |
| facebook | group_75462363 | c/lounge |
| slack | #general | c/support, c/bugs |

```

Each of the chat\_systems would be an independent plugin, which deals with the platform-specific stuff. These plugins would receive information from the “common event system” using similar systems as the current event hooks. So you’d end up with something like:

```ruby
DiscourseEvent.on(:notify_chat, :telegram) do |information|
    # Send a message to the specified telegram chat using the information provided
end

```

### Permissions

Initially this would be admin-only, so no permission checks would be necessary

### Authenticating

Initially this would be admin-only, so no authentication would be necessary

### User Interface

The “common event system” should have its own pane in the admin interface, listing all current subscriptions (effectively each of the rows in the pseudo-table above). I imagine the UI to be very similar to the current slack plugin:

[![](https://global.discourse-cdn.com/meta/optimized/3X/0/9/090d7887804ce156c022163d9fc9cdfe3be6511a_1_690x238.png) ](https://global.discourse-cdn.com/meta/optimized/3X/0/9/090d7887804ce156c022163d9fc9cdfe3be6511a_1_690x238.png)

Providers could also implement something like “slash commands” for changing subscription settings, like the slack plugin does right now:

```plaintext
/discourse [watch|follow|mute|help|status] [category|all]

```

## Possible providers

Each of these could be implemented in separate plugins, keeping things modular & maintainable:

**Slack** - the slack plugin already exists, making use of the [api](https://api.slack.com/web)  
**Telegram** - Mature REST [bot API](https://core.telegram.org/bots/api), which I have used in the past.  
**Facebook Messenger** - [Bot API](https://developers.facebook.com/docs/messenger-platform) available  
**Gitter** - Can add things to ‘activity stream’ using the [services library](https://github.com/gitterHQ/services). @jafeth.diazc has more info [here](https://meta.discourse.org/t/gitter-post-notifications-to-chat-post-transcripts-to-forum/51247/2)  
**HipChat** [(thread)](https://meta.discourse.org/t/hipchat-post-notifications-to-chat-post-transcripts-to-forum/51248/1) - [API available](https://www.hipchat.com/docs/apiv2), see [discourse-akismet-hipchat](https://github.com/discourse/discourse-akismet-hipchat)  
**Mattermost** [(thread)](https://meta.discourse.org/t/mattermost-post-notifications-to-chat-post-transcripts-to-forum/51246) - [API available](https://docs.mattermost.com/developer/api.html)

**[others]** - Hopefully having a central system for managing the complicated user/permissions logic would enable plugin developers to easily create plugins for any service with an API.

* * *
I'd love to hear people's thoughts on this spec, if there's anything that's not clear please let me know and I'll do my best to improve the description.

---

_[View the full topic](https://meta.discourse.org/t/common-event-system-for-chatrooms-a-specification/59245)._
