-
Notifications
You must be signed in to change notification settings - Fork 120
/
main.go
109 lines (96 loc) · 3.11 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
)
// This bot demonstrates some example interactions with commands on telegram.
// It has a basic start command with a bot intro.
// It also has a source command, which sends the bot sourcecode, as a file.
func main() {
// Get token from the environment variable
token := os.Getenv("TOKEN")
if token == "" {
panic("TOKEN environment variable is empty")
}
// Create bot from environment value.
b, err := gotgbot.NewBot(token, nil)
if err != nil {
panic("failed to create new bot: " + err.Error())
}
// Create updater and dispatcher.
dispatcher := ext.NewDispatcher(&ext.DispatcherOpts{
// If an error is returned by a handler, log it and continue going.
Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
log.Println("an error occurred while handling update:", err.Error())
return ext.DispatcherActionNoop
},
MaxRoutines: ext.DefaultMaxRoutines,
})
updater := ext.NewUpdater(dispatcher, nil)
// /start command to introduce the bot
dispatcher.AddHandler(handlers.NewCommand("start", start))
// /source command to send the bot source code
dispatcher.AddHandler(handlers.NewCommand("source", source))
// Start receiving updates.
err = updater.StartPolling(b, &ext.PollingOpts{
DropPendingUpdates: true,
GetUpdatesOpts: &gotgbot.GetUpdatesOpts{
Timeout: 9,
RequestOpts: &gotgbot.RequestOpts{
Timeout: time.Second * 10,
},
},
})
if err != nil {
panic("failed to start polling: " + err.Error())
}
log.Printf("%s has been started...\n", b.User.Username)
// Idle, to keep updates coming in, and avoid bot stopping.
updater.Idle()
}
func source(b *gotgbot.Bot, ctx *ext.Context) error {
// Sending a file by file handle
f, err := os.Open("samples/commandBot/main.go")
if err != nil {
return fmt.Errorf("failed to open source: %w", err)
}
m, err := b.SendDocument(ctx.EffectiveChat.Id,
gotgbot.InputFileByReader("source.go", f),
&gotgbot.SendDocumentOpts{
Caption: "Here is my source code, by file handle.",
ReplyParameters: &gotgbot.ReplyParameters{
MessageId: ctx.EffectiveMessage.MessageId,
},
})
if err != nil {
return fmt.Errorf("failed to send source: %w", err)
}
// Or sending a file by file ID
_, err = b.SendDocument(ctx.EffectiveChat.Id,
gotgbot.InputFileByID(m.Document.FileId),
&gotgbot.SendDocumentOpts{
Caption: "Here is my source code, sent by file id.",
ReplyParameters: &gotgbot.ReplyParameters{
MessageId: ctx.EffectiveMessage.MessageId,
},
})
if err != nil {
return fmt.Errorf("failed to send source: %w", err)
}
return nil
}
// start introduces the bot.
func start(b *gotgbot.Bot, ctx *ext.Context) error {
_, err := ctx.EffectiveMessage.Reply(b, fmt.Sprintf("Hello, I'm @%s.\nI am a sample bot to demonstrate how file sending works.\n\nTry the /source command!", b.User.Username), &gotgbot.SendMessageOpts{
ParseMode: "HTML",
})
if err != nil {
return fmt.Errorf("failed to send start message: %w", err)
}
return nil
}