Skip to content

Commit

Permalink
[Feat] Add telegram push and config file
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeCatsLoveFish committed May 30, 2022
1 parent 8a5dd55 commit 100e94e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 13 deletions.
8 changes: 6 additions & 2 deletions Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ PUSH:
token: "<YOUR-TOKEN-HERE>"
type: "push_deer" # 推送服务类型为 PushDeer
url: "http://<pushdeer-url-or-ip>/message/push"
# 推送服务,每日打卡成功或报错日志推送
# 目前仅支持PushDeer和PushPlus
- name: "TELEGRAM_SAMPLE"
token: "<YOUR-TELEGRAM-CHATID>"
type: "telegram"
url: "https://api.telegram.org/bot<YOUR-BOT-TOKEN-HERE>/sendMessage"
# 推送服务,每日打卡成功或报错日志推送
# 目前支持PushDeer, PushPlus, Telegram
```

请务必严格填写,否则程序将读取失败,可以在这里 [YAML、YML 在线编辑器(格式化校验)-BeJSON.com](https://www.bejson.com/validators/yaml_editor/) 验证你填的 yaml 是否正确
3 changes: 2 additions & 1 deletion service/push/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func SetEndpoint(endpoint util.Endpoint) {
addPush(endpoint.Name, PushDeerPush{endpoint})
case PushPlusName:
addPush(endpoint.Name, PushPlusPush{endpoint})
case TelegramName:
addPush(endpoint.Name, TelegramPush{endpoint})
}
}

Expand All @@ -28,7 +30,6 @@ type Data struct {

// Push contain all info needed for push action
type Push interface {
Info() util.Endpoint
Submit(data Data) error
}

Expand Down
4 changes: 0 additions & 4 deletions service/push/push_deer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,3 @@ func (push PushDeerPush) Submit(pd Data) error {
_, err := manager.Post(push.URL, data)
return err
}

func (push PushDeerPush) Info() util.Endpoint {
return push.Endpoint
}
4 changes: 0 additions & 4 deletions service/push/push_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,3 @@ func (push PushPlusPush) Submit(pd Data) error {
_, err := manager.Get(push.URL, data)
return err
}

func (push PushPlusPush) Info() util.Endpoint {
return push.Endpoint
}
57 changes: 57 additions & 0 deletions service/push/push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package push

import (
"MedalHelper/util"
"testing"
)

func TestPushDeerPush(t *testing.T) {
push := PushDeerPush{
util.Endpoint{
Name: "push",
Type: "push_deer",
URL: "http://api2.pushdeer.com/message/push",
Token: "<YOUR-TOKEN-HERE>",
},
}
if err := push.Submit(Data{
Title: "# Just for test",
Content: "Good Morning!",
}); err != nil {
t.Fatalf("Push error: %v", err)
}
}

func TestPushPlusPush(t *testing.T) {
push := PushDeerPush{
util.Endpoint{
Name: "push",
Type: "push_plus",
URL: "http://www.pushplus.plus/send",
Token: "<YOUR-TOKEN-HERE>",
},
}
if err := push.Submit(Data{
Title: "# Just for test",
Content: "Good Morning!",
}); err != nil {
t.Fatalf("Push error: %v", err)
}
}

func TestTelegramPush(t *testing.T) {
push := TelegramPush{
util.Endpoint{
Name: "push",
Type: "telegram",
URL: "https://api.telegram.org/bot<YOUR-BOT-TOKEN-HERE>/sendMessage",
Token: "<YOUR-TELEGRAM-CHATID>",
},
}
if err := push.Submit(Data{
Title: "# Just for test",
Content: "Good Morning!",
}); err != nil {
t.Fatalf("Push error: %v", err)
}
}
27 changes: 27 additions & 0 deletions service/push/telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package push

import (
"MedalHelper/manager"
"MedalHelper/util"
"fmt"
"net/url"
)

const TelegramName string = "telegram"

// Telegram BOT push
type TelegramPush struct {
util.Endpoint
}

// Submit data to endpoint and finish one task
func (push TelegramPush) Submit(pd Data) error {
// Prepare content and header
data := url.Values{
"chat_id": []string{push.Token},
"text": []string{fmt.Sprintf("%s, %s", pd.Title, pd.Content)},
}
// Submit info
_, err := manager.Get(push.URL, data)
return err
}
8 changes: 6 additions & 2 deletions users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ PUSH:
token: "<YOUR-TOKEN-HERE>"
type: "push_plus"
url: "http://www.pushplus.plus/send"
# 推送服务,每日打卡成功或报错日志推送
# 目前仅支持PushDeer和PushPlus
- name: "TELEGRAM_SAMPLE"
token: "<YOUR-TELEGRAM-CHATID>"
type: "telegram"
url: "https://api.telegram.org/bot<YOUR-BOT-TOKEN-HERE>/sendMessage"
# 推送服务,每日打卡成功或报错日志推送
# 目前支持PushDeer, PushPlus, Telegram

0 comments on commit 100e94e

Please sign in to comment.