-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
82 lines (71 loc) · 2.37 KB
/
handler.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
package main
import (
"context"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"log"
"net/http"
"time"
)
var httpClient = &http.Client{Transport: http.DefaultTransport, Timeout: 60 * time.Second}
type Handler struct {
ctx context.Context
botAPI *tgbotapi.BotAPI
me tgbotapi.User
}
func NewHandler(ctx context.Context, botToken string) (h *Handler) {
var err error
h = &Handler{ctx: ctx}
h.botAPI, err = tgbotapi.NewBotAPIWithClient(botToken, tgbotapi.APIEndpoint, httpClient)
if err != nil {
log.Fatalf("new bot api error %v", err)
}
h.me, err = h.botAPI.GetMe()
if err != nil {
log.Fatalf("test bot api error %v", err)
}
return h
}
func (h *Handler) Chat(chatId int64) (tgbotapi.Chat, error) {
return h.botAPI.GetChat(tgbotapi.ChatInfoConfig{ChatConfig: tgbotapi.ChatConfig{ChatID: chatId}})
}
func (h *Handler) Admins(chatId int64) ([]tgbotapi.ChatMember, error) {
return h.botAPI.GetChatAdministrators(tgbotapi.ChatAdministratorsConfig{ChatConfig: tgbotapi.ChatConfig{ChatID: chatId}})
}
func (h *Handler) Member(chatId, userId int64) (tgbotapi.ChatMember, error) {
return h.botAPI.GetChatMember(tgbotapi.GetChatMemberConfig{
ChatConfigWithUser: tgbotapi.ChatConfigWithUser{ChatID: chatId, UserID: userId},
})
}
func (h *Handler) MembersCount(chatId int64) (int, error) {
return h.botAPI.GetChatMembersCount(tgbotapi.ChatMemberCountConfig{ChatConfig: tgbotapi.ChatConfig{ChatID: chatId}})
}
func (h *Handler) Promote(chatId, userId int64, canPromote ...bool) error {
chatTable := tgbotapi.PromoteChatMemberConfig{
ChatMemberConfig: tgbotapi.ChatMemberConfig{ChatID: chatId, UserID: userId},
IsAnonymous: true,
CanManageChat: true,
CanChangeInfo: true,
CanPostMessages: true,
CanEditMessages: true,
CanDeleteMessages: true,
CanManageVoiceChats: true,
CanInviteUsers: true,
CanRestrictMembers: true,
CanPinMessages: true,
CanPromoteMembers: len(canPromote) > 0 && canPromote[0],
}
_, err := h.botAPI.Request(chatTable)
return err
}
func (h *Handler) Leave(chatId int64) error {
_, err := h.botAPI.Send(tgbotapi.LeaveChatConfig{ChatID: chatId})
return err
}
func (h *Handler) reply(chatId int64, text string) error {
msg := tgbotapi.NewMessage(chatId, text)
_, err := h.botAPI.Send(msg)
if err != nil {
log.Printf("reply chat %d, text %s, error %v\n", chatId, text, err)
}
return err
}