Using Google Chat API with Ruby on Rails (Invalid request error)

77 views Asked by At

I am trying to implement Google Chat in my Ruby on Rails application but I have been getting an invalid request error (with no other details). Below is my service which is pretty basic as I just want to send a chat message to a user to start:

I am using both the google-api-client and googleauth gems as recommended.

require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'google/apis/chat_v1'

class GoogleChatService
  def initialize
    keyfile = 'config/chat-service-account-key.json'
    scope = 'https://www.googleapis.com/auth/chat.bot'
    credentials = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(keyfile),
      scope: scope
    )
    @chat =  Google::Apis::ChatV1::HangoutsChatService.new
    @chat.authorization = credentials
  end

  def send_message(email, message)
    msg = Google::Apis::ChatV1::Message.new(text: message)    
    begin
      @chat.create_space_message("users/#{email}", msg)
      puts "Direct message sent to #{user_email}: #{message}"
    rescue StandardError => e
      puts "Error sending direct message: #{e.message}"
    end
  end
end
0

There are 0 answers