Skip to content

Commit

Permalink
chore: update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Sep 14, 2023
1 parent 5cdb3e9 commit 0690cf9
Show file tree
Hide file tree
Showing 13 changed files with 843 additions and 176 deletions.
1 change: 1 addition & 0 deletions .revive.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ severity = "warning"
[rule.var-naming]
[rule.var-declaration]
[rule.package-comments]
severity = "warning"
[rule.range]
[rule.receiver-naming]
[rule.time-naming]
Expand Down
11 changes: 7 additions & 4 deletions event/command/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package command

import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"time"

log "github.com/sirupsen/logrus"
"gopkg.in/telebot.v3"
)

// About 返回关于信息
func About(b *telebot.Bot) {
b.Handle("/about", func(m *telebot.Message) {
_, err := b.Send(m.Chat, fmt.Sprintf(`ヒトコト(一言) 官方 Telegram 机器人。 目前仅提供简体中文支持。 主要提供一句话服务。
b.Handle("/about", func(ctx telebot.Context) error {
_, err := b.Send(ctx.Chat(), fmt.Sprintf(`ヒトコト(一言) 官方 Telegram 机器人。 目前仅提供简体中文支持。 主要提供一句话服务。
* 官方网站: https://hitokoto.cn
* 官方 QQ 群组: 70029304
* 非官方 Telegram 群组: https://t.me/hitokoto
Expand All @@ -19,6 +20,8 @@ func About(b *telebot.Bot) {
当前服务器时间:%s`, time.Now().Format("2006年1月2日 15:04:05")))
if err != nil {
log.Errorf("发送消息时发生了错误,错误信息: %s \n", err)
return err
}
return nil
})
}
11 changes: 7 additions & 4 deletions event/command/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package command

import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"time"

log "github.com/sirupsen/logrus"
"gopkg.in/telebot.v3"
)

// Help 返回使用帮助
func Help(b *telebot.Bot) {
b.Handle("/help", func(m *telebot.Message) {
_, err := b.Send(m.Chat, fmt.Sprintf(`*以下是目前支持的指令:*
b.Handle("/help", func(ctx telebot.Context) error {
_, err := b.Send(ctx.Chat(), fmt.Sprintf(`*以下是目前支持的指令:*
/about 关于机器人
/hitokoto [分类] 获取一条句子,[分类] 可以在“开发者中心”的“语句接口”部分找到详细定义。默认返回随机分类。
/help 获取机器人帮助信息。
Expand All @@ -25,6 +26,8 @@ func Help(b *telebot.Bot) {
)
if err != nil {
log.Errorf("发送消息时发生了错误,错误信息: %s \n", err)
return err
}
return nil
})
}
19 changes: 11 additions & 8 deletions event/command/hitokoto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package command

import (
"fmt"

"github.com/levigross/grequests"
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"gopkg.in/telebot.v3"
)

var supportedTypeList = []string{
Expand Down Expand Up @@ -34,8 +35,8 @@ func inStringSlice(haystack []string, needle string) bool {

// Hitokoto 获取一言
func Hitokoto(b *telebot.Bot) {
b.Handle("/hitokoto", func(m *telebot.Message) {
payload := m.Payload // 指令:`/hitokoto <payload>` 这里提取 payload 用于提取参数
b.Handle("/hitokoto", func(ctx telebot.Context) error {
payload := ctx.Message().Payload // 指令:`/hitokoto <payload>` 这里提取 payload 用于提取参数
url := "https://v1.hitokoto.cn/"
if inStringSlice(supportedTypeList, payload) {
url += "?c=" + payload
Expand All @@ -45,26 +46,28 @@ func Hitokoto(b *telebot.Bot) {
response, err := grequests.Get(url, nil)
if err != nil {
log.Errorf("尝试获取一言时出现错误,错误信息: %s\n", err)
_, err = b.Send(m.Chat, "很抱歉,尝试获取发生错误。")
_, err = b.Send(ctx.Chat(), "很抱歉,尝试获取发生错误。")
if err != nil {
log.Errorf("尝试发送消息时出现错误,错误信息:%s \n", err)
}
return
return err
}
data := &HitokotoSentenceAPIV1Response{}
err = response.JSON(data)
if err != nil {
log.Errorf("尝试解析一言时发生错误,错误信息: %s", err)
_, err = b.Send(m.Chat, "很抱歉,尝试解析一言时发生错误。")
_, err = b.Send(ctx.Chat(), "很抱歉,尝试解析一言时发生错误。")
if err != nil {
log.Errorf("尝试发送消息时出现错误,错误信息:%s \n", err)
}
return
return err
}
_, err = b.Reply(m, fmt.Sprintf(`%s —— %s「%s」`, data.Hitokoto, data.FromWho, data.From))
_, err = b.Reply(ctx.Message(), fmt.Sprintf(`%s —— %s「%s」`, data.Hitokoto, data.FromWho, data.From))
if err != nil {
log.Errorf("尝试发送消息时出现错误,错误信息:%s \n", err)
return err
}
return nil
})
}

Expand Down
13 changes: 8 additions & 5 deletions event/command/image.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package command

import (
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"gopkg.in/telebot.v3"
"strconv"
"time"

log "github.com/sirupsen/logrus"
)

// Image 返回随机必应图片
func Image(bot *telebot.Bot) {
bot.Handle("/image", func(m *telebot.Message) {
bot.Handle("/image", func(ctx telebot.Context) error {
// 暂时先只做 Bing
photo := &telebot.Photo{
File: telebot.FromURL("https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture&r=" + strconv.FormatInt(time.Now().UnixNano(), 10)),
}
_, err := photo.Send(bot, m.Chat, &telebot.SendOptions{
ReplyTo: m,
_, err := photo.Send(bot, ctx.Chat(), &telebot.SendOptions{
ReplyTo: ctx.Message(),
})
if err != nil {
log.Errorf("发送消息时发生了错误,错误信息: %s \n", err)
return err
}
return nil
})
}
7 changes: 4 additions & 3 deletions event/command/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package command

import (
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"gopkg.in/telebot.v3"
)

// Ping 查询机器人是否在线
func Ping(b *telebot.Bot) {
b.Handle("/ping", func(m *telebot.Message) {
_, err := b.Send(m.Chat, "Pong!")
b.Handle("/ping", func(m telebot.Context) error {
_, err := b.Send(m.Chat(), "Pong!")
if err != nil {
log.Errorf("发送消息时发生了错误,错误信息: %s \n", err)
}
return err
})
}
12 changes: 7 additions & 5 deletions event/command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ package command

import (
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"gopkg.in/telebot.v3"
)

// Start 用于响应 Telegram 要求的机器人 Start 指令
func Start(bot *telebot.Bot) {
bot.Handle("/start", func(m *telebot.Message) {
if !m.Private() { // 如果不是私发消息,不回复
return
bot.Handle("/start", func(ctx telebot.Context) error {
if !ctx.Message().Private() { // 如果不是私发消息,不回复
return nil
}
_, err := bot.Send(m.Sender, `欢迎您选用一言的服务。
_, err := bot.Send(ctx.Sender(), `欢迎您选用一言的服务。
在这里,你可以领略天之高,地之深,可以感受思维的边疆,领略美好,收获感动。还在犹豫什么,快来和我们一起玩耍吧!
你可以...
使用 /about 以更深入得了解我,
使用 /help 查看使用机器人的说明`)
if err != nil {
log.Errorf("发送消息时发生了错误,错误信息: %s \n", err)
return err
}
return nil
})
}
32 changes: 17 additions & 15 deletions event/command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,53 @@ package command

import (
"fmt"
"github.com/hitokoto-osc/telegram_bot/build"
"github.com/levigross/grequests"
"github.com/shirou/gopsutil/load"
log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2"
"gopkg.in/telebot.v3"
"runtime"
"strconv"
"strings"
"time"

"github.com/hitokoto-osc/telegram_bot/build"
"github.com/levigross/grequests"
"github.com/shirou/gopsutil/v3/load"
log "github.com/sirupsen/logrus"
)

// Status 用于响应获取统计信息的指令
func Status(b *telebot.Bot) {
b.Handle("/status", func(m *telebot.Message) {
b.Handle("/status", func(ctx telebot.Context) error {
response, err := grequests.Get("https://status.hitokoto.cn/v1/statistic", nil)
if err != nil {
log.Errorf("尝试获取统计数据时出现错误,错误信息: %s\n", err)
_, err = b.Send(m.Chat, "很抱歉,尝试获取数据时发生错误。")
_, err = b.Send(ctx.Chat(), "很抱歉,尝试获取数据时发生错误。")
if err != nil {
log.Errorf("尝试发送消息时出现错误,错误信息:%s \n", err)
}
return
return err
}
result := &hitokotoStatusAPIV1Response{}
err = response.JSON(result)
if err != nil {
log.Errorf("尝试解析统计数据时发生错误,错误信息: %s", err)
_, err = b.Send(m.Chat, "很抱歉,尝试解析数据时发生错误。")
_, err = b.Send(ctx.Chat(), "很抱歉,尝试解析数据时发生错误。")
if err != nil {
log.Errorf("尝试发送消息时出现错误,错误信息:%s \n", err)
}
return
return err
}

// 读取系统负载
lo, err := load.Avg()
if err != nil {
log.Errorf("尝试解析系统负载时发生错误,错误信息: %s", err)
_, err = b.Send(m.Chat, "很抱歉,尝试解析系统负载时发生错误。")
_, err = b.Send(ctx.Chat(), "很抱歉,尝试解析系统负载时发生错误。")
if err != nil {
log.Errorf("尝试发送消息时出现错误,错误信息:%s \n", err)
}
return
return err
}
// log.Debug(data)
_, err = b.Send(m.Chat, fmt.Sprintf(`*[一言统计信息]*
_, err = b.Send(ctx.Chat(), fmt.Sprintf(`*[一言统计信息]*
句子总数: %s
现存分类: %s
服务负载: %s
Expand Down Expand Up @@ -86,14 +87,15 @@ func Status(b *telebot.Bot) {
ParseMode: "Markdown",
},
)
return err
})
}

type hitokotoStatusAPIV1Response struct { // 因为不需要使用全部数据,所以这里就只解析部分了
Data struct{
Data struct {
Status status `json:"status"`
Requests requests `json:"requests"`
} `json:"data"`
} `json:"data"`
}

type status struct {
Expand Down
2 changes: 1 addition & 1 deletion event/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package event

import (
"github.com/hitokoto-osc/telegram_bot/event/command"
"gopkg.in/tucnak/telebot.v2"
"gopkg.in/telebot.v3"
)

// RegisterEvent 定义了机器人事件处理的入口
Expand Down
52 changes: 41 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
module github.com/hitokoto-osc/telegram_bot

go 1.14
go 1.21

require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/levigross/grequests v0.0.0-20190908174114-253788527a1a
github.com/levigross/grequests v0.0.0-20221222020224-9eee758d18d5
github.com/shirou/gopsutil/v3 v3.23.8
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.16.0
gopkg.in/telebot.v3 v3.1.3
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/chavacava/garif v0.1.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
github.com/mgechev/revive v1.3.3 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shirou/gopsutil v3.21.1+incompatible
github.com/sirupsen/logrus v1.7.0
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
gopkg.in/tucnak/telebot.v2 v2.3.5
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 0690cf9

Please sign in to comment.