Post a message to Google Hangouts Chat with a thread (Python)

Overview

Here's how to post a message to Google Hangouts Chat by specifying a thread.

procedure

  1. Create a chat room
  2. Set up a webhook
  3. Post a message using the webhook URL
  4. Get the thread identifier from the response when posting the message
  5. Post a message using the webhook URL and thread identifier

1. Create a new chat room

I will omit it. (I may add it later

2. Set up a webhook

I will omit it. (I may add it later

3. Post a message using the webhook URL

Post a message using the URL of the webhook.

import requests

webhook_url = 'https://chat.googleapis.com/*******'

response = requests.post(
  webhook_url,
  json={"text": "Transmission test\n"}
)

The result looks like this. aaaaa.png

If you post multiple times this way, each message will be posted in a separate thread. スクリーンショット 2020-02-19 0.42.45.png

4. Get the thread identifier from the response

Print the response when you post a message. The content of the decoded response is described in the text attribute.

print(response.text)

Copy the thread identifier described in "thread" in this.

{
  "name": "spaces/***********/messages/***********************",
  "sender": {
    "name": "users/*********************",
    "displayName": "test_webhook",
    "avatarUrl": "",
    "email": "",
    "domainId": "",
    "type": "BOT"
  },
  "text": "Transmission test\n",
  "cards": [],
  "previewText": "",
  "annotations": [],
  "thread": {
    "name": "spaces/***********/threads/***********"← here
  },
  "space": {
    "name": "spaces/***********",
    "type": "ROOM",
    "displayName": "test"
  },
  "fallbackText": "",
  "argumentText": "Transmission test\n",
  "createTime": "2020-02-18T14:33:38.083263Z"
}

5. Post a message using the thread's identifier

Use the thread identifier obtained in step 4 to specify the thread and post the message.

import requests

webhook_url = 'https://chat.googleapis.com/*******'
thread = "spaces/***********/threads/***********"

response = requests.post(
  webhook_url,
  json = {
      "text": "Transmission test\n",
      "thread": {
          "name": thread
      }
  }
)

Then, the message will be posted to the thread that posted the message in 3. スクリーンショット 2020-02-19 0.48.45.png

Summary

I was able to post a message to Google Hangouts Chat by specifying a thread.

Postscript

If you like, please comment.

Recommended Posts

Post a message to Google Hangouts Chat with a thread (Python)
Send a message to LINE with Python (LINE Notify)
Post to slack with Python 3
How to install NPI + send a message to line with python
Create a message corresponding to localization with python translation string
Easily post to twitter with Python 3
Post a message from IBM Cloud Functions to Slack in Python
I tried to make a 2channel post notification application with Python
Send a message from Python to Slack
Upload images to Google Drive with Python
Send a message from Slack to a Python server
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
Try to draw a life curve with python
I want to make a game with Python
Post youtube soaring title to twitter with python3
Decide to assign a laboratory with Python (fiction)
Steps to create a Twitter bot with python
Try to make a dihedral group with Python
Upload files to Google Drive with Lambda (Python)
I want to write to a file with Python
[Python] Throw a message to the slack channel
Post an article with an image to WordPress with Python
A layman wants to get started with Python
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python
[Python] How to draw a line graph with Matplotlib
Try to make a command standby tool with python
Python Ver. To introduce WebPay with a little code.
I tried to draw a route map with Python
Easy way to scrape with python using Google Colab
From buying a computer to running a program with python
I tried to automatically generate a password with Python3
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
[Python] How to create a 2D histogram with Matplotlib
Upload to a shared drive with Google Drive API V3
I want to run a quantum computer with Python
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
Post from Python to Slack
Post to vim → Python → Slack
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
A road to intermediate Python
How to convert an array to a dictionary with Python [Application]
Create a Mastodon bot with a function to automatically reply with Python
I made a package to filter time series with python
A collection of competitive pro techniques to solve with Python
Connect to Wikipedia with Python
Probably the easiest way to create a pdf with Python3
Experiment to make a self-catering PDF for Kindle with Python
Post to Twitter using Python
Try to bring up a subwindow with PyQt5 and Python
Make a fortune with Python
Get thread ID with python
I want to send a message from Python to LINE Bot
How to build a python2.7 series development environment with Vagrant
Switch python to 2.7 with alternatives
Write to csv with Python
[Python] The first step to making a game with Pyxel
Access Google Drive with Python