-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |