From 909a299ca80681615695640753e425be27b4fc9d Mon Sep 17 00:00:00 2001 From: JustSong Date: Thu, 22 Dec 2022 10:13:04 +0800 Subject: [PATCH] docs: add more examples --- README.md | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bdc4670..ea9f2a7 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,34 @@ proxy_send_timeout 300s; + 注意:请求体编码格式为 `application/json`。 **示例:** + +
+点击展开 Bash 示例 +
+ +```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' +``` + +
+
+ +
+点击展开 Python 示例 +
+ ```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"} ``` +
+
+ +
+点击展开 Python 示例 +
+ +```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("推送成功!") + } +} +``` + +
+
+ +欢迎 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) 版本开发。