NicoM
NicoM

Reputation: 619

Collecting information from API callback with Google Apps Script

I am trying to get a Groupme (groupme is a group chat app) bot to work using Google Apps Script but I am having trouble figuring out what I'm missing in order to collect the JSON data from the callback.

I went through the Groupme Bot Documentation set up my bot properly and my bot can post to the group but I can't pull data from messages that people post.

Here is my code:

function doGet(groupMeData) {
  var incoming = JSON.parse(groupMeData)

  if(incoming.text == "bot"){
    var url = 'https://api.groupme.com/v3/bots/post'
    var payload = {
      "bot_id" : "abc123_Bot_ID",
      "text" : "hello_world " + incoming.id
    }
    var options = {
      "method" : "post",
      "payload" : payload
    }
UrlFetchApp.fetch(url,options);
Logger.log(incoming);
  };
};

I can post to the groupme group when I run the function but can't get it working where if anyone posts a message with 'bot' then it responds with 'hello_world' and that users ID. I've read about ContentService in Google Apps Script and feel like it should be involved but don't know where it fits in.

The response would look like this:

{
  "attachments": [],
  "avatar_url": "http://i.groupme.com/123456789",
  "created_at": 1302623328,
  "group_id": "1234567890",
  "id": "1234567890",
  "name": "John",
  "sender_id": "12345",
  "sender_type": "user",
  "source_guid": "GUID",
  "system": false,
  "text": "Hello world ☃☃",
  "user_id": "1234567890"
}

Upvotes: 0

Views: 939

Answers (1)

sjsyrek
sjsyrek

Reputation: 193

What does your client side code look like? Are you doing something like this:

// code to collect data from user, maybe JQuery
function getJSON() {
  google.script.run.withSuccessHandler(clientCallBack)
    .withFailureHandler(clientFailureFunction)
    .doGet(groupMeData);
}

function clientCallBack(content) {
  // do something with response
}

function clientFailureFunction() {
  // report error
}

On the server side, you probably do need to use Content Service. Try wrapping your JSON up in a TextOutput object:

var response = UrlFetchApp.fetch(url, options);
var content = ContentService.createTextOutput(response)
  .setMimeType("JSON");
return content;

If that doesn't work, post more details of what you're trying to do. Google Apps Script documentation requires a rabbinical level of interpretive skill sometimes.

Upvotes: 1

Related Questions