forked from KevinGong2013/ggbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xiaoice.go
65 lines (57 loc) · 1.26 KB
/
xiaoice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"sync"
wx "github.com/KevinGong2013/wechat"
)
type xiaoice struct {
sync.Mutex
un string
waitting []string
bot *wx.WeChat
}
func newXiaoice(wx *wx.WeChat) *xiaoice {
x := &xiaoice{}
x.bot = wx
return x
}
func (x *xiaoice) autoReplay(msg wx.EventMsgData) {
if msg.IsSendedByMySelf {
return
}
if msg.FromUserName == x.un { // 小冰发来的消息
x.Lock()
x.Unlock()
count := len(x.waitting)
if count == 0 {
logger.Warnf(`msg Form xiaoice %s`, msg.Content)
return
}
to := x.waitting[count-1]
x.waitting = x.waitting[:count-1]
if msg.IsMediaMsg {
if path, err := x.bot.DownloadMedia(msg.MediaURL, msg.OriginalMsg[`MsgId`].(string)); err == nil {
x.bot.SendFile(path, to)
}
} else {
x.bot.SendTextMsg(msg.Content, to)
}
} else if !msg.IsSendedByMySelf { // 转发别人的消息到小冰
var err error
if msg.IsMediaMsg {
if path, e := x.bot.DownloadMedia(msg.MediaURL, msg.OriginalMsg[`MsgId`].(string)); e == nil {
err = x.bot.SendFile(path, x.un)
} else {
err = e
}
} else {
err = x.bot.SendTextMsg(msg.Content, x.un)
}
if err == nil {
x.Lock()
defer x.Unlock()
x.waitting = append(x.waitting, msg.FromUserName)
} else {
logger.Error(err)
}
}
}