<commit_before>package xiaoice

import wx "github.com/KevinGong2013/ggbot/wechat"
import "sync"

import log "github.com/Sirupsen/logrus"

var logger = log.WithFields(log.Fields{
	"module": "xiaoice",
})

// Brain ...
type Brain struct {
	sync.Mutex
	wx             *wx.WeChat
	xiaoice        *wx.Contact
	waittingReplay []string
}

// NewBrain ...
func NewBrain() *Brain {
	return &Brain{waittingReplay: []string{}}
}

// WechatDidLogin ...
func (b *Brain) WechatDidLogin(wechat *wx.WeChat) {
	b.wx = wechat
	b.xiaoice, _ = wechat.ContactByNickName(`小冰`)
}

// WechatDidLogout ...
func (b *Brain) WechatDidLogout(wechat *wx.WeChat) {
}

// MapMsgs ...
func (b *Brain) MapMsgs(msg *wx.CountedContent) {
	for _, m := range msg.Content {

		msgType, _ := m[`MsgType`].(float64)
		if msgType != 1 && msgType != 3 && msgType != 47 { // 目前只回复文字/图片/表情/消息
			m[`needXiaoiceResponse`] = false
			continue
		}

		isSendByMySelf, _ := m[`IsSendByMySelf`].(bool)
		if isSendByMySelf {
			continue
		}
		from, _ := m[`FromUserName`].(string)
		contact, err := b.wx.ContactByUserName(from)
		if err != nil {
			m[`needXiaoiceResponse`] = false
			continue
		}

		switch contact.Type {
		case wx.ContactTypeFriend:
			m[`needXiaoiceResponse`] = true
			m[`xiaoice_info`] = m[`Content`]
			m[`xiaoice_to`] = m[`FromUserName`]
		case wx.ContactTypeOfficial:
			if b.xiaoice.NickName == contact.NickName {
				len := len(b.waittingReplay)
				if len > 0 {
					b.Lock()
					defer b.Unlock()
					m[`isXiaoiceReplay`] = true
					m[`ReplayUserName`] = b.waittingReplay[len-1]
					m[`localFileId`] = m[`MsgId`]
					b.waittingReplay = b.waittingReplay[:len-1]
				}
			}
			m[`needXiaoiceResponse`] = false
		case wx.ContactTypeGroup:
			m[`needXiaoiceResponse`] = true
			m[`xiaoice_info`] = m[`Content`]
			m[`xiaoice_to`] = m[`FromUserName`]
		}
	}
	return
}<commit_msg>Added debug log for `xiaoice`<commit_after>package xiaoice

import wx "github.com/KevinGong2013/ggbot/wechat"
import "sync"

import log "github.com/Sirupsen/logrus"

var logger = log.WithFields(log.Fields{
	"module": "xiaoice",
})

// Brain ...
type Brain struct {
	sync.Mutex
	wx             *wx.WeChat
	xiaoice        *wx.Contact
	waittingReplay []string
}

// NewBrain ...
func NewBrain() *Brain {
	return &Brain{waittingReplay: []string{}}
}

// WechatDidLogin ...
func (b *Brain) WechatDidLogin(wechat *wx.WeChat) {
	b.wx = wechat
	b.xiaoice, _ = wechat.ContactByNickName(`小冰`)
}

// WechatDidLogout ...
func (b *Brain) WechatDidLogout(wechat *wx.WeChat) {
}

// MapMsgs ...
func (b *Brain) MapMsgs(msg *wx.CountedContent) {
	for _, m := range msg.Content {

		msgType, _ := m[`MsgType`].(float64)
		if msgType != 1 && msgType != 3 && msgType != 47 { // 目前只回复文字/图片/表情/消息
			m[`needXiaoiceResponse`] = false
			continue
		}

		isSendByMySelf, _ := m[`IsSendByMySelf`].(bool)
		if isSendByMySelf {
			continue
		}
		from, _ := m[`FromUserName`].(string)
		contact, err := b.wx.ContactByUserName(from)
		if err != nil {
			m[`needXiaoiceResponse`] = false
			logger.Error(err)
			continue
		}

		switch contact.Type {
		case wx.ContactTypeFriend:
			m[`needXiaoiceResponse`] = true
			m[`xiaoice_info`] = m[`Content`]
			m[`xiaoice_to`] = m[`FromUserName`]
		case wx.ContactTypeOfficial:
			if b.xiaoice.NickName == contact.NickName {
				len := len(b.waittingReplay)
				if len > 0 {
					b.Lock()
					defer b.Unlock()
					m[`isXiaoiceReplay`] = true
					m[`ReplayUserName`] = b.waittingReplay[len-1]
					m[`localFileId`] = m[`MsgId`]
					b.waittingReplay = b.waittingReplay[:len-1]
				} else {
					logger.Warnf(`xiaoice replay %s`, m)
				}
			} else {
				logger.Warn(`offical msg %s`, contact.NickName)
			}
			m[`needXiaoiceResponse`] = false
		case wx.ContactTypeGroup:
			m[`needXiaoiceResponse`] = true
			m[`xiaoice_info`] = m[`Content`]
			m[`xiaoice_to`] = m[`FromUserName`]
		}
	}
}
{'fraction_non_alphanumeric': 0.16174183514774496, 'fraction_numerical': 0.00673924313115604, 'mean_word_length': 3.3408323959505064, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4087014', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1595, 'n_words': 274}