Skip to content

Commit

Permalink
Add the msg reciever func
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuchun committed Oct 25, 2016
1 parent cbbe537 commit 069738e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 48 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# wechatcmd [![star this repo](http://github-svg-buttons.herokuapp.com/star.svg?user=liushuchun&repo=wechatcmd&style=flat&background=1081C1)](http://github.com/liushuchun/wechatcmd) [![fork this repo](http://github-svg-buttons.herokuapp.com/fork.svg?user=liushuchun&repo=wechatcmd&style=flat&background=1081C1)](http://github.com/liushuchun/wechatcmd/fork) ![Build](https://img.shields.io/appveyor/ci/gruntjs/grunt.svg)
=================
## 微信命令行版本
开发这个命令行版本,是为了更加方便聊天哦,对于习惯用命令行的同学,可能会比较喜欢
开发这个命令行版本,一是为了熟悉微信的接口,二是方便咱们习惯命令行的同学

项目还是半成品,欢迎有兴趣的朋友一起加入开发
项目还是远未完成,热烈欢迎有兴趣的朋友一起加入开发

有什么建议可以提pr。谢谢。
有什么建议可以提issue。谢谢,也欢迎直接提PR


### 功能特性
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func main() {
logger.Fatalf("拉取联系人失败:%v\n", err)
return
}

if err := wechat.TestCheck(); err != nil {
logger.Fatalf("检查状态失败:%v\n", err)
return
Expand Down
5 changes: 4 additions & 1 deletion ui/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ func (l *Layout) displayMsgIn() {
case msg = <-l.chatIn:
fmt.Println("聊天消息进入:%v", msg)
case msg = <-l.msgIn:
fmt.Println("各类消息进入:%v", msg)
text := msg.String()
l.msgInBox.Text = l.msgInBox.Text + text
ui.Render(l.msgInBox)
case <-l.closeChan:
break
}

}
return
}

func (l *Layout) NextUser() {
Expand Down
21 changes: 13 additions & 8 deletions wechat/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ type Wechat struct {
SyncHost string
SyncKey SyncKey
Users []string
InitContactList []User //谈话的人
MemberList []*Member //
ContactList []User //好友
GroupList []string //群
GroupMemberList []string //群友
PublicUserList []User //公众号
SpecialUserList []User //特殊账号
AutoReplyMode bool //default false
InitContactList []User //谈话的人
MemberList []Member //
ContactList []Member //好友
GroupList []string //群
GroupMemberList []string //群友
PublicUserList []Member //公众号
SpecialUserList []Member //特殊账号
AutoReplyMode bool //default false
AutoOpen bool
Interactive bool
TotalMember int
Expand All @@ -79,6 +79,8 @@ type Wechat struct {
Client *http.Client
Request *BaseRequest
Log *log.Logger
MemberMap map[string]Member
ChatSet []string
}

func NewWechat(logger *log.Logger) *Wechat {
Expand Down Expand Up @@ -110,6 +112,7 @@ func NewWechat(logger *log.Logger) *Wechat {
SaveFolder: path.Join(root, "saved"),
QrImagePath: filepath.Join(root, "qr.jpg"),
Log: logger,
MemberMap: make(map[string]Member),
}

}
Expand Down Expand Up @@ -287,6 +290,8 @@ func (w *Wechat) Login() (err error) {
for _, contact := range newResp.ContactList {
w.InitContactList = append(w.InitContactList, contact)
}

w.ChatSet = strings.Split(newResp.ChatSet, ",")
w.User = newResp.User
w.SyncKey = newResp.SyncKey
w.SyncKeyStr = ""
Expand Down
85 changes: 77 additions & 8 deletions wechat/wechat_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"regexp"
"strconv"
"strings"
"time"
)

Expand All @@ -21,16 +22,57 @@ func (w *Wechat) GetContacts() (err error) {
if err := w.Send(apiURI, nil, resp); err != nil {
return err
}
w.MemberList, w.TotalMember = make([]*Member, 0, resp.MemberCount/5*2), resp.MemberCount
for _, member := range resp.MemberList {
if member.IsNormal(w.User.UserName) {
w.MemberList = append(w.MemberList, member)

w.MemberList = resp.MemberList
w.TotalMember = resp.MemberCount
for _, member := range w.MemberList {
w.MemberMap[member.UserName] = member
}

for _, user := range w.ChatSet {
exist := false
for _, initUser := range w.InitContactList {
if user == initUser.UserName {
exist = true
break
}
}
if !exist {
value, ok := w.MemberMap[user]
if ok {
contact := User{
UserName: value.UserName,
NickName: value.NickName,
Signature: value.Signature,
}

w.InitContactList = append(w.InitContactList, contact)
}
}

}

return
}

func (w *Wechat) getSyncMsg() (msgs []Message, err error) {
func (w *Wechat) getRoomMembers(roomID string) (map[string]string, error) {
url := fmt.Sprintf("%s/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s", w.BaseUri, w.GetUnixTime(), w.Request.PassTicket)
params := make(map[string]interface{})
params["BaseRequest"] = *w.Request
params["Count"] = 1
params["List"] = []map[string]string{}
l := []map[string]string{}
params["List"] = append(l, map[string]string{
"UserName": roomID,
"ChatRoomId": "",
})
members := []string{}
stats := make(map[string]string)
fmt.Println(members, stats, url)
return nil, nil
}

func (w *Wechat) getSyncMsg() (msgs []interface{}, err error) {
name := "webwxsync"
syncResp := new(SyncResp)
url := fmt.Sprintf("%s/%s?sid=%s&pass_ticket=%s&skey=%s", w.BaseUri, name, w.Request.Wxsid, w.Request.PassTicket, w.Request.Skey)
Expand Down Expand Up @@ -88,8 +130,33 @@ func (w *Wechat) SyncDaemon(msgIn chan Message) {
w.Log.Printf("w.getSyncMsg() error:%+v\n", err)
}

for _, msg := range msgs {
msgIn <- msg
for _, m := range msgs {
msg := Message{}
msgType := m.(map[string]interface{})["MsgType"].(float64)
msg.MsgType = int(msgType)
msg.FromUserName = m.(map[string]interface{})["FromUserName"].(string)
if nickNameFrom, ok := w.MemberMap[msg.FromUserName]; ok {
msg.FromUserNickName = nickNameFrom.NickName
}

msg.ToUserName = m.(map[string]interface{})["ToUserName"].(string)
if nickNameTo, ok := w.MemberMap[msg.ToUserName]; ok {
msg.ToUserNickName = nickNameTo.NickName
}

msg.Content = m.(map[string]interface{})["Content"].(string)
msg.Content = strings.Replace(msg.Content, "&lt;", "<", -1)
msg.Content = strings.Replace(msg.Content, "&gt;", ">", -1)
msg.Content = strings.Replace(msg.Content, " ", " ", 1)
if msg.MsgType == 1 {
msgIn <- msg
if msg.FromUserName[:2] == "@@" {
//群消息,暂时不处理
} else {

}
}

}
case 4: //通讯录更新
w.GetContacts()
Expand All @@ -102,10 +169,12 @@ func (w *Wechat) SyncDaemon(msgIn chan Message) {
}
default:
w.Log.Printf("the resp:%+v", resp)
time.Sleep(time.Second * 4)

continue
}

time.Sleep(time.Second * 4)

}
}

Expand Down
57 changes: 29 additions & 28 deletions wechat/wechat_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strings"
"time"
)

type Config struct {
Expand Down Expand Up @@ -40,10 +39,12 @@ type Message struct {
MediaId string
FileName string
FileSize string
FromUserNickName string
ToUserNickName string
}

func (m Message) String() string {
return time.Now().Format("2006-01-02 15:04:05") + " " + m.FromUserName + "->" + m.ToUserName + ":\n " + m.Content + "\n"
return m.FromUserNickName + "->" + m.ToUserNickName + ":\n " + m.Content + "\n"
}

type AppInfo struct {
Expand Down Expand Up @@ -129,33 +130,11 @@ func (r *Response) Error() error {
return fmt.Errorf("message:[%s]", r.BaseResponse.ErrMsg)
}

type User struct {
UserName string `json:"UserName"`
Uin int `json:"Uin"`
NickName string `json:"NickName"`
HeadImgUrl string `json:"HeadImgUrl" xml:""`
RemarkName string `json:"RemarkName" xml:""`
PYInitial string `json:"PYInitial" xml:""`
PYQuanPin string `json:"PYQuanPin" xml:""`
RemarkPYInitial string `json:"RemarkPYInitial" xml:""`
RemarkPYQuanPin string `json:"RemarkPYQuanPin" xml:""`
HideInputBarFlag int `json:"HideInputBarFlag" xml:""`
StarFriend int `json:"StarFriend" xml:""`
Sex int `json:"Sex" xml:""`
Signature string `json:"Signature" xml:""`
AppAccountFlag int `json:"AppAccountFlag" xml:""`
VerifyFlag int `json:"VerifyFlag" xml:""`
ContactFlag int `json:"ContactFlag" xml:""`
WebWxPluginSwitch int `json:"WebWxPluginSwitch" xml:""`
HeadImgFlag int `json:"HeadImgFlag" xml:""`
SnsFlag int `json:"SnsFlag" xml:""`
}

type MemberResp struct {
Response
MemberCount int
ChatRoomName string
MemberList []*Member
MemberList []Member
Seq int
}

Expand All @@ -175,6 +154,28 @@ func (this *Member) IsSpecail() bool {
return false
}

type User struct {
UserName string `json:"UserName"`
Uin int `json:"Uin"`
NickName string `json:"NickName"`
HeadImgUrl string `json:"HeadImgUrl" xml:""`
RemarkName string `json:"RemarkName" xml:""`
PYInitial string `json:"PYInitial" xml:""`
PYQuanPin string `json:"PYQuanPin" xml:""`
RemarkPYInitial string `json:"RemarkPYInitial" xml:""`
RemarkPYQuanPin string `json:"RemarkPYQuanPin" xml:""`
HideInputBarFlag int `json:"HideInputBarFlag" xml:""`
StarFriend int `json:"StarFriend" xml:""`
Sex int `json:"Sex" xml:""`
Signature string `json:"Signature" xml:""`
AppAccountFlag int `json:"AppAccountFlag" xml:""`
VerifyFlag int `json:"VerifyFlag" xml:""`
ContactFlag int `json:"ContactFlag" xml:""`
WebWxPluginSwitch int `json:"WebWxPluginSwitch" xml:""`
HeadImgFlag int `json:"HeadImgFlag" xml:""`
SnsFlag int `json:"SnsFlag" xml:""`
}

type Member struct {
Uin int
UserName string
Expand Down Expand Up @@ -229,9 +230,9 @@ type SyncParams struct {

type SyncResp struct {
Response
SyncKey SyncKey `json:"SyncKey"`
ContinueFlag int `json:"ContinueFlag"`
AddMsgList []Message `json:"AddMsgList"`
SyncKey SyncKey `json:"SyncKey"`
ContinueFlag int `json:"ContinueFlag"`
AddMsgList []interface{} `json:"AddMsgList"`
}

type NotifyResp struct {
Expand Down

0 comments on commit 069738e

Please sign in to comment.