wechat

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2019 License: Apache-2.0 Imports: 29 Imported by: 0

README

wechat

Awaken your wechat bot.

INSTALLATION

go get github.com/KevinGong2013/ggbot/wechat

Basic Usage

import "github.com/KevinGong2013/wechat"

// awaken a bot
bot, _ := wechat.AwakenNewBot(nil)
bot.Go() // begin handle everything

Login State

bot.Handle(`/login`, func(arg2 wechat.Event) {
	isSuccess := arg2.Data.(int) == 1
	if isSuccess {
		fmt.Println(`login Success`)
	} else {
		fmt.Println(`login Failed`)
	}
})

Contact

Get
// all contacts

bot.AllContacts()

// get contact by `UserName`
contact, _ := bot.ContactByUserName(UserName)

// get contact by `NickName`
contacts, _ := bot.ContactsByNickName(NickName)
Change
// handle contact change event
bot.Handle(`/contact`, func(evt wechat.Event) {
	data := evt.Data.(wechat.EventContactData)
	fmt.Println(`contact change event` + data.GGID)
})

Message

Send
to := `filehelper`
// text message
bot.SendTextMsg(`Text`, to)
// video message
bot.SendFile(`testResource/test.mov`, to)
// image message
bot.SendFile(`testResource/test.png`, to)
// emoticon message
bot.SendFile(`testResource/test.gif`, to)
// file message
bot.SendFile(`testResource/test.txt`, to)
bot.SendFile(`testResource/test.mp3`, to)
Receive
// all solo msg
bot.Handle(`/msg/solo`, func(evt wechat.Event) {
	data := evt.Data.(wechat.EventMsgData)
	fmt.Println(`/msg/solo/` + data.Content)
})

// all group msg
bot.Handle(`/msg/group`, func(evt wechat.Event) {
	data := evt.Data.(wechat.EventMsgData)
	fmt.Println(`/msg/group/` + data.Content)
})

Convenice

bot.AddTimer(5 * time.Second)
bot.Handle(`/timer/5s`, func(arg2 wechat.Event) {
	data := arg2.Data.(wechat.EventTimerData)
	if bot.IsLogin {
		bot.SendTextMsg(fmt.Sprintf(`%v times`, data.Count), `filehelper`)
	}
})

bot.AddTiming(`9:00`)
bot.Handle(`/timing/9:00`, func(arg2 wechat.Event) {
	// data := arg2.Data.(wechat.EventTimingtData)
	bot.SendTextMsg(`9:00`, `filehelper`)
})

Documentation

Index

Constants

