all repos — telegram-bot-api @ 5d6f84e9b2505cf86f8ae4c02c9eb0b415895c8f

Golang bindings for the Telegram Bot API

plugin_manage.go (view raw)

  1package main
  2
  3import (
  4	"fmt"
  5	"log"
  6	"strings"
  7)
  8
  9type ManagePlugin struct {
 10}
 11
 12func (plugin *ManagePlugin) GetName() string {
 13	return "Plugin manager"
 14}
 15
 16func (plugin *ManagePlugin) GetCommands() []string {
 17	return []string{
 18		"/enable",
 19		"Enable",
 20		"/disable",
 21		"Disable",
 22		"/reload",
 23	}
 24}
 25
 26func (plugin *ManagePlugin) GetHelpText() []string {
 27	return []string{
 28		"/enable [name] - enables a plugin",
 29		"/disable [name] - disables a plugin",
 30		"/reload - reloads bot configuration",
 31	}
 32}
 33
 34func (plugin *ManagePlugin) Setup() {
 35}
 36
 37func (plugin *ManagePlugin) GotCommand(command string, message Message, args []string) {
 38	log.Println(command)
 39
 40	if command == "/enable" {
 41		keyboard := [][]string{}
 42
 43		hasDisabled := false
 44		for _, plug := range plugins {
 45			enabled, _ := config.EnabledPlugins[plug.GetName()]
 46			if enabled {
 47				continue
 48			}
 49
 50			hasDisabled = true
 51			keyboard = append(keyboard, []string{"Enable " + plug.GetName()})
 52		}
 53
 54		if !hasDisabled {
 55			msg := NewMessage(message.Chat.Id, "All plugins are enabled!")
 56			msg.ReplyToMessageId = message.MessageId
 57
 58			bot.sendMessage(msg)
 59
 60			return
 61		}
 62
 63		msg := NewMessage(message.Chat.Id, "Please specify which plugin to enable")
 64		msg.ReplyToMessageId = message.MessageId
 65		msg.ReplyMarkup = ReplyKeyboardMarkup{
 66			Keyboard:        keyboard,
 67			OneTimeKeyboard: true,
 68			Selective:       true,
 69			ResizeKeyboard:  true,
 70		}
 71
 72		bot.sendMessage(msg)
 73	} else if command == "Enable" {
 74		pluginName := strings.SplitN(message.Text, " ", 2)
 75
 76		msg := NewMessage(message.Chat.Id, "")
 77		msg.ReplyToMessageId = message.MessageId
 78		msg.ReplyMarkup = ReplyKeyboardHide{
 79			HideKeyboard: true,
 80			Selective:    true,
 81		}
 82
 83		_, ok := config.EnabledPlugins[pluginName[1]]
 84		if !ok {
 85			msg.Text = "Unknown plugin!"
 86			msg.ReplyToMessageId = message.MessageId
 87			bot.sendMessage(msg)
 88
 89			return
 90		}
 91
 92		config.EnabledPlugins[pluginName[1]] = true
 93		msg.Text = fmt.Sprintf("Enabled '%s'!", pluginName[1])
 94		bot.sendMessage(msg)
 95	} else if command == "/disable" {
 96		keyboard := [][]string{}
 97
 98		hasEnabled := false
 99		for _, plug := range plugins {
100			enabled, _ := config.EnabledPlugins[plug.GetName()]
101			if !enabled {
102				continue
103			}
104
105			hasEnabled = true
106			keyboard = append(keyboard, []string{"Disable " + plug.GetName()})
107		}
108
109		if !hasEnabled {
110			msg := NewMessage(message.Chat.Id, "All plugins are disabled!")
111			msg.ReplyToMessageId = message.MessageId
112
113			bot.sendMessage(msg)
114
115			return
116		}
117
118		msg := NewMessage(message.Chat.Id, "Please specify which plugin to disable")
119		msg.ReplyToMessageId = message.MessageId
120		msg.ReplyMarkup = ReplyKeyboardMarkup{
121			Keyboard:        keyboard,
122			OneTimeKeyboard: true,
123			Selective:       true,
124			ResizeKeyboard:  true,
125		}
126
127		bot.sendMessage(msg)
128	} else if command == "Disable" {
129		pluginName := strings.SplitN(message.Text, " ", 2)
130
131		msg := NewMessage(message.Chat.Id, "")
132		msg.ReplyToMessageId = message.MessageId
133		msg.ReplyMarkup = ReplyKeyboardHide{
134			HideKeyboard: true,
135			Selective:    true,
136		}
137
138		_, ok := config.EnabledPlugins[pluginName[1]]
139		if !ok {
140			msg.Text = "Unknown plugin!"
141			msg.ReplyToMessageId = message.MessageId
142			bot.sendMessage(msg)
143
144			return
145		}
146
147		config.EnabledPlugins[pluginName[1]] = false
148		msg.Text = fmt.Sprintf("Disabled '%s'!", pluginName[1])
149		bot.sendMessage(msg)
150	}
151
152	saveConfig()
153}