openwechatpp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2024 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

OpenWeChatPP

OpenWeChatPP是对openwechat的扩展,让编写机器人变得更方便。

示例

一个简单的天气查询机器人

效果图

bot := openwechat.DefaultBot(openwechat.Desktop)

// 你只需要构造一个调度器
dispatcher := openwechatpp.Dispatcher{}

// 把你想要的指令以声明式的语法添加进去
dispatcher.AddCommand(openwechatpp.Command{
    Name:   "天气查询",
    Usage:  "发送“/天气”查询天气",
    Filter: openwechatpp.AcceptSameContent("/天气"),
    Handler: func(msg *openwechat.Message) error {
        msg.ReplyText("请发送要查询的城市。")
        // 等待msg发送方的下一条消息
        replyMsg, err := dispatcher.WaitForNext(msg, time.Second*30)
        if err != nil {
            return err
        }
        _, err = replyMsg.ReplyText(fmt.Sprintf("%s今天的天气是……", replyMsg.Content))
        return err
    },
})

// 最后将调度器设置为bot的MessageHandler即可
bot.MessageHandler = dispatcher.AsMessageHandler()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptEverything

func AcceptEverything(*ow.Message) bool

func AcceptImage

func AcceptImage(msg *ow.Message) bool

func AcceptText

func AcceptText(msg *ow.Message) bool

Types

type Command

type Command struct {
	IsOnce  bool
	Name    string
	Usage   string
	Filter  Filter
	Handler func(*ow.Message) error
}

func (Command) CheckAvailability

func (c Command) CheckAvailability() error

type Dispatcher

type Dispatcher struct {
	Commands []*Command
}

func (*Dispatcher) AddCommand

func (e *Dispatcher) AddCommand(cmd Command)

func (*Dispatcher) AsMessageHandler

func (e *Dispatcher) AsMessageHandler() ow.MessageHandler

func (*Dispatcher) AwaitMatchingMessage

func (e *Dispatcher) AwaitMatchingMessage(filter Filter, maxWaitingTime time.Duration) (*ow.Message, error)

func (*Dispatcher) HandleMessage

func (e *Dispatcher) HandleMessage(msg *ow.Message)

func (*Dispatcher) WaitForNext

func (e *Dispatcher) WaitForNext(msg *ow.Message, maxInputTime time.Duration) (*ow.Message, error)

等待给定msg的发送者的下一条消息直到超出maxInputTime

type Filter

type Filter func(*ow.Message) (canHandle bool)

func AcceptRegexMatching

func AcceptRegexMatching(regex string) Filter

func AcceptSameContent

func AcceptSameContent(content string) Filter

func AcceptSamePrefix

func AcceptSamePrefix(prefix string) Filter

func ConstructSameOriginFilter

func ConstructSameOriginFilter(originMsg *ow.Message) (Filter, error)

接受同源消息 即对于好友消息,是同一好友发送的 对于群聊消息,是同一群聊中的同一个人发送的

Jump to

Keyboard shortcuts

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