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

72
bot.go
View File

@ -5,59 +5,25 @@ import (
"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 @@ func main() {
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 @@ func main() {
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)
}
}
}
}

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
@ -52,12 +53,16 @@ func (bot *BotApi) makeRequest(endpoint string, params url.Values) (ApiResponse,
}
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
}

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

@ -5,8 +5,10 @@ import (
)
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 {