This series of code address github

Gradle in Action gradle in Action

Return to the directory

In the last article, we implemented the process of packaging and uploading applications. In this article, we will continue to realize the function of sending messages to Dingding or enterprise wechat after apK uploading successfully.

Without losing generality, we use enterprise wechat to achieve this function.

Enterprise wechat provides the function of group robots. After robots are added to a group at the terminal, the webhook address can be obtained. Then the developer user constructs POST data to initiate HTTP POST request to this address according to the following instructions, so as to send messages to the group. Here’s a simple example. We can see the official documents for specific use.

We now create a group in the enterprise wechat, and add developers and related testers to this group.

Add a swarm robot. How to add group robots to enterprise wechat

Record the webhook address of the robot.

Next we create the task to send the message as follows:


task sendWeChatMessage(){
    group "publish"
    doLast{
        def robotUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bece9001-ab3e-44f2-913e-5560a9633de2"

        def markdownText = """ #### A new test package has been uploaded """
        def builder = new JsonBuilder({
            msgtype "markdown"
            markdown {
                content markdownText
            }
        })


        URL url = new URL(robotUrl)
        URLConnection connection = url.openConnection()
        connection.setDoInput(true)
        connection.setDoOutput(true)
        connection.setUseCaches(false)
        connection.setRequestMethod("POST")
        connection.setRequestProperty("Content-Type"."application/json")

        connection.connect()

        OutputStream outStream = connection.getOutputStream()

        outStream.write(builder.toString().getBytes())

        InputStream inStream = connection.getInputStream()

        Scanner scanner = new Scanner(inStream)

        while (scanner.hasNextLine()) {
            println scanner.nextLine()
        }
        outStream.close()
        inStream.close()
    }
}

Copy the code

When we perform this task, the corresponding group will receive a message as follows:

Next, we string this task together with the previously completed process.

We add the following code to build.gradle:


uploadPgyerByApi.finalizedBy(sendWeChatMessage)

Copy the code

Execute sendWeChatMessage task after uploadPgyerByApi task is finished.

We then execute the “publishReleaseRelease” task and see that three steps have been completed:

  1. Package application APK
  2. Upload apps to dandelion
  3. Send a message to the enterprise wechat group to inform the relevant wish

Send messages to Dingpin and other supporting chatbot functions are the same, interested in their own implementation of the following.

But our current implementation is a bit rough, and next time, we’ll refine it.