chatbot

package module
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: Apache-2.0 Imports: 23 Imported by: 2

README

chatbot

概要

golang的chatbot框架。
特性如下:

  • 支持多聊天工具,现在支持telegram和QQ(coolq)。
  • 分布式弹性架构,主进程和各聊天工具对接进程彻底分离,用户管理进程等也可以独立配置,各服务节点之间通过grpc通信,不限开发语言。
  • 插件式结构,可以非常方便的通过插件扩展chatbot功能。
  • 支持command扩展,可以方便的处理各种指令参数。
  • 支持文件处理器扩展,可以方便的添加新的文件处理代码。
  • 支持多语言。
  • 支持配置文件响应指令。
  • 开发简单,易上手;大部分节点都无需二次开发,即可直接使用。
  • 调试友好,自带debug模式,直接通过聊天,就可以得到足够的调试信息。
  • 运维友好,所有节点都直接通过Docker部署,提供第三方服务部署脚本。

计划添加的功能:

  • 跨聊天工具的统一账号管理;通过token绑定账号。
  • 支持自然语言聊天。
使用

暂略

开发 Core Node

暂略

接入 telegram

接入telegram非常简单,您可以直接使用官方镜像。

安装或更新官方镜像:

docker pull zerrozhao/telegrambot

然后需要一个telegram的配置文件,格式如下:

telegramtoken: 1234567

token: 1234567
servaddr: 127.0.0.1:123
username: ada

这个文件需要放在当前目录的cfg下,然后我们可以通过下面的脚本来启动telegrambot节点。

docker container stop telegrambot
docker container rm telegrambot
docker run -d \
    --name telegrambot \
    --link adachatbot:adachatbot \
    -v $PWD/cfg:/app/telegrambot/cfg \
    -v $PWD/logs:/app/telegrambot/logs \
    zerrozhao/telegrambot

这里没有直接用--rm,而是先 stop ,再 rm ,主要是方便查看错误日志。

接入 CoolQ
  1. 注册QQ号,绑定手机号。
  2. 用手机版的QQ登录手机号,开启账号锁定。
  3. 使用coolq目录下的 init.sh 脚本初始化 coolq,这个脚本基本上可以不用修改。
  4. 第一次启动 coolq,用 coolq目录下的 start.sh 脚本启动 coolq,注意参数,建议修改端口、密码、qq号等参数。
  5. 用浏览器连接导出的9000端口,用VNC密码登录进去,启动CoolQ,第一次启动,会需要短信验证码,前面步骤1、2一定要执行,这种方式最省事了!
  6. 到data/app/io.github.richardchien.coolqhttpapi/config/修改配置文件,qq号.ini。
serve_data_files = yes
use_ws = yes
ws_host = 0.0.0.0
ws_port = 6700
  1. 重启coolq。

然后,才可以开始部署coolqbot。
还是直接使用官方镜像吧。

docker pull zerrozhao/coolqbot

修改coolqbot的配置文件。

ooolqtoken:
coolqsecret:
coolqservurl: ws://127.0.0.1:5700
coolqhttpservaddr: 

token: 1234567
servaddr: 127.0.0.1:123
username: ada

debug: true
preloaduserinfo: true

接下来,我们可以启动coolqbot了。

docker container stop coolqbot
docker container rm coolqbot
docker run -d \
    --name coolqbot \
    --link adachatbot:adachatbot \
    --link cqhttp:cqhttp \
    -p 7234:7234 \
    -v $PWD/cfg:/app/coolqbot/cfg \
    -v $PWD/logs:/app/coolqbot/logs \
    zerrozhao/coolqbot

必要的参数要改,如果开在一台服务器上,建议用 --link,可以省掉外网端口占用。

版本更新
v0.6
  • 支持多行command,不影响老版本command扩展的用法。
  • logger模块调整,支持重设logger,兼容性更强。
v0.5
  • golang升级到1.13
  • 增加基础chatbot节点,完全无需二次开发即可使用。
  • 支持图片。
  • 支持forward消息。
  • 支持笔记助理,可以帮您记录关键聊天信息,并支持全局检索和列表。
  • 代码结构小幅调整,直接 go get -u 无法正确升级,需要显示升级。
v0.3
  • grpcprotobuf版本更新,protos 结构调整,支持 go_package
  • 代码结构小幅调整,直接 go get -u 无法正确升级,需要显示升级。
  • 支持channel、group等。
