ppasler
ppasler

Reputation: 3729

Setting up an incoming webhook for Hangouts Chat API with Java?

I followed the example here (Incoming webhook with Python), which sends a simple message to a Hangouts chat room and works as expected

from httplib2 import Http
from json import dumps

def main():
    url = 'https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>'
    bot_message = {
        'text' : 'Hello from Python script!'}

    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()

Now I want achive the same simple thing using Java and tried it with this code

private void sendPost() throws IOException {

    String url = "https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>";

    final HttpClient client = new DefaultHttpClient();
    final HttpPost request = new HttpPost(url);
    final HttpResponse response = client.execute(request);

    request.addHeader("Content-Type", "application/json; charset=UTF-8");

    final StringEntity params = new StringEntity("{\"text\":\"Hello from Java!\"}", ContentType.APPLICATION_FORM_URLENCODED);
    request.setEntity(params);

    final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());
}

But this leads to an error message saying

 {
    "error": {
        "code": 400,
        "message": "Message cannot be empty. Discarding empty create message request in spaces/AAAAUfABqBU.",
        "status": "INVALID_ARGUMENT"
    }
}

I assume there is something wrong with the way I add the json object. Does anybody see the mistake?

Upvotes: 0

Views: 3546

Answers (1)

ppasler
ppasler

Reputation: 3729

Kind of dump, but moving the line final HttpResponse response = client.execute(request); after setting the request body solves the issue.

private void sendPost() throws IOException {

    String url = "https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>";

    final HttpClient client = new DefaultHttpClient();
    final HttpPost request = new HttpPost(url);
    // FROM HERE

    request.addHeader("Content-Type", "application/json; charset=UTF-8");

    final StringEntity params = new StringEntity("{\"text\":\"Hello from Java!\"}", ContentType.APPLICATION_FORM_URLENCODED);
    request.setEntity(params);

    // TO HERE
    final HttpResponse response = client.execute(request);

    final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());
}

Order sometimes does matter :)

Upvotes: 2

Related Questions