Hangouts chat API returns 400 for create message of type actionResponse

1.3k Views Asked by At

I trying to post messages using chat python client. Request body should be of type https://developers.google.com/hangouts/chat/reference/rest/v1/spaces.messages?hl=zh-cn#Message.

This is my sample code.

    def callback(message):
    logging.info('Received message: %s', message.data)

    event = json.loads(message.data)
    space_name = event['space']['name']

    # If the bot was removed, we don't need to return a response.
    if event['type'] == 'REMOVED_FROM_SPACE':
        logging.info('Bot removed rom space %s', space_name)
        return

    # response = format_response(event)
    response = {'actionResponse': {'type': 'REQUEST_CONFIG', 'url': 'https://google.com'}}
    
    # Send the asynchronous response back to Hangouts Chat
    chat.spaces().messages().create(
        parent=space_name,
        body=response).execute()
    message.ack()

I get this error

E2020-08-14 21:43:08,632 streaming_pull_manager.py:82] Top-level exception occurred in callback while processing a message
Traceback (most recent call last):
  File "/Users/kasi/repos/hangouts-chat-samples/python/pub-sub-bot/env/lib/python3.8/site-packages/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py", line 77, in _wrap_callback_errors
    callback(message)
  File "bot.py", line 59, in callback
    chat.spaces().messages().create(
  File "/Users/kasi/repos/hangouts-chat-samples/python/pub-sub-bot/env/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/kasi/repos/hangouts-chat-samples/python/pub-sub-bot/env/lib/python3.8/site-packages/googleapiclient/http.py", line 917, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://chat.googleapis.com/v1/spaces/1YIkLgAAAAE/messages?alt=json returned "Message cannot be empty.">

If I add a text field to response, bot response message is exactly same as simple text reply. for below given response body.

        response = {
        'actionResponse': {
            'type': 'REQUEST_CONFIG',
            'url': 'https://google.com'
        },
        'text': 'This is a text',
        'thread': {'name': 'spaces/AAAAiu7EpR8/threads/4wRxupjMbbk'}
    }

I am getting this instead of a private message.

enter image description here

What I expect:

enter image description here

0

There are 0 best solutions below