I am new to python and coding in general and I have been tasked with creating a Google Chat Bot that will send an alert in a Google Chat Room when our hardware monitoring service detects an issue. The general premise of the project is that the monitoring service takes my webhook URL and uses it to give me the JSON and I have to get that to the Google Chat in a readable and timely fashion.
This is what I have so far :
from flask import Flask, request, abort, Response
from json import dumps
from httplib2 import Http
app = Flask(__name__)
def main(botmsg):
url = #placeholder for Google Chat Webhook URL#
bot_message = {
'text' : botmsg}
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)
@app.route('/', methods=['POST'])
def respond():
if request.method == 'POST':
print(request.json)
return Response(status=200)
else: abort(400)
if __name__ == '__main__':
app.run()
At first I had this exact file running locally. I would spin up my flask server but I could never get the main() to ping the Google Chat when I triggered it with a local webhook. Alone, the main function is working but I couldn't call the function in response to receiving the webhook. Now I am struggling with the same issue with this code in a Google Function pinging it using Github.
I get this error in my function logs when I ping it with the webhook:
"TypeError: Object of type Request is not JSON serializable"
I have been scraping the internet for weeks on how to do this to no avail so if anyone could give me some pointers it would be greatly appreciated!