I've set up a webhook in a chat room in my Google Hangouts Chat.
I can successfully run their example code, which generates a message from the bot associated with the webhook in the chat:
from httplib2 import Http
from json import dumps
#
# Hangouts Chat incoming webhook quickstart
#
def main():
url = '<INCOMING-WEBHOOK-URL>'
bot_message = {
'text' : 'Hello World!'}
message_headers = { 'Content-Type': 'application/json; charset=UTF-8'}
http_obj = Http()
response = http_obj.request(
uri=url,
method='POST',
headers=message_headers,
body=dumps(bot_message),
)
print(response)
if __name__ == '__main__':
main()
But when I try to sent a Numpy Array with the code:
bot_message = {
'text' : NumpyArrayObject}
I get the error:
TypeError: Object of type 'ndarray' is not JSON serializable
Using Python list I got the error:
"description": "Invalid JSON payload received. Unknown name \\"text\\" at \'message\': Proto field is not repeating, cannot start list."\n }\n ]\n }\n ]\n }\n}\n')
What should I do?
The reason for the error is that a NumPy
array is an object, presumably w/a variety of struct/binary/metadata, and can't be directly serialized (converted to a bytestream) that can be saved to JSON format. In order to do so, you need to first convert your array into something that can, using something like ndarray.tolist()
. Please see this SO answer for specifics.