-
Notifications
You must be signed in to change notification settings - Fork 2
/
telebot_factory.go
93 lines (85 loc) · 2.1 KB
/
telebot_factory.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
package api
import (
"github.com/ailinykh/pullanusbot/v2/core"
tb "gopkg.in/telebot.v3"
)
func makeTbMessage(m *core.Message) *tb.Message {
message := &tb.Message{
ID: m.ID,
Chat: &tb.Chat{ID: m.Chat.ID},
Sender: makeTbUser(m.Sender),
}
if m.ReplyTo != nil {
message.ReplyTo = makeTbMessage(m.ReplyTo)
}
if m.Video != nil {
message.Video = makeTbVideo(m.Video, m.Text)
}
return message
}
func makeTbVideo(vf *core.Video, caption string) *tb.Video {
var video *tb.Video
if len(vf.ID) > 0 {
video = &tb.Video{File: tb.File{FileID: vf.ID}}
video.Caption = makeCaption(caption)
} else {
video = &tb.Video{File: tb.FromDisk(vf.Path)}
video.FileName = vf.File.Name
video.Width = vf.Width
video.Height = vf.Height
video.Caption = makeCaption(caption)
video.Duration = vf.Duration
video.Streaming = true
video.Thumbnail = &tb.Photo{
File: tb.FromDisk(vf.Thumb.Path),
Width: vf.Thumb.Width,
Height: vf.Thumb.Height,
}
}
return video
}
func makeTbPhoto(image *core.Image, caption string) *tb.Photo {
photo := &tb.Photo{File: tb.FromDisk(image.File.Path)}
if len(image.ID) > 0 {
photo = &tb.Photo{File: tb.File{FileID: image.ID}}
}
photo.Caption = makeCaption(caption)
return photo
}
func makeTbUser(user *core.User) *tb.User {
return &tb.User{
ID: user.ID,
FirstName: user.FirstName,
LastName: user.LastName,
Username: user.Username,
}
}
func makeInlineKeyboard(k core.Keyboard) [][]tb.InlineButton {
keyboard := [][]tb.InlineButton{}
for _, buttons := range k {
btns := []tb.InlineButton{}
for _, b := range buttons {
btn := tb.InlineButton{Unique: b.ID, Text: b.Text, Data: b.Payload}
btns = append(btns, btn)
}
keyboard = append(keyboard, btns)
}
return keyboard
}
func makeTbCommands(commands []core.Command) []tb.Command {
comands := []tb.Command{}
for _, command := range commands {
c := tb.Command{
Text: command.Text,
Description: command.Description,
}
comands = append(comands, c)
}
return comands
}
func makeCaption(caption string) string {
if len(caption) > 1024 {
return caption[:1024]
}
return caption
}