help plugin, working on plugin api

bot-api-6.1
Syfaro 2015-06-25 14:20:02 -05:00
parent a7b8af6adb
commit 567a37868d
4 changed files with 99 additions and 54 deletions

70
bot.go
View File

@ -5,59 +5,25 @@ import (
"flag" "flag"
"io/ioutil" "io/ioutil"
"log" "log"
"strconv"
"strings" "strings"
"time" "time"
) )
type Config struct { type Config struct {
Token string `json:"token"` Token string `json:"token"`
Plugins map[string]string `json:"plugins"`
} }
type Plugin interface { type Plugin interface {
GetCommand() string GetName() string
GotCommand(Message, []string) GetCommands() []string
} GetHelpText() []string
GotCommand(string, 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"))
}
} }
var bot *BotApi var bot *BotApi
var plugins []Plugin
var config Config
func main() { func main() {
configPath := flag.String("config", "config.json", "path to config.json") configPath := flag.String("config", "config.json", "path to config.json")
@ -69,22 +35,24 @@ func main() {
log.Panic(err) log.Panic(err)
} }
var cfg Config json.Unmarshal(data, &config)
json.Unmarshal(data, &cfg)
bot = NewBotApi(BotConfig{ bot = NewBotApi(BotConfig{
token: cfg.Token, token: config.Token,
debug: true, debug: true,
}) })
plugins := []Plugin{&ColonThree{}} plugins = []Plugin{&HelpPlugin{}, &FAPlugin{}}
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(time.Second)
lastUpdate := 0 lastUpdate := 0
for range ticker.C { 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 { if err != nil {
log.Panic(err) log.Panic(err)
@ -100,10 +68,12 @@ func main() {
for _, plugin := range plugins { for _, plugin := range plugins {
parts := strings.Split(update.Message.Text, " ") parts := strings.Split(update.Message.Text, " ")
if plugin.GetCommand() == parts[0] { for _, cmd := range plugin.GetCommands() {
parts = append(parts[:0], parts[1:]...) if cmd == parts[0] {
args := append(parts[:0], parts[1:]...)
plugin.GotCommand(update.Message, parts) plugin.GotCommand(parts[0], update.Message, args)
}
} }
} }
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -52,12 +53,16 @@ func (bot *BotApi) makeRequest(endpoint string, params url.Values) (ApiResponse,
} }
if bot.config.debug { if bot.config.debug {
log.Println(string(bytes[:])) log.Println(endpoint, string(bytes))
} }
var apiResp ApiResponse var apiResp ApiResponse
json.Unmarshal(bytes, &apiResp) json.Unmarshal(bytes, &apiResp)
if !apiResp.Ok {
return ApiResponse{}, errors.New(apiResp.Description)
}
return apiResp, nil return apiResp, nil
} }

68
plugin_help.go 100644
View File

@ -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)
}

View File

@ -7,6 +7,8 @@ import (
type ApiResponse struct { type ApiResponse struct {
Ok bool `json:"ok"` Ok bool `json:"ok"`
Result json.RawMessage `json:"result"` Result json.RawMessage `json:"result"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
} }
type Update struct { type Update struct {