1

I'm trying to create an httptrigger function that calls a Google Chat API webhook. I'm doing this so I can provide another API with a simple callback URL.

Basically I've written the following but getting the usual unhelpful 500 error

import logging

import azure.functions as func

# for chat API call
import http.client
import json
# end for chat API call

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # call the other API
    conn = http.client.HTTPSConnection("chat.googleapis.com")
    payload = "{'text':'hello world, from my azure function.'\r\n}"
    headers = {'Content-Type': 'application/json'}
    conn.request("POST", "/v1/spaces/Ajdf9u4jf4/messages?key=f03j4fij43", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    # end call

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')    
    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

I'm getting a 200 on the function but I don't see a message in Google Chat. The python I have added to this function to post to the Chat webhook works in Postman, but not here. Any suggestions?

Kevin D
  • 98
  • 8

0 Answers0