-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_chatid.go
69 lines (56 loc) · 1.66 KB
/
last_chatid.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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
// // //
func getLastChatIDFromMessage(cfg *appConfig, botName, username string) (configUpdated bool, err error) {
token, err := getBotTokenByName(cfg.Bots, botName)
if err != nil {
return
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
funcPrintMessage := func() {
fmt.Printf("Por favor, envía un mensaje al bot \"%s\"...\n", colors.clrHighlighted.Sprint(botName))
}
opts := []bot.Option{
bot.WithDefaultHandler(func(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message != nil {
if username == "" && update.Message.From.Username != "" {
username = update.Message.From.Username
}
if username != "" {
fmt.Printf(
"Usuario %s (%s): %s\n",
colors.clrBold.Sprint(strings.TrimSpace(strings.Join([]string{update.Message.From.FirstName, update.Message.From.LastName}, " "))),
colors.clrHighlighted.Sprint(username),
colors.clrHighlighted.Sprintf("%d", update.Message.From.ID),
)
// Buscar el usuario en la lista configurada
if cfgUsername, ok := cfg.Users[update.Message.From.ID]; !ok || cfgUsername != username {
cfg.Users[update.Message.From.ID] = username
configUpdated = true
}
} else {
err = errors.New("must be define username")
}
cancel()
} else {
funcPrintMessage()
}
}),
}
b, err := bot.New(token, opts...)
if err == nil {
funcPrintMessage()
b.Start(ctx)
}
return
}