all repos — telegram-bot-api @ 567a37868d87cbd50149f484f26cefd2859ecbfd

Golang bindings for the Telegram Bot API

plugin_help.go (view raw)

 1package main
 2
 3import (
 4	"bytes"
 5	"log"
 6)
 7
 8type HelpPlugin struct {
 9}
10
11func (plugin *HelpPlugin) GetName() string {
12	return "Plugins help"
13}
14
15func (plugin *HelpPlugin) GetCommands() []string {
16	return []string{"/help"}
17}
18
19func (plugin *HelpPlugin) GetHelpText() []string {
20	return []string{"/help (/command) - returns help about a command"}
21}
22
23func (plugin *HelpPlugin) GotCommand(command string, message Message, args []string) {
24	msg := NewMessage(message.Chat.Id, "")
25	msg.ReplyToMessageId = message.MessageId
26	msg.DisableWebPagePreview = true
27
28	var buffer bytes.Buffer
29
30	if len(args) > 0 {
31		for _, plug := range plugins {
32			for _, cmd := range plug.GetCommands() {
33				log.Println(cmd)
34				log.Println(args[0])
35				log.Println(args[0][1:])
36				if cmd == args[0] || cmd[1:] == args[0] {
37					buffer.WriteString(plug.GetName())
38					buffer.WriteString("\n")
39
40					for _, help := range plug.GetHelpText() {
41						buffer.WriteString("  ")
42						buffer.WriteString(help)
43						buffer.WriteString("\n")
44					}
45				}
46			}
47		}
48	} else {
49		buffer.WriteString(config.Plugins["about_text"])
50		buffer.WriteString("\n\n")
51
52		for _, plug := range plugins {
53			buffer.WriteString(plug.GetName())
54			buffer.WriteString("\n")
55
56			for _, cmd := range plug.GetHelpText() {
57				buffer.WriteString("  ")
58				buffer.WriteString(cmd)
59				buffer.WriteString("\n")
60			}
61
62			buffer.WriteString("\n")
63		}
64	}
65
66	msg.Text = buffer.String()
67	bot.sendMessage(msg)
68}