docs: add more examples

This commit is contained in:
JustSong
2022-12-22 10:13:04 +08:00
parent c1312e456f
commit 909a299ca8
+111 -4
View File
@@ -165,6 +165,34 @@ proxy_send_timeout 300s;
+ 注意:请求体编码格式为 `application/json`。
**示例:**
<details>
<summary><strong>点击展开 Bash 示例 </strong></summary>
<div>
```shell
#!/bin/bash
MESSAGE_PUSHER_SERVER="https://msgpusher.com"
MESSAGE_PUSHER_USERNAME="test"
MESSAGE_PUSHER_TOKEN="666"
function send_message {
curl -s -X POST "$MESSAGE_PUSHER_SERVER/push/$MESSAGE_PUSHER_USERNAME" \
-H 'Content-Type: application/json' \
-d '{"title":"'"$1"'","description":"'"$2"'", "content":"'"$3"'", "token":"'"$MESSAGE_PUSHER_TOKEN"'"}' > /dev/null
}
send_message 'title' 'description' 'content'
```
</div>
</details>
<details>
<summary><strong>点击展开 Python 示例 </strong></summary>
<div>
```python
import requests
@@ -173,16 +201,95 @@ res = requests.get("https://your.domain.com/push/username?title={}&description={
# POST 方式
res = requests.post("https://your.domain.com/push/username", json={
"title": "标题",
"description": "描述",
"content": "**Markdown 内容**",
"token": "6666"
"title": "标题",
"description": "描述",
"content": "**Markdown 内容**",
"token": "6666"
})
print(res.text)
# 输出为:{"success":true,"message":"ok"}
```
</div>
</details>
<details>
<summary><strong>点击展开 Python 示例 </strong></summary>
<div>
```go
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var serverAddress = "https://msgpusher.com"
var username = "test"
var token = "666"
type request struct {
Title string `json:"title"`
Description string `json:"description"`
Content string `json:"content"`
URL string `json:"url"`
Channel string `json:"channel"`
Token string `json:"token"`
}
type response struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func SendMessage(title string, description string, content string) error {
req := request{
Title: title,
Description: description,
Content: content,
Token: token,
}
data, err := json.Marshal(req)
if err != nil {
return err
}
resp, err := http.Post(fmt.Sprintf("%s/push/%s", serverAddress, username),
"application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
var res response
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return err
}
if !res.Success {
return errors.New(res.Message)
}
return nil
}
func main() {
err := SendMessage("标题", "描述", "**Markdown 内容**")
if err != nil {
fmt.Println("推送失败:" + err.Error())
} else {
fmt.Println("推送成功!")
}
}
```
</div>
</details>
欢迎 PR 添加更多语言的示例。
## 其他
1. `v0.3` 之前的版本基于 Node.js,你可以切换到 [`nodejs`](https://github.com/songquanpeng/message-pusher/tree/nodejs) 分支查看,该版本不再有功能性更新。
2. `v0.3` 以及后续版本基于 Gin Template [`v0.2.1`](https://github.com/songquanpeng/gin-template) 版本开发。