Skip to content

Commit

Permalink
feat: 支持top群聊统计
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 5, 2024
1 parent aade519 commit 78b80f9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
61 changes: 61 additions & 0 deletions dbase/message/extra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package message

import (
"time"

"github.com/opentdp/go-helper/dborm"
)

type TopItem struct {
Sender string `json:"sender"`
RecordCount int32 `json:"record_count"`
}

func TodayUnix() int64 {

now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())

return today.Unix()

}

func TalkTop10(roomid string) []*TopItem {

var result []*TopItem

sql := `
SELECT sender, COUNT(sender) AS record_count
FROM message
WHERE created_at >= ? AND roomid = ?
GROUP BY sender
ORDER BY record_count DESC
LIMIT 10
`

timestamp := TodayUnix()
dborm.Db.Raw(sql, timestamp, roomid).Scan(&result)

return result

}

func ImageTop10(roomid string) []*TopItem {

var result []*TopItem

sql := `
SELECT sender, COUNT(sender) AS record_count
FROM message
WHERE created_at >= ? AND roomid = ? AND type = 3
GROUP BY sender
ORDER BY record_count DESC
LIMIT 10
`

timestamp := TodayUnix()
dborm.Db.Raw(sql, timestamp, roomid).Scan(&result)

return result

}
1 change: 1 addition & 0 deletions wclient/robot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func setupHandlers() {
apiHandler()
badHandler()
banHandler()
topHandler()
roomHandler()
wgetHandler()

Expand Down
37 changes: 37 additions & 0 deletions wclient/robot/handler_top.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package robot

import (
"fmt"
"strings"

"github.com/opentdp/wechat-rest/dbase/message"
"github.com/opentdp/wechat-rest/wcferry"
)

func topHandler() {

handlers["/top"] = &Handler{
Level: 7,
Order: 50,
ChatAble: false,
RoomAble: true,
Describe: "获取群聊统计信息",
Callback: func(msg *wcferry.WxMsg) string {
text := []string{}
// 聊天统计
text = append(text, "", "今日灌水排行", "----------------")
for _, v := range message.TalkTop10(msg.Roomid) {
u := wc.CmdClient.GetAliasInChatRoom(v.Sender, msg.Roomid)
text = append(text, fmt.Sprintf("%s: %d 次", u, v.RecordCount))
}
// 图片统计
text = append(text, "", "今日斗图排行", "----------------")
for _, v := range message.ImageTop10(msg.Roomid) {
u := wc.CmdClient.GetAliasInChatRoom(v.Sender, msg.Roomid)
text = append(text, fmt.Sprintf("%s: %d 张", u, v.RecordCount))
}
return strings.Join(text, "\n")
},
}

}

0 comments on commit 78b80f9

Please sign in to comment.