chatbot

package module
v0.0.0-...-53aaeea Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 15 Imported by: 0

README

ChatBot 的 Go 版本 DEMO

接入流程和如何获取 token

点击这里查看

运行

  1. 下载代码
    git clone https://github.com/chatrbot/chatbot-go
  2. 安装 Go 依赖库 go mod download
  3. 注册 获取的一个智能机器人API的Token
  4. 替换代码中的token(如何获取我的 token)或者在命令行中添加 token 参数 修改代码: go run /example/ai/main.go
    不修改代码: go run /example/ai/main.go -token your_token -ai ai_token
  5. 就能实现一个能够只能回复的机器人

示例插件

  1. AI机器人
    该示例需要自行注册思知的AI API Token,可以在这个地址获取
    下载代码并且成功获取到ChatBot Token

    go run example/ai/main.go -token {ChatBotToken} -ai {AIToken}
    

    这时候和机器人对话,即可收到智能回复(如果是群内,需要@机器人)

  2. 复读机 复读机插件并没有实际作用。只是作为一个SDK能力的展示和实际代码示例。展示了文本、图片、视频、语音类型的消息收发能力。

Documentation

Index

Constants

View Source
const (
	MsgTypeText   = 1  // 文本消息
	MsgTypeImg    = 3  // 图片消息
	MsgTypeVoice  = 34 // 语音消息
	MsgTypeVideo  = 43 // 视频消息
	MsgTypeEmoji  = 47 // 表情动图消息
	MsgTypeApplet = 49 // 小程序
)

收到的转发消息具体分类

View Source
const (
	RoleMember = 1 + iota
	RoleAdmin
	RoleOwner
)

Variables

This section is empty.

Functions

func IsBotBeenAt

func IsBotBeenAt(msg *UserMessage) bool

IsBotBeenAt 机器人是否被@了

func IsGroupMessage

func IsGroupMessage(userName string) bool

IsGroupMessage 是否为群聊消息

func SplitAtContent

func SplitAtContent(msgContent string) string

SplitAtContent 分割包含@的消息内容 例如 @小明 你吃饭了么 其中消息体中间会有个"空格",这个可能是个特殊字符,也可能是个真的空格

Types

type BotServer

type BotServer struct {
	// contains filtered or unexported fields
}

BotServer 调用机器人http接口的服务 主要用于基本的消息发送

type ChatBot

type ChatBot struct {
	// contains filtered or unexported fields
}

机器人实例

func New

func New(host, token string) (*ChatBot, error)

New 新建一个ChatBot实例 @host WebSocket的服务端地址 @token激活后机器人给的token

func (*ChatBot) Close

func (bot *ChatBot) Close()

func (*ChatBot) DelGroupMembers

func (bot *ChatBot) DelGroupMembers(group string, members []string) ([]string, error)

DelGroupMembers 删除群成员

func (*ChatBot) DownloadEmoji

func (bot *ChatBot) DownloadEmoji(xml string) (string, error)

DownloadEmoji 下载表情或者动态图片 注意这个方法只能提取表情的下载地址不能用于直接发送 发送(转发)表情需要用拿到的xml中的md5和len字段发送,可以使用ParseEmojiXML方法来获取

func (*ChatBot) DownloadPic

func (bot *ChatBot) DownloadPic(xml string) (*DownloadImageResponse, error)

DownloadPic 下载图片

func (*ChatBot) DownloadVideo

func (bot *ChatBot) DownloadVideo(xml string) (*DownloadVideoResponse, error)

DownloadVideo 下载视频

func (*ChatBot) DownloadVoice

func (bot *ChatBot) DownloadVoice(msgID int64, xml string) (*DownloadVoiceResponse, error)

DownloadVoice 下载音频

func (*ChatBot) ParseEmojiXML

func (bot *ChatBot) ParseEmojiXML(xml string) (md5, length string, err error)

ParseEmojiXML 解析emoji表情中的md5和len字段

func (*ChatBot) Run

func (bot *ChatBot) Run()

Run 连接WebSocket服务并且开始监听

