-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
113 lines (97 loc) · 2.49 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"github.com/razzie/botkit"
)
func main() {
var token string
flag.StringVar(&token, "token", "", "Telegram bot token")
flag.Parse()
if len(token) == 0 {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter token: ")
token, _ = reader.ReadString('\n')
}
dlg := botkit.NewDialogBuilder().
AddMultiChoiceQuery("Pick your favorite", func(choices []int) error {
if len(choices) < 1 {
return fmt.Errorf("pick at least one")
}
return nil
}, "Apple", "Orange", "Banana", "Grapes", "Melon").
AddTextInputQuery("Why?", func(resp string) error {
if len(resp) < 2 {
return fmt.Errorf("please write a longer response")
}
return nil
}).
SetFinalizer(func(ctx *botkit.Context, responses []any) {
ctx.SendMessage("responses: %v", responses)
}).
Build()
filedlg := botkit.NewDialogBuilder().
AddFileInputQuery("Upload a file", nil).
SetFinalizer(func(ctx *botkit.Context, responses []any) {
file := responses[0].(io.Reader)
p := make([]byte, 4)
n, _ := file.Read(p)
p = p[:n]
ctx.SendReply("First %d bytes in hex: %x", n, p)
}).
Build()
bot, err := botkit.NewBot(token,
//botkit.WithAPIEndpoint("localhost:8080"),
botkit.WithCommand("hello", cmdHelloWorld),
botkit.WithCommand("album", cmdAlbum),
botkit.WithCommand("startdlg", cmdStartDialog),
botkit.WithCommand("filedlg", cmdFileDialog),
botkit.WithCommand("sticker", cmdSticker),
botkit.WithCommand("sum", cmdSum),
botkit.WithCommand("sumMany", cmdSumMany),
botkit.WithDialog("dlg", dlg),
botkit.WithDialog("filedlg", filedlg),
)
if err != nil {
panic(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
bot.Close()
}()
bot.Run()
}
func cmdHelloWorld() botkit.CommandResponse {
return botkit.SendMessage("Hello World!")
}
func cmdAlbum(ctx *botkit.Context) {
ctx.SendMedia(
botkit.NewPhoto(botkit.URLSource("https://gorzsony.com/img/razchess.png")),
botkit.NewPhoto(botkit.URLSource("https://gorzsony.com/img/razbox.png")))
}
func cmdStartDialog(ctx *botkit.Context) {
ctx.StartDialog("dlg")
}
func cmdFileDialog(ctx *botkit.Context) {
ctx.StartDialog("filedlg")
}
func cmdSticker(ctx *botkit.Context) {
ctx.SendSticker("pizzabot", -1)
}
func cmdSum(ctx *botkit.Context, a, b int) {
ctx.SendReply("%d", a+b)
}
func cmdSumMany(ctx *botkit.Context, numbers ...int) {
sum := 0
for _, n := range numbers {
sum += n
}
ctx.SendReply("%d", sum)
}