#开源项目#wechat-go 微信机器人/web 协议

2017-04-21 13:34:41 +08:00
 spice630

wechat-go

https://github.com/songtianyi/wechat-go

go 版本 wechat web api , 十分欢迎 gopher 参与开发更多有趣实用的插件。同时求一个前端,写两个页面给普通用户使用。

Install

go get -u -v github.com/songtianyi/wechat-go

golang.org/x dep install

mkdir $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/net.git
git clone https://github.com/golang/text.git

Demo project

go-aida

Example code

Create your own chatbot
package main

import (
	"github.com/songtianyi/rrframework/logs"
	"github.com/songtianyi/wechat-go/plugins/faceplusplus"
	"github.com/songtianyi/wechat-go/wxweb"
	"github.com/songtianyi/wechat-go/plugins/wxweb/gifer"
	"github.com/songtianyi/wechat-go/plugins/wxweb/replier"
	"github.com/songtianyi/wechat-go/plugins/wxweb/switcher"
)

func main() {
	// create session
	session, err := wxweb.CreateSession(nil, nil, wxweb.TERMINAL_MODE)
	if err != nil {
		logs.Error(err)
		return
	}

	// add plugins for this session, they are disabled by default
	faceplusplus.Register(session)
	replier.Register(session)
	switcher.Register(session)
	gifer.Register(session)

	// enable plugin
	session.HandlerRegister.EnableByName("switcher")
	session.HandlerRegister.EnableByName("faceplusplus")

	if err := session.LoginAndServe(); err != nil {
		logs.Error("session exit, %s", err)
	}
}

Plugins

switcher

一个管理插件的插件

#关闭某个插件, 在微信聊天窗口输入
disable faceplusplus
#开启某个插件, 在微信聊天窗口输入
enable faceplusplus
#查看所有插件信息, 在微信聊天窗口输入
dump
faceplusplus

对收到的图片做面部识别,返回性别和年龄

gifer

以收到的文字消息为关键字做 gif 搜索,返回 gif 图, 注意返回的 gif 可能尺度较大,比如文字消息中包含“污”等关键词。

replier

对收到的文字 /图片消息,做自动应答,回复固定文字消息

laosj

随机获取一张美女图片, 在聊天窗口输入

美女
joker

获取一则笑话, 在聊天窗口输入

笑话
revoker

消息撤回插件, 3s 后自动撤回手机端所发的文本消息. 机器人发出的消息需要自己在插件里写撤回逻辑.

制作自己插件

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")

	// 可以多个个消息类型使用同一个处理函数,也可以分开
	session.HandlerRegister.Add(wxweb.MSG_IMG, wxweb.Handler(demo), "imgdemo")
}

// 消息处理函数
func demo(session *wxweb.Session, msg *wxweb.ReceivedMessage) {

	// 可选:避免此插件对所有群 /联系人生效 可以用 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

}

Show

13225 次点击
所在节点    分享创造
94 条回复
only0jac
2017-05-24 22:50:34 +08:00
@spice630 我在 Windows 下交叉编译 Linux 的,Windows 直接生成 exe 不行?
spice630
2017-05-24 23:18:23 +08:00
@only0jac
嗯,原因还不明确。
spice630
2017-05-24 23:28:15 +08:00
@only0jac
等我搞一套好用的开发环境再去 debug
spice630
2017-05-27 13:55:14 +08:00
@only0jac
windows 下推荐使用 cmder,可以用 TERMINAL_MODE
spice630
2017-05-27 18:24:45 +08:00
https://www.zhihu.com/lives/846360223609413632
https://github.com/rixingyike/goeve-wechat-robot
都有人拿我的项目去变现了,感谢认可,也很鼓励大家这么做,这样这个项目也更有意义。但是源码中的 license 请保留,不单单是为了我自己,也为后续的贡献者。
spice630
2017-06-06 22:15:39 +08:00
wechat-go 的示例项目,目标是可以方便用户在页面上扫码登录并管理插件
https://github.com/songtianyi/go-aida
myself659
2017-06-09 13:13:19 +08:00
大赞
spice630
2017-06-12 20:37:32 +08:00
go-aida restful api 开发完成,这周页面应该可以好。
spice630
2017-06-16 13:53:21 +08:00
update@2017.06.16
新增插件 verify
自动接受好友请求
spice630
2017-06-16 18:30:42 +08:00
新增插件 share
资源自动分发示例,纸牌屋第五季!
spice630
2017-07-29 00:08:04 +08:00
go-aida 的坑埋了
https://github.com/songtianyi/go-aida
界面很简陋,很丑,先将就用。
spice630
2017-09-21 13:42:32 +08:00
ibreaker
2019-01-19 18:56:53 +08:00
@spice630 交流群打不开了
useben
2019-06-19 17:32:07 +08:00
之前用给一个 py 的做过,重构到 go 也不错

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/356399

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX