Last Updated: September 29, 2021
·
4.132K
· blazeeboy

Posting to facebook groups all at once using ruby

i create a script everyday and i post this script to twitter, facebook profile and groups so i'm trying to automate this process with some scripts, and this is my first script to post to certain facebook groups

#!/usr/bin/env ruby
require 'koala' # gem install koala --no-ri --no-rdoc

# create a facebook app and get access token from here
# https://developers.facebook.com/tools/explorer
# select "groups", "photos" when authenticating
oauth_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
group_filtering_words = ['ruby']
image_path = 'image.png' #change to your image path
message = 'My Cool image.' # your message

graph = Koala::Facebook::API.new(oauth_access_token)

# getting groups of interest
groups = graph.get_connections("me", "groups").select do |group|
    group_filtering_words.any? do |word|
        group["name"].downcase.include? word
    end
end

index = 0
groups.each do |group|
    index += 1
    puts "[#{index}/#{groups.size}] Posting to group #{group["name"]}."
    graph.put_picture(
        image_path, 
        {:message => message}, 
        group['id']
    )
end

4 Responses
Add your response

hi, is there anyway to post the message with hyperlink inside to the Facebook group with Koala and facebook's graph API?

over 1 year ago ·

yes of course there is a way, and koala has some good examples in it's wiki and README file

over 1 year ago ·

thanks for reply. But I really can't find much of posting message to facebook through Koala. Could you give me a hint where could I find it? I already exhausted 3 days on searching related topics but it is very limited documents I could find.

over 1 year ago ·

I finally figured out how to post the message to FACEBOOK. It needs to get usergroups & publishaction permission for the accesstoken first. I missed them in the beginning when I tried to use putwallpost method. But if I use putpicture, it could work without such permissions.
So the conclusion is if anyone wants to post to facebook Group through Koala with Facebook Graph API, he needs request the accesstoken with usergroups and publish_action first.

over 1 year ago ·