func (*ChatBot) SendEmoji

func (bot *ChatBot) SendEmoji(toUser, emojiMd5, emojiLen string) error

SendEmoji 发送表情动图 toUser 接收人微信号 emojiMd5 从收到的xml中可以解析md5字段 emojiLen 从收到的xml可以解析len字段

func (*ChatBot) SendMiniProgram

func (bot *ChatBot) SendMiniProgram(req *SendMiniProgramRequest) error

SendMiniProgram 发送小程序 toUser 接收人微信号/ID thumbUrl 缩略图地址 title 标题 des 描述 url 地址 sourceUserName 来源用户名 sourceDisplayName 来源显示名 username 用户名 appId 小程序AppId type 类型 version 版本 iconUrl 图标地址 pagePath 启动页

func (*ChatBot) SendPic

func (bot *ChatBot) SendPic(toUser, imgUrl string) error

SendPic 发送图片消息 @toUser 接收人微信号 @imgUrl 图片的网络地址

func (*ChatBot) SendText

func (bot *ChatBot) SendText(toUser, content string, atList []string) error

SendText 发送文本形式的消息 @toUser 接收人微信号,一般为机器人推送过来的消息发送人,即你自己 @content 文本内容,如果有人被@需要填写对方昵称 @atList 被@人列表,这里填写的是对方微信号

func (*ChatBot) SendVideo

func (bot *ChatBot) SendVideo(toUser, videoUrl, thumbUrl string) error

SendVideo 发送视频 @toUser 接收人微信号 @videoUrl 视频网络地址 @thumbUrl 封面缩略图地址 视频发送必须有封面图,如果需要根据视频内容截取封面 可以自行搜索ffmpeg相关的资料

func (*ChatBot) SendVoice

func (bot *ChatBot) SendVoice(toUser, url string) error

SendVoice 发送语音 @toUser 接收人微信号 @url 音频文件网络地址

func (*ChatBot) Use

func (bot *ChatBot) Use(plugin ...Plugin)

Use 添加处理消息的插件

type DelGroupRequest

type DelGroupRequest struct {
	Group      string   `json:"chatroom"`   // 群号
	MemberList []string `json:"memberList"` // 删除人员
}

删除群成员

type DelGroupResponse

type DelGroupResponse struct {
	DelMemberList []string `json:"delMemberList"`
}

type DownloadImageRequest

type DownloadImageRequest struct {
	XML string `json:"xml"`
}

下载图片

type DownloadImageResponse

type DownloadImageResponse struct {
	Content []byte `json:"content"` // 下载失败后的提示
	ImgUrl  string `json:"imgUrl"`  // 图片地址
}

type DownloadVideoRequest

type DownloadVideoRequest struct {
	XML string `json:"xml"`
}

下载视频

type DownloadVideoResponse

type DownloadVideoResponse struct {
	Content  []byte `json:"content"`  // 下载失败后的提示
	VideoUrl string `json:"videoUrl"` // 视频地址
}

type DownloadVoiceRequest

type DownloadVoiceRequest struct {
	NewMsgId int64  `json:"newMsgId"` // 服务端ID
	XML      string `json:"xml"`      // 内容xml
}

下载语音

type DownloadVoiceResponse

type DownloadVoiceResponse struct {
	Content     []byte `json:"content"`     // 下载失败后的提示
	VoiceLength int64  `json:"voiceLength"` // 语音长度
	VoiceUrl    string `json:"voiceUrl"`    // 语音地址
}

type GroupBase

type GroupBase struct {
	GroupUserName string `json:"groupUserName"`
	GroupNickName string `json:"groupNickName"`
	GroupHeadImg  string `json:"groupHeadImg"`
}

群基本信息

type GroupBotEvent

type GroupBotEvent struct {
	// 事件id
	Event GroupEvent `json:"event"`
	// 事件中文提示
	EventText string `form:"eventText"`
	// 群信息
	Group GroupBase `json:"group"`
	// 变动的群成员
	Members []MemberBase `json:"members"`
}

接收到的群内事件

type GroupEvent

type GroupEvent int
const (
	// 机器人被邀请进群
	GroupEventInvited GroupEvent = 100000 + iota
	// 机器人被踢出群
	GroupEventKicked
	// 群内有新用户加群
	GroupEventNewMember
	// 群内有用户离开
	GroupEventMemberQuit
)

type MemberBase

type MemberBase struct {
	UserName string `json:"userName"`
	NickName string `json:"nickName"`
	HeadImg  string `json:"headImg"`
}

成员基本信息

type Plugin

type Plugin interface {
	Name() string
	Do(msg *PushMessage) error
}

Plugin 机器人插件接口

type PushMessage

type PushMessage struct {
	MsgType PushMsgType     `json:"msgType"`
	Data    json.RawMessage `json:"data"`
}

收到的推送消息统一格式 根据msgType字段来区别消息类型 data为具体的消息内容

type PushMsgType

type PushMsgType int
const (
	// 机器人收到的用户信息、群信息
	// 包括文本、图片、音频等多种类型
	CusMsgTypeUser PushMsgType = 10000 + iota

	// 群事件消息
	// 包括群内成员变动、机器人加入和退出群
	CusMsgTypeGroupEvent
)

自定义的消息类型,在整个消息体的最外围,即外层的msgType字段 用于区分是普通的聊天消息还是群内事件等

type SendEmojiRequest

type SendEmojiRequest struct {
	ToUser        string `json:"toUser"`        // 发送对象
	EmojiMd5      string `json:"emojiMd5"`      // 表情md5值
	GifUrl        string `json:"gifUrl"`        // 动图地址,和md5和len互斥,不为空时候上传动图
	EmojiTotalLen int64  `json:"emojiTotalLen"` // 表情长度
}

发送表情

type SendEmojiResponse

type SendEmojiResponse struct {
	MsgId    int64  `json:"msgId"`    // 服务端消息ID
	NewMsgId int64  `json:"newMsgId"` // 服务端消息ID
	Md5      string `json:"md5"`      // 表情md5值
	TotalLen int64  `json:"totalLen"` // 表情长度
}

type SendMiniProgramRequest

type SendMiniProgramRequest struct {
	ToUser            string `json:"toUser"`            // 接收人微信号/ID
	ThumbUrl          string `json:"thumbUrl"`          //	缩略图地址
	Title             string `json:"title"`             // 标题
	Des               string `json:"des"`               // 描述
	Url               string `json:"url"`               //	地址
	SourceUserName    string `json:"sourceUserName"`    // 来源用户名
	SourceDisplayName string `json:"sourceDisplayName"` // 来源显示名
	Username          string `json:"username"`          // 用户名
	AppId             string `json:"appid"`             // 小程序 APPID
	Type              int    `json:"type"`              // 类型
	Version           int    `json:"version"`           // 版本
	IconUrl           string `json:"iconUrl"`           //  图标地址
	PagePath          string `json:"pagePath"`          // 启动页
}

发送小程序

type SendMiniProgramResponse

type SendMiniProgramResponse struct {
	ClientMsgId string `json:"clientMsgId"` // 客户端消息ID
	MsgId       int64  `json:"msgId"`       // 服务端消息ID
	NewMsgId    int64  `json:"newMsgId"`    // 服务端消息ID
}

type SendPicRequest

type SendPicRequest struct {
	ToUser string `json:"toUser"` // 发送对象
	ImgUrl string `json:"imgUrl"` // 图片地址
}

发送图片

type SendPicResponse

type SendPicResponse struct {
	ClientMsgId string `json:"clientMsgId"` // 客户端消息ID
	MsgId       int64  `json:"msgId"`       // 服务端消息ID
	NewMsgId    int64  `json:"newMsgId"`    // 服务端消息ID
}

type SendTextRequest

type SendTextRequest struct {
	ToUser  string   `json:"toUser"`  // 发送对象
	AtList  []string `json:"atList"`  // 群内at的人微信号
	Content string   `json:"content"` // 发送内容 存在at时候必须有@xxx标识,xxx为对方昵称
}

发送文本

type SendTextResponse

type SendTextResponse struct {
	CreateTime  int64 `json:"createTime"`  // 客户端时间
	ClientMsgId int64 `json:"clientMsgId"` // 客户端消息ID
	ServerTime  int64 `json:"serverTime"`  // 服务端时间
	MsgId       int64 `json:"msgId"`       // 服务端消息ID
	NewMsgId    int64 `json:"newMsgId"`    // 服务端消息ID
}

type SendVideoRequest

type SendVideoRequest struct {
	ToUser        string `json:"toUser"`        // 发送对象
	VideoUrl      string `json:"videoUrl"`      // 视频地址
	VideoThumbUrl string `json:"videoThumbUrl"` // 视频缩略图地址
}

发送视频

type SendVideoResponse

type SendVideoResponse struct {
	ClientMsgId string `json:"clientMsgId"` // 客户端消息ID
	MsgId       int64  `json:"msgId"`       // 服务端消息ID
	NewMsgId    int64  `json:"newMsgId"`    // 服务端消息ID
}

type SendVoiceRequest

type SendVoiceRequest struct {
	ToUser  string `json:"toUser"`  // 发送对象
	SilkUrl string `json:"silkUrl"` // 语音链接
}

发送语音

type SendVoiceResponse

type SendVoiceResponse struct {
	ClientMsgId string `json:"clientMsgId"` // 客户端消息ID
	MsgId       int64  `json:"msgId"`       // 服务端消息ID
	NewMsgId    int64  `json:"newMsgId"`    // 服务端消息ID
}

type UserMessage

type UserMessage struct {
	NewMsgID       int64    `json:"newMsgId"` // 消息id,在下载语音时候会用到
	FromUser       string   `json:"fromUser"`
	AtList         []string `json:"atList"`
	CreateTime     int      `json:"createTime"`
	PushContent    string   `json:"pushContent"`
	ClientUserName string   `json:"clientUserName"`
	ToUser         string   `json:"toUser"`
	ImgBuf         string   `json:"imgBuf"`
	MsgType        int      `json:"msgType"`
	Content        string   `json:"content"`
	MsgSource      string   `json:"msgSource"`
	// 群内消息才会用到的字段
	WhoAtBot            string `json:"whoAtBot"`            // 谁@的机器人,微信昵称,方便客户端机器人反向@
	GroupMember         string `json:"groupMember"`         // 如果是群聊消息,则为分离content后的发言人微信号
	GroupMemberNickname string `json:"groupMemberNickname"` // 群内说话人的微信昵称
	GroupMemberRole     int8   `json:"groupMemberRole"`     // 用户在群内身份,1成员,2管理员,3群主
	GroupContent        string `json:"groupContent"`        // 如果是群消息,则为分离content后的群消息内容
}

接收到的转发消息

func (*UserMessage) IsAdmin

func (m *UserMessage) IsAdmin() bool

func (*UserMessage) IsGroupOwner

func (m *UserMessage) IsGroupOwner() bool

type WsServer

type WsServer struct {
	// contains filtered or unexported fields
}

监听WebSocket的推送消息

func (*WsServer) Close

func (ws *WsServer) Close()

关闭WebSocket连接

func (*WsServer) ReceiveCallbackMessage

func (ws *WsServer) ReceiveCallbackMessage()

ReceiveCallbackMessage 开始监听服务端消息和调用插件

Directories

Path Synopsis
example
ai
* 这是一个接入思知API让机器人成为AI机器人的例子 里面需要的思知token需要自行注册获取 https://www.ownthink.com/
* 这是一个接入思知API让机器人成为AI机器人的例子 里面需要的思知token需要自行注册获取 https://www.ownthink.com/
repeater
一个单纯的复读机插件演示 没有实际意义,只是用于展示机器人sdk的消息收发能力和具体实现方式
一个单纯的复读机插件演示 没有实际意义,只是用于展示机器人sdk的消息收发能力和具体实现方式

Jump to

Keyboard shortcuts

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