0

I'm creating a few simple Bot functions to create threads and send messages to Chat. Creating 'arbitrary' threads and inserting messages into threads with known ids is fine, no problem, I am simply not able to specify the thread id when creating a new one. I've followed details in the Google documentation but can't get any of them to work.

See: https://developers.google.com/hangouts/chat/how-tos/bots-develop

This is my code - very straightforward, but obfuscated

$("#fa-comment").click(function() {
    $.ajax({
        method: 'POST',
        url: 'https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY_ID&token=TOKEN_ID',
        dataType: "json",
        contentType: "application/json; charset=UTF-8",
        //data: "{'text':'This creates a new thread and inserts message - thread id is not known'}",
        //data: "{'text':'This inserts into a current thread with known id', 'thread':{'name':'spaces/SPACE_ID/threads/THREAD_ID'}}",
        success: function () {
            $('.response').html('Success');
        }
    });
})

Both of the commented out data: lines work perfectly.

I have tried:

  • data: "{'text':'This inserts into a new thread with specified id', 'thread':{'name':'spaces/SPACE_ID/threads/SPECIFIED_THREAD_ID'}}", but that results in a 500 error
  • https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?\ threadKey=ARBITRARY_STRING as written in the documentation but no variation works - get 400 errors and violations/unknown variable 'threadkey'

I've followed every which way I can from the documentation and trying variations but to no avail.

There is undoubtedly something I have missed here but I can't see it. Any help for this would enable my hair to grow again!

3 Answers3

3

I believe you may have already solved this but anyway...

The query param should be thread_key instead of threadKey. So, in your case, it would be:

url: 'https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY_ID&token=TOKEN_ID&thread_key=THREAD_KEY'

Hope this is useful for someone in the future :).

  • This may be old since official docs say that it is 'threadKey'. https://developers.google.com/chat/api/reference/rest/v1/spaces.messages/create#query-parameters. Any future viewers, see my answer below for up-to-date method. – Onur Çevik Mar 18 '22 at 12:57
  • Yea, but back then it also said it was supposed to be 'threadKey' and it wasn't. Hopefully they fixed it :D – Rodrigo De Rosa Apr 26 '22 at 11:23
0

In Google Chat, 'creating a thread' is not different than 'sending a message to a thread'. So, you can simply send any message you want following the basic tutorial, and just add 'threadKey=my_thread_key' as a query string to the webhook URL. If my_thread_key does not exist, Google Chat will simply create a new thread with that key.

Then, use the same query string with 'my_thread_key' value on any other webhook to send a 'reply' to the existing thread.

To sum up, same flow for creating and replying threads. If not exists, it will be created. If exists, it will be replied to.

A simple request can be seen below. Note that all values except MY_THREAD_KEY in the 'url' should be provided to you when you generate the webhook for your space.

axios({
  method: "post",
  url: `https://chat.googleapis.com/v1/spaces/<MY_SPACE_KEY>/messages?threadKey=<MY_THREAD_KEY>&key=<MY_KEY>&token=<MY_TOKEN>`,
  data: {
    text: `Create/reply to thread`,
  },
});

Check Google Chat for Developers docs that explains how 'threadKey' works:

https://developers.google.com/chat/api/reference/rest/v1/spaces.messages/create#query-parameters

Onur Çevik
  • 1,560
  • 13
  • 21
0

I have just spent the morning with the same problem and it is badly documented.

The threadKey header works as documented but only if you specify the messageReplyOption header as REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD.

I've modified your code slightly, and it should work.

$("#fa-comment").click(function() {
  $.ajax({
    method: 'POST',
    url: 'https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY_ID&token=TOKEN_ID&messageReplyOption= REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
    dataType: "json",
    contentType: "application/json; charset=UTF-8",
    data: "{'text':'This replies to an existing thread with the same threadKey, or starts a new thread if it doesn't exist', 'thread':{'threadKey':'myThreadKey'}}",
    success: function() {
      $('.response').html('Success');
    }
  });
})

Also note that the threadKey header is now deprecated (but still works). But it is now preferred to specify the threadKey in the request body (which is what I did in the example I provided.

Hope this helps.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31