wechat/go 微信web协议/微信机器人 wechat/go

songtianyi2017-04-21 07:36:05 • 9822 次点击    
这是一个分享于 2017-04-21 07:36:05 的项目,其中的信息可能已经有所发展或是发生改变。

wechat-go

go version wechat web api

  • 支持多用户(多开)
  • 防掉线
  • 功能以插件的形式提供,可以根据用户(比如付费情况)选择加载或者不加载某插件
  • 对于加载的插件, 用户可以通过微信动态开启/关闭.
  • 目前已提供头像识别, gif搜索, 笑话大全, 阅后即焚等多个有趣插件
  • 插件编写简单, 二次开发极为方便
  • 可以发送图片/文字/gif/视频/表情等多种消息

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

example

go version wechat web api and message framework for building wechat robotRead More

Latest commit to the master branch on 7-13-2022
Download as zip
授权协议:
MIT
开发语言:
go/golang 查看源码»
操作系统:
All
9822 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传