-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (80 loc) · 2.6 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
package main
import (
"context"
"fmt"
"github.com/go-telegram/bot"
models "github.com/go-telegram/bot/models"
"github.com/joho/godotenv"
repository "github.com/phaalonso/go-reputation-bot/pkg/models"
"log"
"os"
"os/signal"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
telegramKey := os.Getenv("TELEGRAM_API_KEY")
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(handler),
bot.WithDebug(),
}
b, err := bot.New(telegramKey, opts...)
if err != nil {
log.Fatal(err)
}
b.RegisterHandler(bot.HandlerTypeMessageText, "/start", bot.MatchTypeExact, startCommandHandler)
b.RegisterHandler(bot.HandlerTypeMessageText, "/reputation", bot.MatchTypeExact, reputationHanler)
b.RegisterHandler(bot.HandlerTypeMessageText, "/global_reputation", bot.MatchTypeExact, totalReputationHandler)
b.DeleteMyCommands(ctx, &bot.DeleteMyCommandsParams{})
//b.SetMyCommands(ctx, &bot.SetMyCommandsParams{
// Scope: nil,
// LanguageCode: "",
//})
b.Start(ctx)
}
func startCommandHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Welcome to the reputation bot!\nAdd me into a chat so I can begin to track the reputation of your members",
})
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message.ReplyToMessage == nil {
return
}
fmt.Println(update.Message.Text)
chatId := update.Message.Chat.ID
userId := update.Message.ReplyToMessage.From.ID
rep := repository.UpdateOrCreateReputation(chatId, userId)
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: chatId,
Text: fmt.Sprintf("Esse usuário possui %d de reputação", rep.Reputation),
})
}
func reputationHanler(ctx context.Context, b *bot.Bot, update *models.Update) {
chatId := update.Message.Chat.ID
userId := update.Message.From.ID
uReputation, err := repository.GetUserReputationInChat(chatId, userId)
if err != nil {
log.Fatal(err)
}
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: fmt.Sprintf("Your reputation in this chat is %d", uReputation.Reputation),
})
}
func totalReputationHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
userId := update.Message.From.ID
reputation, err := repository.GetTotalUserReputation(userId)
if err != nil {
log.Fatal(err)
}
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: fmt.Sprintf("Your global reputation is %d", reputation),
})
}