Integrating Discourse with Facebook (Notifications)

(Pekka Gaiser) #1

I’m thinking about suggesting migrating a University department’s forums from Facebook Groups to a Discourse install.

However, the users will be mostly kids who live exclusively on Facebook. Super tight integration with FB would be a prerequisite. It would be cool to be able to get a new item in your FB stream upon new posts in specific forum tags, but the absolute minimum is getting notified in the FB inbox when you are mentioned or responded to in the Discourse forum.

I know woefully little about the Facebook API. Is this even possible?

  • Is this something that can currently be done using Discourse and maybe a bit of custom programming to talk to the FB API?

  • Does the Discourse team have plans to implement anything like this in the core?

  • If not, is this worth a Plugin request?

(Jeff Atwood) #2

This is very unlikely to happen in core, we mostly use email as a notification point (also works for reply-by-email), and have plans to greatly enhance email functionality.

You can of course log in with Facebook quite easily right now today, and someone could write a plugin that notifies of new posts on Facebook somewhere.

(Sam Saffron) #3

Technically you could write a plugin to achieve this kind of stuff, it would require some work and possibly required adding some hooks in core (which we are open to)

Clearly to get this level of integration the users would need to “approve” these actions (write in my stream, etc) upon signup. This will probably require adding a hook to request the permissions.

(Stefano Bernardi) #4

Hey @pekka, I’m in the process of moving a 15k people group to discourse from FB, and a tight integration is a must for me as well.

I’ll most likely work on an integration tomorrow, starting with an autoposting by a bot account of every new post.

I unfortunately haven’t understood too much how to develop plugins, so I’ll most probably just edit the app itself and then ask for help in extracting it.

(Pekka Gaiser) #5

That sounds great! I’d be very interested in how this works out for you.

(Roberto_Pezzali) #6


Do you find also a way to route notifications?

I’m waiting for notification api!

(Stefano Bernardi) #7

I actually understood how to develop plugins so it’ll be one of those.

Will also work on giving the ability to users to add FB as a connection if they signed up with a username or twitter.

(Stefano Bernardi) #8

@robypez what do you mean? In FB or in discourse?

For FB, it should be simple to have notifications, but not directly.

If you autopost to a group and a user has notifs enabled then that serves the purpose.

Having them user specific for likes and mentions is more complicated and outside the scope of what I’ll be working on for now.

(Stefano Bernardi) #9

Ok, this is a tiny bit more complicated then I imagined to ship as a plugin.

I need to alter the schema to save Facebook’s access token which is currently discarded.

@sam would you be open to a PR where I add a “token” field to FacebookUserInfo? Or do you want to add it yourself? Otherwise I’ll just be committing to my own repo and won’t be able to release as a plugin

I’ll also need another gem to then post to FB, is there any way for a plugin to add a gem to the Gemfile?

This is the only change other then the schema (which I can keep in the plugin):

class Auth::FacebookAuthenticator
  def register_middleware(omniauth)
        omniauth.provider :facebook,
         :setup => lambda { |env|
            strategy = env["omniauth.strategy"]
            strategy.options[:client_id] = SiteSetting.facebook_app_id
            strategy.options[:client_secret] = SiteSetting.facebook_app_secret
         :scope => "email,publish_actions" #addedd the publish_actions scope


  def parse_auth_token(auth_token)

    raw_info = auth_token["extra"]["raw_info"]
    email = auth_token["info"][:email]
    access_token = auth_token["credentials"][:token] #this is new

      facebook: {
        facebook_user_id: auth_token["uid"],
        link: raw_info["link"],
        username: raw_info["username"],
        first_name: raw_info["first_name"],
        last_name: raw_info["last_name"],
        email: email,
        gender: raw_info["gender"],
        name: raw_info["name"],
        token: access_token #this is new
      email: email,
      email_valid: true


(Martin) #10

When creating a Facebook account, they create a Facebook email for you I believe (or at least they did). So when connecting to a Discourse forum using Facebook maybe it could pull that email address and send the notifications to the inbox there? I believe they set up the Facebook email in order to get people to use it as an email box.

Don’t know if it could work but it’s just a thought.

(Sam Saffron) #11

Sure, this is fine, don’t mind if core is extended to allow for richer plugins.

(Stefano Bernardi) #12

Ok, thanks. What about the Gemfile?

(Stefano Bernardi) #13

BTW, I don’t seem to understand how git works, so I’m not able to send a pull request just for one commit and it seems to put in there all my past changes to the core.

So, if I find someone who can help me out understanding how to do this I’ll be able to commit… otherwise, I’m really sorry.

(Roberto_Pezzali) #14

I’m looking for notification api: I must send external notification to discourse notification bar and I must also send Discourse notification to a notification zone in my main app

(Sam Saffron) #15

you can add gem dependencies to plugins, that is the way to go, don’t want to make our Gemfile any fatter.

(Stefano Bernardi) #16

I have this working. I unfortunately had to hardcode the bot access token as I can’t read ENV thanks to Nginx and can’t access discourse.conf, not sure why. I’ll have to go with some YAML stuff, tomorrow.

How it works for now is that if we have an access_token stored for a user it’ll post as that user. If we don’t have it, it’ll post as the bot.

Still have to test a lot and extract into a plugin, which might be tricky.

(Kane York) #17

You actually can access discourse.conf from a plugin now, it seems!

(Aj Koft ModifyWordpressCourse) #18

Hi, I have the same issue on migrating people from facebook platform into discourse.

Hope for the plugin very soon.


(Greg Swallow) #19

@stefanobernardi @sam - I just made the pull request Stefano suggested - Update facebook_authenticator.rb by rl7greg · Pull Request #2406 · discourse/discourse · GitHub

I’m interested in better integration with Facebook too, and have the Facebook profile pic as your avater, etc as I described here - Google Avatar - not associating? and here - Why use Discourse over Linkedin, Google, or Facebook Groups?

(Sam Saffron) #20

I am fine with storing more information, but the PR needs to pass all tests, and have a test for the condition :slight_smile: