-
Notifications
You must be signed in to change notification settings - Fork 120
/
custom_helpers.go
152 lines (131 loc) · 4.6 KB
/
custom_helpers.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gotgbot
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// GetLink is a helper method to easily get the message link (It will return an empty string in case of private or group chat type).
func (m Message) GetLink() string {
if m.Chat.Type == "private" || m.Chat.Type == "group" {
return ""
}
if m.Chat.Username != "" {
return fmt.Sprintf("https://t.me/%s/%d", m.Chat.Username, m.MessageId)
}
// Message links use raw chatIds without the -100 prefix; this trims that prefix.
rawChatId := strings.TrimPrefix(strconv.FormatInt(m.Chat.Id, 10), "-100")
return fmt.Sprintf("https://t.me/c/%s/%d", rawChatId, m.MessageId)
}
// GetText returns the message text, for both text messages and media messages. (Why is this not the telegram default!)
func (m Message) GetText() string {
if m.Caption != "" {
return m.Caption
}
return m.Text
}
// GetEntities returns the message entities, for both text messages and media messages. (Why is this not the telegram default!)
func (m Message) GetEntities() []MessageEntity {
if len(m.CaptionEntities) > 0 {
return m.CaptionEntities
}
return m.Entities
}
// Reply is a helper function to easily call Bot.SendMessage as a reply to an existing Message.
func (m Message) Reply(b *Bot, text string, opts *SendMessageOpts) (*Message, error) {
if opts == nil {
opts = &SendMessageOpts{}
}
if opts.ReplyParameters == nil || opts.ReplyParameters.MessageId == 0 {
if opts.ReplyParameters == nil {
opts.ReplyParameters = &ReplyParameters{}
}
opts.ReplyParameters.MessageId = m.MessageId
}
return b.SendMessage(m.Chat.Id, text, opts)
}
// Reply is a helper function to easily call Bot.SendMessage as a reply to an existing InaccessibleMessage.
func (im InaccessibleMessage) Reply(b *Bot, text string, opts *SendMessageOpts) (*Message, error) {
if opts == nil {
opts = &SendMessageOpts{}
}
if opts.ReplyParameters == nil || opts.ReplyParameters.MessageId == 0 {
if opts.ReplyParameters == nil {
opts.ReplyParameters = &ReplyParameters{}
}
opts.ReplyParameters.MessageId = im.MessageId
}
return b.SendMessage(im.Chat.Id, text, opts)
}
// ToMessage is a helper function to simplify dealing with telegram's message nonsense.
// It populates a standard message object with all of InaccessibleMessage's shared fields.
func (im InaccessibleMessage) ToMessage() *Message {
return &Message{
MessageId: im.MessageId,
Date: im.Date,
Chat: im.Chat,
}
}
// ToChat is a helper function to turn a ChatFullInfo struct into a Chat.
func (c ChatFullInfo) ToChat() Chat {
return Chat{
Id: c.Id,
Type: c.Type,
Title: c.Title,
Username: c.Username,
FirstName: c.FirstName,
LastName: c.LastName,
IsForum: c.IsForum,
}
}
// SendMessage is a helper function to easily call Bot.SendMessage in a chat.
func (c Chat) SendMessage(b *Bot, text string, opts *SendMessageOpts) (*Message, error) {
return b.SendMessage(c.Id, text, opts)
}
// Unban is a helper function to easily call Bot.UnbanChatMember in a chat.
func (c Chat) Unban(b *Bot, userId int64, opts *UnbanChatMemberOpts) (bool, error) {
return b.UnbanChatMember(c.Id, userId, opts)
}
// Promote is a helper function to easily call Bot.PromoteChatMember in a chat.
func (c Chat) Promote(b *Bot, userId int64, opts *PromoteChatMemberOpts) (bool, error) {
return b.PromoteChatMember(c.Id, userId, opts)
}
// URL gets the URL the file can be downloaded from.
func (f File) URL(b *Bot, opts *RequestOpts) string {
return b.FileURL(b.Token, f.FilePath, opts)
}
// IsJoinRequest returns true if ChatMemberUpdated originated from a join request; either from a direct join, or from an invitelink.
func (cm ChatMemberUpdated) IsJoinRequest() bool {
return cm.ViaJoinRequest || (cm.InviteLink != nil && cm.InviteLink.CreatesJoinRequest)
}
// unmarshalMaybeInaccessibleMessage is a JSON unmarshal helper to marshal the right structs into a
// MaybeInaccessibleMessage interface based on the Date field.
// This method is manually maintained due to special-case handling on the Date field rather than a specific type field.
func unmarshalMaybeInaccessibleMessage(d json.RawMessage) (MaybeInaccessibleMessage, error) {
if len(d) == 0 {
return nil, nil
}
t := struct {
Date int64
}{}
err := json.Unmarshal(d, &t)
if err != nil {
return nil, err
}
// As per the docs, date is always 0 for inaccessible messages:
// https://core.telegram.org/bots/api#inaccessiblemessage
if t.Date == 0 {
s := InaccessibleMessage{}
err := json.Unmarshal(d, &s)
if err != nil {
return nil, err
}
return s, nil
}
s := Message{}
err = json.Unmarshal(d, &s)
if err != nil {
return nil, err
}
return s, nil
}