forked from mymmrac/telego
/
main.go
72 lines (55 loc) · 1.92 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
package main
import (
"fmt"
"io"
"os"
"github.com/dsurl/tgo"
tu "github.com/dsurl/tgo/utils"
)
func main() {
botToken := os.Getenv("TOKEN")
// Note: Please keep in mind that default logger may expose sensitive information, use in development only
bot, err := tgo.NewBot(botToken, tgo.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Create tgo.ChatID from int64
chatID := tu.ID(123)
// Create message with only required parameters
_, _ = bot.SendMessage(tu.Message(chatID, "Hello"))
// Create tgo.ChatID username and send message
_, _ = bot.SendMessage(tu.Message(tu.Username("@user"), "World"))
// Create message and change optional parameters
msg := tu.Message(chatID, "Hello World").
WithReplyToMessageID(1234).
WithDisableNotification().
WithProtectContent()
_, _ = bot.SendMessage(msg)
var file *os.File // Used just for example (not valid in real use)
// Create document using *os.File as tgo.InputFile
_, _ = bot.SendDocument(tu.Document(chatID, tu.File(file)))
var reader io.Reader // Used just for example (not valid in real use)
// Create document using io.Reader by "naming" it and send as document
_, _ = bot.SendDocument(tu.Document(chatID, tu.File(tu.NameReader(reader, "my_file"))))
// Create document using URL to file as tgo.InputFile
_, _ = bot.SendDocument(tu.Document(chatID, tu.FileByURL("https://example.com/test.txt")))
// Create contact from phone and first name
_, _ = bot.SendContact(tu.Contact(chatID, "+123454321", "John"))
// Small example of parsing commands
updates, _ := bot.GetUpdates(nil)
for _, u := range updates {
if u.Message != nil {
text := u.Message.Text
// Parse text into command and its arguments
command, args := tu.ParseCommand(text)
// Check if text contains command
if command != "" {
fmt.Println("Command:", command)
fmt.Println("Args:", args)
} else {
fmt.Println("Not a command:", text)
}
}
}
}