v0.2
  • 支持QQ。
v0.1
  • 核心架构完成。
  • 完成telegram节点。
  • plugin、command、fileprocessor架构完成。
  • 多语言框架完成。
  • 第一个机器人,Ada的telegram完成,这里

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildChatMsg

func BuildChatMsg(lst []*chatbotpb.ChatMsgStream) (*chatbotpb.ChatMsg, error)

BuildChatMsg - []ChatMsgStream => ChatMsg

func BuildChatMsgList

func BuildChatMsgList(lst []*chatbotpb.ChatMsgStream) ([]*chatbotpb.ChatMsg, error)

BuildChatMsgList - []ChatMsgStream => []ChatMsg

func BuildChatMsgStream

func BuildChatMsgStream(chat *chatbotpb.ChatMsg) ([]*chatbotpb.ChatMsgStream, error)

BuildChatMsgStream - ChatMsg -> ChatMsgStream

func BuildErrorChatMsg

func BuildErrorChatMsg(err error, uai *chatbotpb.UserAppInfo, token string,
	sessionid string) *chatbotpb.ChatMsg

BuildErrorChatMsg - build ChatMsg

func BuildFileChatMsg added in v0.2.10

func BuildFileChatMsg(msg string, fd *chatbotpb.FileData, uai *chatbotpb.UserAppInfo, token string,
	sessionid string) *chatbotpb.ChatMsg

BuildFileChatMsg - build ChatMsg

func BuildGroupAppInfo added in v0.5.42

func BuildGroupAppInfo(appType chatbotpb.ChatAppType, usernameAppServ string,
	groupid string, groupname string) *chatbotpb.GroupAppInfo

BuildGroupAppInfo - build GroupAppInfo

func BuildTextChatMsg

func BuildTextChatMsg(msg string, uai *chatbotpb.UserAppInfo, token string,
	sessionid string) *chatbotpb.ChatMsg

BuildTextChatMsg - build ChatMsg

func BuildUserAppInfo

func BuildUserAppInfo(appType chatbotpb.ChatAppType, usernameAppServ string,
	appuid string, appuname string, lang string) *chatbotpb.UserAppInfo

BuildUserAppInfo - build UserAppInfo

func NewChatMsgWithText

func NewChatMsgWithText(l *i18n.Localizer, msgid string, tempParams map[string]interface{}, uai *chatbotpb.UserAppInfo) (
	*chatbotpb.ChatMsg, error)

NewChatMsgWithText - new ChatMsg with TextMgr

func NewLocalUserMgr added in v0.2.13

func NewLocalUserMgr(dbpath string, httpAddr string, engine string, core ServiceCore) (
	*chatbotusermgr.UserMgr, error)

NewLocalUserMgr - new local UserMgr

func RegPlugin

func RegPlugin(plugin Plugin) error

RegPlugin - register plugin

func RegisterCommand

func RegisterCommand(cmd string, c Command)

RegisterCommand - add command with cmd

func SplitCommandString

func SplitCommandString(str string) []string

SplitCommandString - split command string

func SplitMultiCommandString added in v0.5.43

func SplitMultiCommandString(str string) []string

SplitMultiCommandString - split multi command string

Types

type Client

type Client struct {
	SessionID string
	// contains filtered or unexported fields
}

Client - ChatBotServiceClient

func NewClient

func NewClient(servAddr string, apptype chatbotpb.ChatAppType, token string, username string) *Client

NewClient - new ChatBotServiceClient

func (*Client) RegisterAppService

func (client *Client) RegisterAppService(ctx context.Context) error

RegisterAppService - RegisterAppService

func (*Client) RequestChat added in v0.3.10

func (client *Client) RequestChat(ctx context.Context) ([]*chatbotpb.ChatMsg, error)

RequestChat - RequestChat

func (*Client) SendChat

func (client *Client) SendChat(ctx context.Context, chat *chatbotpb.ChatMsg) ([]*chatbotpb.ChatMsg, error)

SendChat - SendChat

type Command

