The Go implementation of the WeChat web version API simulates the login/contact/message sending and receiving functions of the WeChat web version. It can completely take over the messages received by WeChat and customize your own sending content.
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/net.git
go get -u -v github.com/songtianyi/wechat-go
cd $GOPATH/src/github.com/songtianyi/wechat-go
go get ./...
go build examples/linux/terminal_bot.go
./terminal_bot
The Windows version needs to be generated using cross-compilation on non-windows systems, and then run under Windows after generation.
GOOS=windows GOARCH=amd64 go build examples/windows/windows_bot.go
./windows_bot.exe
扫码图片需要用软件打开,路径在输出日志内.
go-aida
package main
import (
"github.com/songtianyi/rrframework/logs"
"github.com/songtianyi/wechat-go/plugins/wxweb/faceplusplus"
"github.com/songtianyi/wechat-go/plugins/wxweb/gifer"
"github.com/songtianyi/wechat-go/plugins/wxweb/replier"
"github.com/songtianyi/wechat-go/plugins/wxweb/switcher"
"github.com/songtianyi/wechat-go/wxweb"
)
func main () {
// 创建session, 一个session对应一个机器人
// 二维码显示在终端上
session , err := wxweb . CreateSession ( nil , nil , wxweb . TERMINAL_MODE )
if err != nil {
logs . Error ( err )
return
}
// 注册插件, 所有插件默认是开启的
faceplusplus . Register ( session )
replier . Register ( session )
switcher . Register ( session )
gifer . Register ( session )
// 你也可以自己选择关闭插件里的handler(消息处理器)
session . HandlerRegister . DisableByName ( "faceplusplus" )
// 登录并接收消息
if err := session . LoginAndServe ( false ); err != nil {
logs . Error ( "session exit, %s" , err )
}
}A plugin for managing plugins
#关闭某个插件, 在微信聊天窗口输入
disable faceplusplus
#开启某个插件, 在微信聊天窗口输入
enable faceplusplus
#查看所有插件信息, 在微信聊天窗口输入
dump
Do facial recognition for received pictures, return gender and age
Use the received text messages as keywords to search for gifs and return the gif graph. Note that the returned gif may have a large scale, such as the text messages contain keywords such as "polluted".
Automatically respond to received text/picture messages and reply to fixed text messages
Randomly get a beautiful picture and enter it in the chat window
美女
Get a joke and enter it in the chat window
笑话
Message withdrawal plug-in, and text messages sent by the mobile phone will be automatically withdrawn after 3 seconds. The messages sent by the robot need to write the withdrawal logic in the corresponding plug-in.
System prompts for processing message withdrawal/red envelopes, etc.
For forwarding messages across groups, just modify the full spelling of the group name in the plug-in.
Chinese-English translation plug-in, based on Youdao Translation API
Automatically accept friend requests, and can be filtered according to conditions
Example of automatic distribution of resources (House of Cards)
Configuration management plug-in settings, enter in the chat window
set config key value
Check the configuration and enter it in the chat window
get config key
Using configuration in code
import "github.com/songtianyi/wechat-go/kv"
func demo () {
kv . KVStorageInstance . Set ( "key" , "value" )
v := kv . KVStorageInstance . Get ( "key" )
if v == nil {
return
}
// v.(string) etc.
}Two principles of custom plug-ins
Plugin Example
package demo // 以插件名命令包名
import (
"github.com/songtianyi/rrframework/logs" // 导入日志包
"github.com/songtianyi/wechat-go/wxweb" // 导入协议包
)
// 必须有的插件注册函数
// 指定session, 可以对不同用户注册不同插件
func Register ( session * wxweb. Session ) {
// 将插件注册到session
// 第一个参数: 指定消息类型, 所有该类型的消息都会被转发到此插件
// 第二个参数: 指定消息处理函数, 消息会进入此函数
// 第三个参数: 自定义插件名,不能重名,switcher插件会用到此名称
session . HandlerRegister . Add ( wxweb . MSG_TEXT , wxweb . Handler ( demo ), "textdemo" )
// 开启插件
if err := session . HandlerRegister . EnableByName ( "textdemo" ); err != nil {
logs . Error ( err )
}
}
// 消息处理函数
func demo ( session * wxweb. Session , msg * wxweb. ReceivedMessage ) {
// 可选: 可以用contact manager来过滤, 比如过滤掉没有保存到通讯录的群
// 注意,contact manager只存储了已保存到通讯录的群组
contact := session . Cm . GetContactByUserName ( msg . FromUserName )
if contact == nil {
logs . Error ( "ignore the messages from" , msg . FromUserName )
return
}
// 可选: 根据消息类型来过滤
if msg . MsgType == wxweb . MSG_IMG {
return
}
// 可选: 根据wxweb.User数据结构中的数据来过滤
if contact . PYQuanPin != "songtianyi" {
// 比如根据用户昵称的拼音全拼来过滤
return
}
// 可选: 过滤和自己无关的群组消息
if msg . IsGroup && msg . Who != session . Bot . UserName {
return
}
// 取出收到的内容
// 取text
logs . Info ( msg . Content )
//// 取img
//if b, err := session.GetImg(msg.MsgId); err == nil {
// logs.Debug(string(b))
//}
// anything
// 回复消息
// 第一个参数: 回复的内容
// 第二个参数: 机器人ID
// 第三个参数: 联系人/群组/特殊账号ID
session . SendText ( "plugin demo" , session . Bot . UserName , wxweb . RealTargetUserName ( session , msg ))
// 回复图片和gif 参见wxweb/session.go
}