groovy 向企业微信机器人发消息
时间: 2023-08-08 14:08:45 浏览: 71
你可以使用Groovy的HTTPBuilder库向企业微信机器人发送消息。以下是一个简单的示例代码:
```groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def http = new HTTPBuilder('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=<your_key_here>')
http.request(POST, JSON) {
uri.path = '/cgi-bin/webhook/send'
headers.'Content-Type' = 'application/json'
body = [
msgtype: 'text',
text: [
content: 'Hello, World!'
]
]
response.success = { resp, json ->
println "Message sent successfully!"
}
response.failure = { resp, json ->
println "Error sending message: ${resp.statusLine}"
}
}
```
请将`<your_key_here>`替换为你的企业微信机器人的Key。此代码将发送一条简单的文本消息。你可以通过更改`msgtype`和`text`字段来发送其他类型的消息,例如图片,Markdown格式的消息等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)