type Command interface {
	// RunCommand - run command
	RunCommand(ctx context.Context, serv *Serv, params interface{},
		chat *chatbotpb.ChatMsg, ui *chatbotpb.UserInfo, ud proto.Message,
		scs chatbotpb.ChatBotService_SendChatServer) (bool, []*chatbotpb.ChatMsg, error)
	// ParseCommandLine - parse command line
	ParseCommandLine(cmdline []string, chat *chatbotpb.ChatMsg) (interface{}, error)
	// OnMessage - get message
	OnMessage(ctx context.Context, serv *Serv, chat *chatbotpb.ChatMsg,
		ui *chatbotpb.UserInfo, ud proto.Message,
		scs chatbotpb.ChatBotService_SendChatServer) (bool, []*chatbotpb.ChatMsg, error)
}

Command - command

type CommandData added in v0.5.43

type CommandData struct {
	Cmd    string
	Params interface{}
}

CommandData - command data

type CommandsMgr

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

CommandsMgr - command manager

func (*CommandsMgr) GetCommand

func (m *CommandsMgr) GetCommand(cmd string) Command

GetCommand - get command

func (*CommandsMgr) RegisterCommand

func (m *CommandsMgr) RegisterCommand(cmd string, c Command)

RegisterCommand - add command with cmd

type CommondsList

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

CommondsList - command list

func NewCommondsList

func NewCommondsList() *CommondsList

NewCommondsList - new CommondsList

func (*CommondsList) AddCommand

func (cmds *CommondsList) AddCommand(cmd string) error

AddCommand - add command

func (*CommondsList) HasCommand

func (cmds *CommondsList) HasCommand(cmd string) bool

HasCommand - has command

func (*CommondsList) MultiParseInChat added in v0.5.43

func (cmds *CommondsList) MultiParseInChat(chat *chatbotpb.ChatMsg) ([]*CommandData, error)

MultiParseInChat - parse in chat

func (*CommondsList) OnMessage added in v0.5.42

OnMessage - get message

func (*CommondsList) ParseInChat

func (cmds *CommondsList) ParseInChat(chat *chatbotpb.ChatMsg) (string, interface{}, error)

ParseInChat - parse in chat

func (*CommondsList) RunInChat

func (cmds *CommondsList) RunInChat(ctx context.Context, cmd string, serv *Serv, params interface{},
	chat *chatbotpb.ChatMsg, ui *chatbotpb.UserInfo, ud proto.Message,
	scs chatbotpb.ChatBotService_SendChatServer) (bool, []*chatbotpb.ChatMsg, error)

RunInChat - run func with cmd

type Config

type Config struct {
	AppServ []chatbotbase.AppServConfig

	BindAddr string
	DBPath   string
	DBEngine string

	PluginsPreprocess []string
	Plugins           []string
	PluginsSecondLine []string

	Commands []string

	Language        string
	LangPath        string
	StartText       []string
	HelpText        []string
	ChatBotNameText string

	Preprocessor string
	NoteKeyword  string

	Telegram *TelegramConfig
}

Config - config

func LoadConfig

func LoadConfig(filename string) (*Config, error)

LoadConfig - load config

type EmptyServiceCore added in v0.2.10

type EmptyServiceCore struct {
}

EmptyServiceCore - chatbot service core

func (*EmptyServiceCore) NewAppData added in v0.2.10

func (core *EmptyServiceCore) NewAppData() (proto.Message, error)

NewAppData - new a app data

func (*EmptyServiceCore) NewUserData added in v0.2.10

func (core *EmptyServiceCore) NewUserData(ui *chatbotpb.UserInfo) (proto.Message, error)

NewUserData - new a userdata

func (*EmptyServiceCore) OnDebug added in v0.2.13

func (core *EmptyServiceCore) OnDebug(ctx context.Context, serv *Serv, chat *chatbotpb.ChatMsg,
	ui *chatbotpb.UserInfo, ud proto.Message) ([]*chatbotpb.ChatMsg, error)

OnDebug - call in plugin.debug

func (*EmptyServiceCore) UnmarshalAppData added in v0.2.10

func (core *EmptyServiceCore) UnmarshalAppData(buf []byte) (proto.Message, error)

UnmarshalAppData - unmarshal

func (*EmptyServiceCore) UnmarshalUserData added in v0.2.10

func (core *EmptyServiceCore) UnmarshalUserData(buf []byte) (proto.Message, error)

UnmarshalUserData - unmarshal

type FileProcessor added in v0.2.10

