Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97cc7ba

Browse files
committed
关键词回复写库
1 parent 42d2b9b commit 97cc7ba

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

config/db.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,16 @@ CREATE TABLE `wechat_user` (
827827
KEY `uid` (`uid`),
828828
KEY `updated_at` (`updated_at`)
829829
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信用户绑定表';
830+
831+
CREATE TABLE `wechat_auto_reply` (
832+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
833+
`typ` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '回复类型:0-关键词回复;1-收到消息未找到回复;2-被关注回复',
834+
`word` varchar(15) NOT NULL DEFAULT '' COMMENT '关键词',
835+
`msg_type` varchar(15) NOT NULL DEFAULT '' COMMENT '回复消息类型,和微信对应',
836+
`content` varchar(255) NOT NULL DEFAULT '' COMMENT '要回复的内容',
837+
`created_at` timestamp NOT NULL DEFAULT '2020-07-13 14:38:09',
838+
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
839+
PRIMARY KEY (`id`),
840+
KEY `word` (`word`),
841+
KEY `updated_at` (`updated_at`)
842+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信自动回复';

http/http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ func Render(ctx echo.Context, contentTpl string, data map[string]interface{}) er
260260
return err
261261
}
262262

263+
if strings.Contains(ctx.Request().UserAgent(), "miniProgram") {
264+
data["min_program"] = true
265+
}
266+
263267
data["pos_ad"] = logic.DefaultAd.FindAll(context.EchoContext(ctx), ctx.Path())
264268
data["cur_time"] = times.Format("Y-m-d H:i:s")
265269
data["path"] = ctx.Path()

logic/wechat.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func (self WechatLogic) AutoReply(ctx context.Context, reqData []byte) (*model.W
176176
} else if strings.Contains(wechatMsg.Content, "图书") || strings.Contains(wechatMsg.Content, "book") {
177177
return self.bookContent(ctx, wechatMsg)
178178
} else {
179+
// 用户获取验证码用
179180
user := DefaultUser.FindOne(ctx, "username", wechatMsg.Content)
180181
if user.Uid > 0 {
181182
var content string
@@ -188,13 +189,28 @@ func (self WechatLogic) AutoReply(ctx context.Context, reqData []byte) (*model.W
188189
return self.wechatResponse(ctx, content, wechatMsg)
189190
}
190191

192+
// 关键词回复
193+
autoReply := &model.WechatAutoReply{}
194+
MasterDB.Where("word LIKE ?", "%"+wechatMsg.Content+"%").Get(autoReply)
195+
if autoReply.Id != 0 {
196+
wechatMsg.MsgType = autoReply.MsgType
197+
return self.wechatResponse(ctx, autoReply.Content, wechatMsg)
198+
}
199+
191200
return self.searchContent(ctx, wechatMsg)
192201
}
193202
case model.WeMsgTypeEvent:
194203
switch wechatMsg.Event {
195204
case model.WeEventSubscribe:
196205
wechatMsg.MsgType = model.WeMsgTypeText
197206
welcomeText := strings.ReplaceAll(config.ConfigFile.MustValue("wechat", "subscribe"), "\\n", "\n")
207+
208+
autoReply := &model.WechatAutoReply{}
209+
_, err = MasterDB.Where("typ=?", model.AutoReplyTypSubscribe).Get(autoReply)
210+
if err == nil {
211+
welcomeText = autoReply.Content
212+
}
213+
198214
return self.wechatResponse(ctx, welcomeText, wechatMsg)
199215
}
200216
}

model/wechat.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ type WechatUser struct {
2222
CreatedAt time.Time
2323
UpdatedAt time.Time `xorm:"<-"`
2424
}
25+
26+
const (
27+
AutoReplyTypWord = iota // 关键词回复
28+
AutoReplyTypNotFound // 收到消息(未命中关键词且未搜索到)
29+
AutoReplyTypSubscribe // 被关注回复
30+
)
31+
32+
// WechatAutoReply 微信自动回复
33+
type WechatAutoReply struct {
34+
Id int `xorm:"pk autoincr"`
35+
Typ uint8
36+
Word string
37+
MsgType string
38+
Content string
39+
CreatedAt time.Time
40+
UpdatedAt time.Time `xorm:"<-"`
41+
}

template/common/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249

250250
<script type="text/javascript" src="{{.static_domain}}/static/dist/js/sidebar.min.js"></script>
251251

252-
{{if .is_pro}}
252+
{{if and .is_pro (not .min_program)}}
253253
<!-- 统计分析、广告脚本等 -->
254254
{{include "common/analytics.html" .}}
255255
{{end}}

0 commit comments

Comments
 (0)