Skip to content

Commit

Permalink
feat: 增加插件-监控转发公众号消息
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Feb 7, 2023
1 parent 8214fa6 commit ab95e98
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 0 deletions.
5 changes: 5 additions & 0 deletions engine/robot/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ func (ctx *Ctx) IsEventSelfMessage() bool {
func (ctx *Ctx) IsEventFriendVerify() bool {
return ctx.Event.Type == EventFriendVerify
}

// IsEventSubscription 判断消息是否是订阅消息
func (ctx *Ctx) IsEventSubscription() bool {
return ctx.Event.Type == EventSubscription
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
_ "github.com/yqchilde/wxbot/plugins/baidubaike" // 百度百科
_ "github.com/yqchilde/wxbot/plugins/chatgpt" // GPT聊天
_ "github.com/yqchilde/wxbot/plugins/crazykfc" // 肯德基疯狂星期四骚话
_ "github.com/yqchilde/wxbot/plugins/ghmonitor" // 公众号消息监控转发
_ "github.com/yqchilde/wxbot/plugins/jingdong" // 京豆上车
_ "github.com/yqchilde/wxbot/plugins/manager" // 群组管理相关
_ "github.com/yqchilde/wxbot/plugins/memepicture" // 表情包原图
Expand Down
80 changes: 80 additions & 0 deletions plugins/ghmonitor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ghmonitor

import (
"encoding/xml"
"strings"

"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/pkg/sqlite"
"github.com/yqchilde/wxbot/engine/robot"
)

var (
db sqlite.DB
monitor Monitor
)

type Monitor struct {
Mode int `gorm:"column:mode"` // 模式
GhWxId string `gorm:"column:gh_wxid"` // 监控公众号wxId
PushWxId string `gorm:"column:push_wxid"` // 推送转发wxId
}

func init() {
engine := control.Register("ghmonitor", &control.Options[*robot.Ctx]{
Alias: "公众号监控",
DataFolder: "ghmonitor",
})

if err := sqlite.Open(engine.GetDataFolder()+"/monitor.db", &db); err != nil {
log.Fatalf("open sqlite db failed: %v", err)
}
if err := db.Create("monitor", &monitor); err != nil {
log.Fatalf("create monitor table failed: %v", err)
}

engine.OnRegex(`monitor (gh_.*) push (.*)`, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
ghAccount := ctx.State["regex_matched"].([]string)[1]
pushAccount := strings.Split(ctx.State["regex_matched"].([]string)[2], ";")

for i := range pushAccount {
db.Orm.Table("monitor").FirstOrCreate(&Monitor{
Mode: 1,
GhWxId: ghAccount,
PushWxId: pushAccount[i],
})
}

ctx.ReplyText("设置成功,查看请输入 monitor get")
})

// 监控模式
// 1. 指定公众号的所有发布消息都转发
// 2. 标题或描述有关键字的发布消息转发
// 3. 文章中内容有关键字的发布消息转发

engine.OnMessage().SetBlock(false).Handle(func(ctx *robot.Ctx) {
if ctx.IsEventSubscription() {
var monitorList []Monitor
if err := db.Orm.Table("monitor").Find(&monitorList).Error; err != nil {
return
}

for _, data := range monitorList {
switch data.Mode {
case 1: // 模式1实现
content := ctx.Event.SubscriptionMessage.Content
var msgModel SubscriptionMsgModel
if err := xml.Unmarshal([]byte(content), &msgModel); err != nil {
return
}
msgModel.Fromusername = robot.WxBot.BotConfig.BotWxId
if newXml, err := xml.Marshal(msgModel); err == nil {
ctx.SendXML(data.PushWxId, string(newXml))
}
}
}
}
})
}
141 changes: 141 additions & 0 deletions plugins/ghmonitor/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package ghmonitor

import "encoding/xml"

type SubscriptionMsgModel struct {
XMLName xml.Name `xml:"msg"`
Text string `xml:",chardata"`
Appmsg struct {
Text string `xml:",chardata"`
Appid string `xml:"appid,attr"`
Sdkver string `xml:"sdkver,attr"`
Title string `xml:"title"`
Des string `xml:"des"`
Action string `xml:"action"`
Type string `xml:"type"`
Showtype string `xml:"showtype"`
Content string `xml:"content"`
Contentattr string `xml:"contentattr"`
URL string `xml:"url"`
Lowurl string `xml:"lowurl"`
Appattach struct {
Text string `xml:",chardata"`
Totallen string `xml:"totallen"`
Attachid string `xml:"attachid"`
Fileext string `xml:"fileext"`
} `xml:"appattach"`
Extinfo string `xml:"extinfo"`
Mmreader struct {
Text string `xml:",chardata"`
Category struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
Count string `xml:"count,attr"`
Name string `xml:"name"`
Topnew struct {
Text string `xml:",chardata"`
Cover string `xml:"cover"`
Width string `xml:"width"`
Height string `xml:"height"`
Digest string `xml:"digest"`
} `xml:"topnew"`
Item struct {
Text string `xml:",chardata"`
Itemshowtype string `xml:"itemshowtype"`
Title string `xml:"title"`
URL string `xml:"url"`
Shorturl string `xml:"shorturl"`
Longurl string `xml:"longurl"`
PubTime string `xml:"pub_time"`
Summary string `xml:"summary"`
Cover string `xml:"cover"`
Tweetid string `xml:"tweetid"`
Digest string `xml:"digest"`
Fileid string `xml:"fileid"`
Sources struct {
Text string `xml:",chardata"`
Source struct {
Text string `xml:",chardata"`
Name string `xml:"name"`
} `xml:"source"`
} `xml:"sources"`
Styles string `xml:"styles"`
NativeURL string `xml:"native_url"`
DelFlag string `xml:"del_flag"`
Contentattr string `xml:"contentattr"`
PlayLength string `xml:"play_length"`
PlayURL string `xml:"play_url"`
VoiceID string `xml:"voice_id"`
Player string `xml:"player"`
MusicSource string `xml:"music_source"`
PicNum string `xml:"pic_num"`
Vid string `xml:"vid"`
Author string `xml:"author"`
Recommendation string `xml:"recommendation"`
PicUrls string `xml:"pic_urls"`
CommentTopicID string `xml:"comment_topic_id"`
Cover2351 string `xml:"cover_235_1"`
Cover11 string `xml:"cover_1_1"`
Cover169 string `xml:"cover_16_9"`
AppmsgLikeType string `xml:"appmsg_like_type"`
VideoWidth string `xml:"video_width"`
VideoHeight string `xml:"video_height"`
IsPaySubscribe string `xml:"is_pay_subscribe"`
FinderFeed struct {
Text string `xml:",chardata"`
ObjectID string `xml:"object_id"`
ObjectNonceID string `xml:"object_nonce_id"`
FeedType string `xml:"feed_type"`
Nickname string `xml:"nickname"`
Avatar string `xml:"avatar"`
Desc string `xml:"desc"`
MediaCount string `xml:"media_count"`
MediaList string `xml:"media_list"`
MegaVideo struct {
Text string `xml:",chardata"`
ObjectID string `xml:"object_id"`
ObjectNonceID string `xml:"object_nonce_id"`
} `xml:"mega_video"`
} `xml:"finder_feed"`
FinderLive struct {
Text string `xml:",chardata"`
FinderUsername string `xml:"finder_username"`
Category string `xml:"category"`
FinderNonceID string `xml:"finder_nonce_id"`
ExportID string `xml:"export_id"`
Nickname string `xml:"nickname"`
HeadURL string `xml:"head_url"`
Desc string `xml:"desc"`
LiveStatus string `xml:"live_status"`
LiveSourceTypeStr string `xml:"live_source_type_str"`
ExtFlag string `xml:"ext_flag"`
AuthIconURL string `xml:"auth_icon_url"`
AuthIconTypeStr string `xml:"auth_icon_type_str"`
Media struct {
Text string `xml:",chardata"`
CoverURL string `xml:"cover_url"`
Height string `xml:"height"`
Width string `xml:"width"`
} `xml:"media"`
} `xml:"finder_live"`
} `xml:"item"`
} `xml:"category"`
Publisher struct {
Text string `xml:",chardata"`
Username string `xml:"username"`
Nickname string `xml:"nickname"`
} `xml:"publisher"`
TemplateHeader string `xml:"template_header"`
TemplateDetail string `xml:"template_detail"`
ForbidForward string `xml:"forbid_forward"`
} `xml:"mmreader"`
Thumburl string `xml:"thumburl"`
} `xml:"appmsg"`
Fromusername string `xml:"fromusername"`
Appinfo struct {
Text string `xml:",chardata"`
Version string `xml:"version"`
Appname string `xml:"appname"`
Isforceupdate string `xml:"isforceupdate"`
} `xml:"appinfo"`
}

0 comments on commit ab95e98

Please sign in to comment.