type FileProcessor interface {
	// Proc - process
	Proc(ctx context.Context, serv *Serv, chat *chatbotpb.ChatMsg,
		ui *chatbotpb.UserInfo, ud proto.Message) ([]*chatbotpb.ChatMsg, error)

	// IsMyFile - is my file
	IsMyFile(chat *chatbotpb.ChatMsg) bool
}

FileProcessor - file processor

type FileProcessorMgr added in v0.2.10

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

FileProcessorMgr - file processor manager

func (*FileProcessorMgr) ProcFile added in v0.2.10

func (m *FileProcessorMgr) ProcFile(ctx context.Context, serv *Serv, chat *chatbotpb.ChatMsg,
	ui *chatbotpb.UserInfo, ud proto.Message) ([]*chatbotpb.ChatMsg, error)

ProcFile - process a file

func (*FileProcessorMgr) RegisterFileProcessor added in v0.2.10

func (m *FileProcessorMgr) RegisterFileProcessor(fp FileProcessor)

RegisterFileProcessor - register file processor

type FuncOnChatMsgs added in v0.3.10

type FuncOnChatMsgs func(lst []*chatbotpb.ChatMsg)

FuncOnChatMsgs - function

type FuncOnTimer added in v0.3.10

type FuncOnTimer func()

FuncOnTimer - function

type Plugin

type Plugin interface {
	// OnMessage - get message
	OnMessage(ctx context.Context, serv *Serv, chat *chatbotpb.ChatMsg,
		ui *chatbotpb.UserInfo, ud proto.Message,
		scs chatbotpb.ChatBotService_SendChatServer) ([]*chatbotpb.ChatMsg, error)

	// OnStart - on start
	OnStart(ctx context.Context) error

	// GetPluginName - get plugin name
	GetPluginName() string
}

Plugin - chat bot plugin interface

func GetStaticPlugin

func GetStaticPlugin(name string) Plugin

GetStaticPlugin - get static plugin

type PluginsList

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

PluginsList - plugins list

func NewPluginsList

func NewPluginsList() *PluginsList

NewPluginsList - new PluginsList

func (*PluginsList) AddPlugin

func (lst *PluginsList) AddPlugin(name string) error

AddPlugin - add plugin

func (*PluginsList) FindPlugin

func (lst *PluginsList) FindPlugin(name string) Plugin

FindPlugin -

func (*PluginsList) OnMessage

OnMessage - get message

func (*PluginsList) OnMessageEx

OnMessageEx - get message

func (*PluginsList) OnStart

func (lst *PluginsList) OnStart(ctx context.Context) error

OnStart - on start

type Serv

type Serv struct {
	Cfg *Config

	MgrUser UserMgr
	MgrText *TextMgr
	Cmds    *CommondsList
	Core    ServiceCore
	MgrFile *FileProcessorMgr
	// contains filtered or unexported fields
}

Serv - service

func NewChatBotServ

func NewChatBotServ(cfg *Config, mgr UserMgr, core ServiceCore) (*Serv, error)

NewChatBotServ -

func NewSimpleChatBotServ added in v0.2.13

func NewSimpleChatBotServ(cfg *Config, core ServiceCore) (*Serv, error)

NewSimpleChatBotServ - new a simple chatbot service with local user manager

func (*Serv) BuildBasicParamsMap

func (serv *Serv) BuildBasicParamsMap(chat *chatbotpb.ChatMsg, ui *chatbotpb.UserInfo, lang string) (
	map[string]interface{}, error)

BuildBasicParamsMap - build basic params map

func (*Serv) BuildChatMsgWithLang added in v0.5.42

func (serv *Serv) BuildChatMsgWithLang(chat *chatbotpb.ChatMsg, ui *chatbotpb.UserInfo, prefix string,
	params map[string]interface{}) ([]*chatbotpb.ChatMsg, error)

BuildChatMsgWithLang -

func (*Serv) BuildChatMsgWithLangEx added in v0.5.42

func (serv *Serv) BuildChatMsgWithLangEx(chat *chatbotpb.ChatMsg, ui *chatbotpb.UserInfo, lst []string,
	params map[string]interface{}) ([]*chatbotpb.ChatMsg, error)

BuildChatMsgWithLangEx -

func (*Serv) GetChatMsgLang

func (serv *Serv) GetChatMsgLang(chat *chatbotpb.ChatMsg) string

GetChatMsgLang - get chat message language

func (*Serv) Init

func (serv *Serv) Init(ctx context.Context) error