View Source
const (
	// Offical 公众号 ...
	Offical = 0
	// Friend 好友 ...
	Friend = 1
	// Group 群组 ...
	Group = 2
	// Member 群组成员 ...
	Member = 3
	// FriendAndMember 即是好友也是群成员 ...
	FriendAndMember = 4
)
View Source
const (
	// Delete 删除联系人
	Delete = 0
	// Modify 有人修改了自己的信息
	Modify = 1
)
View Source
const (
	// 男
	Male = iota
	// 女
	Female
	// 未知
	Unknow
	// 任何
	Any
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseRequest

type BaseRequest struct {
	XMLName xml.Name `xml:"error" json:"-"`

	Ret        int    `xml:"ret" json:"-"`
	Message    string `xml:"message" json:"-"`
	Wxsid      string `xml:"wxsid" json:"Sid"`
	Skey       string `xml:"skey"`
	DeviceID   string `xml:"-"`
	Wxuin      int64  `xml:"wxuin" json:"Uin"`
	PassTicket string `xml:"pass_ticket" json:"-"`
}

BaseRequest is a base for all wx api request.

type BaseResponse

type BaseResponse struct {
	Ret    int
	ErrMsg string
}

BaseResponse for all api resp.

type Caller

type Caller interface {
	IsSuccess() bool
	Error() error
}

Caller is a interface, All response need implement this.

type Configure

type Configure struct {
	Processor         UUIDProcessor
	Debug             bool
	CachePath         string
	UniqueGroupMember bool
	// contains filtered or unexported fields
}

Configure ...

func DefaultConfigure

func DefaultConfigure() *Configure

DefaultConfigure create default configuration

type Contact

type Contact struct {
	UserName        string
	NickName        string
	HeadImgURL      string `json:"HeadImgUrl"`
	HeadHash        string
	RemarkName      string
	DisplayName     string
	StarFriend      float64
	Sex             float64
	Signature       string
	VerifyFlag      float64
	ContactFlag     float64
	HeadImgFlag     float64
	Province        string
	City            string
	Alias           string
	EncryChatRoomID string `json:"EncryChatRoomId"`
	Type            int
	MemberList      []*Contact
}

Contact is wx Account struct

func (*Contact) To

func (contact *Contact) To() string

To is contact's ID can be used in msg struct

type CountedContent

type CountedContent struct {
	Count   int
	Content []map[string]interface{}
}

CountedContent is a Wrappered for data struct from wx server

type Event

type Event struct {
	Type string
	Path string
	From string
	To   string
	Data interface{}
	Time int64
}

Event ...

type EventContactData

type EventContactData struct {
	ChangeType int
	Contact    Contact
}

EventContactData 通讯录中删人 或者有人修改资料的时候

type EventMsgData

type EventMsgData struct {
	IsGroupMsg       bool
	IsMediaMsg       bool
	IsSendedByMySelf bool
	MsgType          int64
	AtMe             bool
	MediaURL         string
	Content          string
	FromUserName     string
	SenderUserName   string
	ToUserName       string
	OriginalMsg      map[string]interface{}
}

EventMsgData 新消息

type EventTimerData

type EventTimerData struct {
	Duration time.Duration
	Count    uint64
}

EventTimerData ...

type EventTimingtData

type EventTimingtData struct {
	Count uint64
}

EventTimingData ...

type Msg

type Msg interface {
	Path() string
	To() string
	Content() map[string]interface{}

	Description() string
}

Msg implement this interface, can added addition send by wechat

type Response

type Response struct {
	BaseResponse *BaseResponse
}

Response is a wrapper.

func (*Response) Error

func (response *Response) Error() error

response's error msg.

func (*Response) IsSuccess

func (response *Response) IsSuccess() bool

IsSuccess flag this request is success or failed.

type UUIDProcessor

type UUIDProcessor interface {
	ProcessUUID(uuid string) error
	UUIDDidConfirm(err error)
}

UUIDProcessor scan this uuid

type WeChat

type WeChat struct {
	Client      *http.Client
	BaseURL     string
	BaseRequest *BaseRequest
	MySelf      Contact
	IsLogin     bool
	// contains filtered or unexported fields
}

WeChat container a default http client and base request.

func NewBot

func NewBot(conf *Configure) (*WeChat, error)

NewBot is start point for wx bot.

func (*WeChat) AddTimer

func (wechat *WeChat) AddTimer(du time.Duration)

AddTimer ..

func (*WeChat) AddTiming

func (wechat *WeChat) AddTiming(hm string)

AddTiming ...

func (*WeChat) AllContacts

func (wechat *WeChat) AllContacts() []*Contact

AllContacts ...

func (*WeChat) ContactByUserName

func (wechat *WeChat) ContactByUserName(un string) *Contact

ContactByUserName ...

func (*WeChat) CookieDataTicket

func (wechat *WeChat) CookieDataTicket() string

CookieDataTicket ...

func (*WeChat) DownloadMedia

func (wechat *WeChat) DownloadMedia(url string, localPath string) (string, error)

DownloadMedia use to download a voice or immage msg

func (*WeChat) Execute

func (wechat *WeChat) Execute(path string, body io.Reader, call Caller) error

Execute a http request by default http client.

func (*WeChat) ExecuteRequest

func (wechat *WeChat) ExecuteRequest(req *http.Request, call Caller) error

ExecuteRequest is designed for perform http request

func (*WeChat) ForceUpdateGroup

func (wechat *WeChat) ForceUpdateGroup(groupUserName string)

ForceUpdateGroup update group information

func (*WeChat) GetContactHeadImg

func (wechat *WeChat) GetContactHeadImg(c *Contact) ([]byte, error)

GetContactHeadImg ...

func (*WeChat) Go

func (wechat *WeChat) Go()

Go 皮皮虾我们走

func (*WeChat) Handle

func (wechat *WeChat) Handle(path string, handler func(Event))

Handle 处理消息,联系人,登录态 等等 所有东西

func (*WeChat) Hook

func (wechat *WeChat) Hook(f func(Event))

Hook modify event on fly

func (*WeChat) MembersOfGroup

func (wechat *WeChat) MembersOfGroup(groupUserName string) ([]*Contact, error)

MembersOfGroup ..返回群中所有的成员

func (*WeChat) PassTicketKV

func (wechat *WeChat) PassTicketKV() string

PassTicketKV return a string like `pass_ticket=1234s`

func (*WeChat) ResetHandlers

func (wechat *WeChat) ResetHandlers()

ResetHandlers remove all regeisted handler

func (*WeChat) SearchContact

func (wechat *WeChat) SearchContact(nickName string, city string, sex int, contactType int) ([]*Contact, error)

ContactsByNickName search contact with nick name

func (*WeChat) SendFile

func (wechat *WeChat) SendFile(path, to string) error

SendFile is desined to send contain attachment Message to group or contact. path must exit in local file system.

func (*WeChat) SendMsg

func (wechat *WeChat) SendMsg(message Msg) error

SendMsg send Message to group or contact

func (*WeChat) SendTextMsg

func (wechat *WeChat) SendTextMsg(msg, to string) error

SendTextMsg send text message

func (*WeChat) SkeyKV

func (wechat *WeChat) SkeyKV() string

SkeyKV return a string like `skey=1234s`

func (*WeChat) Stop

func (wechat *WeChat) Stop()

Stop 皮皮虾快停下

func (*WeChat) SyncContact

func (wechat *WeChat) SyncContact() error

SyncContact with Wechat server.

func (*WeChat) UpdateGroupIfNeeded

func (wechat *WeChat) UpdateGroupIfNeeded(groupID string)

UpdateGroupIfNeeded ...

func (*WeChat) UploadMedia

func (wechat *WeChat) UploadMedia(buf []byte, kind types.Type, info os.FileInfo, to string) (string, error)

UploadMedia is a convernice method to upload attachment to wx cdn.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL