2

I am trying to send a post message to the google chat API using webhooks in the same thread when the message is about the same file being processed. Right now the messages are being posted but not in the same thread. Any help will be appreciated.

public async Task ExecutionStarted(string fileName, string filePath)
        {
            var today = DateTime.Now.ToString("yyyyMMddTHH:mm:ss");
            string json = "{\"text\":\"" + today + ": Processing file " + fileName + " from " + filePath + "\"}";
            await PostToGoogleChat(json);
        }

public async Task PostToGoogleChat(string json)
        {
            HttpClient client = new HttpClient();
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var result = await client.PostAsync(new Uri(_chatUrl), content);
        }
MJ9094
  • 123
  • 3
  • 11

4 Answers4

1

In order to respond to a Thread, you will have to include the thread identifier in the URL of the call. The thread identifier is part of the parent parameter (see spaces.message.create).

When you get the message from the webhook (see: message resource, you can get the thread it was posted from the thread property that it holds). Afterwards, when creating the message you just have to send this exact thread's name (which is its identifier, should look something like spaces/AAAAMpdlehY/threads/UMxbHmzDlr4) as the message parent.

carlesgg97
  • 4,184
  • 1
  • 8
  • 24
1

According to this docs, just add the threadKey to the query parameter

https://developers.google.com/hangouts/chat/reference/rest/v1/spaces.messages/create

Same threadKey will go to same thread, no need to remember the thread ID from previous post.

Your request url will look like https://chat.googleapis.com/v1/spaces/AAAAL0vuKXY/messages?key=...&token=...&threadKey=...

dttung1412
  • 93
  • 1
  • 2
  • 8
1

Any Post message response will contain in the response payload.

"thread": {
    "name": "spaces/AAAASchux4w/threads/GgWXNtFDOpE"
}

What you need to do is include this in any future message that one wants to be sent in the same thread.

{
    "text": "message in the same thread",
    "thread": {
        "name": "spaces/AAAASchux4w/threads/GgWXNtFDOpE"
     }
}
Juan Rada
  • 3,513
  • 1
  • 26
  • 26
  • 1
    The damn documentation is so bad, I tried threadKey, thread_key in the query string as documented and only this one worked – JorgeeFG May 13 '22 at 12:29
0

When you send the first message you will receive a response json, in this response you receive a thread property. Tou have to send new messages with this thread property on the same level as text and/or cards.

public async Task ExecutionStarted(string fileName, string filePath)
        {
            var today = DateTime.Now.ToString("yyyyMMddTHH:mm:ss");
            string json = "{\"text\":\"" + today + ": Processing file " + fileName + " from " + filePath + "\"}";
            if(this.currentThread){
                json.thread = this.currentThread
            }
            await PostToGoogleChat(json);
        }

public async Task PostToGoogleChat(string json)
        {
            HttpClient client = new HttpClient();
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var result = await client.PostAsync(new Uri(_chatUrl), content);
            this.currentThread = result.thread
        }
Rafalages
  • 1,376
  • 1
  • 9
  • 19