Init - initial service

func (*Serv) PushChatMsgs added in v0.3.10

func (serv *Serv) PushChatMsgs(token string, lst []*chatbotpb.ChatMsg)

PushChatMsgs - send ChatMsg List

func (*Serv) RegisterAppService

func (serv *Serv) RegisterAppService(ctx context.Context, ras *chatbotpb.RegisterAppService) (
	*chatbotpb.ReplyRegisterAppService, error)

RegisterAppService - register app service

func (*Serv) RequestChat

RequestChat - request chat

func (*Serv) RequestCtrl added in v0.3.2

func (serv *Serv) RequestCtrl(ctx context.Context, msg *chatbotpb.RequestCtrlData) (
	*chatbotpb.AppCtrlData, error)

RequestCtrl - request control

func (*Serv) SendChat

func (serv *Serv) SendChat(scs chatbotpb.ChatBotService_SendChatServer) error

SendChat - send chat

func (*Serv) SendCtrlResult added in v0.3.2

func (serv *Serv) SendCtrlResult(ctx context.Context, msg *chatbotpb.AppCtrlResult) (
	*chatbotpb.SCRResult, error)

SendCtrlResult - send control result

func (*Serv) Start

func (serv *Serv) Start(ctx context.Context) error

Start - start a service

func (*Serv) Stop

func (serv *Serv) Stop()

Stop - stop service

type ServiceCore

type ServiceCore interface {
	// UnmarshalAppData - unmarshal
	UnmarshalAppData(buf []byte) (proto.Message, error)
	// NewAppData - new a app data
	NewAppData() (proto.Message, error)

	// UnmarshalUserData - unmarshal
	UnmarshalUserData(buf []byte) (proto.Message, error)
	// NewUserData - new a userdata
	NewUserData(ui *chatbotpb.UserInfo) (proto.Message, error)

	// OnDebug - call in plugin.debug
	OnDebug(ctx context.Context, serv *Serv, chat *chatbotpb.ChatMsg,
		ui *chatbotpb.UserInfo, ud proto.Message) ([]*chatbotpb.ChatMsg, error)
}

ServiceCore - chatbot service core

type TelegramConfig added in v0.3.2

type TelegramConfig struct {
	Channels []string
}

TelegramConfig - config for telegram

type TextMgr

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

TextMgr - text manager

func NewTextMgr

func NewTextMgr(cfg *Config) (*TextMgr, error)

NewTextMgr - new TextMgr

func (*TextMgr) FindKeys added in v0.5.42

func (mgr *TextMgr) FindKeys(prefix string) []string

FindKeys - find keys

func (*TextMgr) GetLocalizer

func (mgr *TextMgr) GetLocalizer(lang string) (*i18n.Localizer, error)

GetLocalizer - get Localizer

type Timer added in v0.3.10

type Timer struct {
	OnTimer FuncOnTimer
	// contains filtered or unexported fields
}

Timer - timer

func SetTimer added in v0.3.10

func SetTimer(t time.Duration, onTimer FuncOnTimer) *Timer

SetTimer - set timer

func (*Timer) Close added in v0.3.10

func (t *Timer) Close()

Close - close timer

type UserMgr

type UserMgr interface {
	// GetAppUserInfo - get user infomation
	GetAppUserInfo(ctx context.Context, appToken string, uai *chatbotpb.UserAppInfo) (
		*chatbotpb.UserInfo, proto.Message, error)

	// GetNoteInfo - get note infomation
	GetNoteInfo(ctx context.Context, name string) (
		*chatbotpb.NoteInfo, error)
	// UpdNoteInfo - update note infomation
	UpdNoteInfo(ctx context.Context, ni *chatbotpb.NoteInfo) error
	// UpdNoteNode - update note node
	UpdNoteNode(ctx context.Context, nn *chatbotpb.NoteNode) error
	// GetNoteNode - get note node
	GetNoteNode(ctx context.Context, nameNote string, noteIndex int64) (
		*chatbotpb.NoteNode, error)
	// DelNoteNode - delete note node
	DelNoteNode(ctx context.Context, nameNote string, noteIndex int64) error
}

UserMgr - user maqnager

Directories

Path Synopsis
app
Package chatbotbase is a generated GoMock package.
Package chatbotbase is a generated GoMock package.
commands
plugins
cmd
samples

Jump to

Keyboard shortcuts

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