help plugin, working on plugin api
Syfaro syfaro@foxpaw.in
Thu, 25 Jun 2015 14:20:02 -0500
4 files changed,
99 insertions(+),
54 deletions(-)
M
bot.go
→
bot.go
@@ -5,59 +5,25 @@ "encoding/json"
"flag" "io/ioutil" "log" - "strconv" "strings" "time" ) type Config struct { - Token string `json:"token"` + Token string `json:"token"` + Plugins map[string]string `json:"plugins"` } type Plugin interface { - GetCommand() string - GotCommand(Message, []string) -} - -type ColonThree struct { -} - -func (plugin *ColonThree) GetCommand() string { - return "/three" -} - -func (plugin *ColonThree) GotCommand(message Message, args []string) { - if len(args) > 0 { - n, err := strconv.Atoi(args[0]) - if err != nil { - msg := NewMessage(message.Chat.Id, "Bad number!") - msg.ReplyToMessageId = message.MessageId - - bot.sendMessage(msg) - - return - } - - if n > 5 { - msg := NewMessage(message.Chat.Id, "That's a bit much, no?") - msg.ReplyToMessageId = message.MessageId - - bot.sendMessage(msg) - - return - } - - for i := 0; i < n; i++ { - bot.sendMessage(NewMessage(message.Chat.Id, ":3")) - } - } else { - bot.sendMessage(NewMessage(message.Chat.Id, ":3")) - - bot.sendPhoto(NewPhotoUpload(message.Chat.Id, "fox.png")) - } + GetName() string + GetCommands() []string + GetHelpText() []string + GotCommand(string, Message, []string) } var bot *BotApi +var plugins []Plugin +var config Config func main() { configPath := flag.String("config", "config.json", "path to config.json")@@ -69,22 +35,24 @@ if err != nil {
log.Panic(err) } - var cfg Config - json.Unmarshal(data, &cfg) + json.Unmarshal(data, &config) bot = NewBotApi(BotConfig{ - token: cfg.Token, + token: config.Token, debug: true, }) - plugins := []Plugin{&ColonThree{}} + plugins = []Plugin{&HelpPlugin{}, &FAPlugin{}} - ticker := time.NewTicker(5 * time.Second) + ticker := time.NewTicker(time.Second) lastUpdate := 0 for range ticker.C { - updates, err := bot.getUpdates(NewUpdate(lastUpdate + 1)) + update := NewUpdate(lastUpdate + 1) + update.Timeout = 30 + + updates, err := bot.getUpdates(update) if err != nil { log.Panic(err)@@ -100,10 +68,12 @@
for _, plugin := range plugins { parts := strings.Split(update.Message.Text, " ") - if plugin.GetCommand() == parts[0] { - parts = append(parts[:0], parts[1:]...) + for _, cmd := range plugin.GetCommands() { + if cmd == parts[0] { + args := append(parts[:0], parts[1:]...) - plugin.GotCommand(update.Message, parts) + plugin.GotCommand(parts[0], update.Message, args) + } } } }
M
methods.go
→
methods.go
@@ -3,6 +3,7 @@
import ( "bytes" "encoding/json" + "errors" "io" "io/ioutil" "log"@@ -52,11 +53,15 @@ return ApiResponse{}, err
} if bot.config.debug { - log.Println(string(bytes[:])) + log.Println(endpoint, string(bytes)) } var apiResp ApiResponse json.Unmarshal(bytes, &apiResp) + + if !apiResp.Ok { + return ApiResponse{}, errors.New(apiResp.Description) + } return apiResp, nil }
A
plugin_help.go
@@ -0,0 +1,68 @@
+package main + +import ( + "bytes" + "log" +) + +type HelpPlugin struct { +} + +func (plugin *HelpPlugin) GetName() string { + return "Plugins help" +} + +func (plugin *HelpPlugin) GetCommands() []string { + return []string{"/help"} +} + +func (plugin *HelpPlugin) GetHelpText() []string { + return []string{"/help (/command) - returns help about a command"} +} + +func (plugin *HelpPlugin) GotCommand(command string, message Message, args []string) { + msg := NewMessage(message.Chat.Id, "") + msg.ReplyToMessageId = message.MessageId + msg.DisableWebPagePreview = true + + var buffer bytes.Buffer + + if len(args) > 0 { + for _, plug := range plugins { + for _, cmd := range plug.GetCommands() { + log.Println(cmd) + log.Println(args[0]) + log.Println(args[0][1:]) + if cmd == args[0] || cmd[1:] == args[0] { + buffer.WriteString(plug.GetName()) + buffer.WriteString("\n") + + for _, help := range plug.GetHelpText() { + buffer.WriteString(" ") + buffer.WriteString(help) + buffer.WriteString("\n") + } + } + } + } + } else { + buffer.WriteString(config.Plugins["about_text"]) + buffer.WriteString("\n\n") + + for _, plug := range plugins { + buffer.WriteString(plug.GetName()) + buffer.WriteString("\n") + + for _, cmd := range plug.GetHelpText() { + buffer.WriteString(" ") + buffer.WriteString(cmd) + buffer.WriteString("\n") + } + + buffer.WriteString("\n") + } + } + + msg.Text = buffer.String() + bot.sendMessage(msg) +}
M
responses.go
→
responses.go
@@ -5,8 +5,10 @@ "encoding/json"
) type ApiResponse struct { - Ok bool `json:"ok"` - Result json.RawMessage `json:"result"` + Ok bool `json:"ok"` + Result json.RawMessage `json:"result"` + ErrorCode int `json:"error_code"` + Description string `json:"description"` } type Update struct {