forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
103 lines (82 loc) · 2 KB
/
util.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
package telebot
import (
"encoding/json"
"fmt"
"strconv"
"github.com/pkg/errors"
)
func extractMsgResponse(respJSON []byte) (*Message, error) {
var resp struct {
Ok bool
Result *Message
Description string
}
err := json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result, nil
}
func extractOkResponse(respJSON []byte) error {
var resp struct {
Ok bool
Description string
}
err := json.Unmarshal(respJSON, &resp)
if err != nil {
return errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return errors.Errorf("api error: %s", resp.Description)
}
return nil
}
func extractOptions(how []interface{}) *SendOptions {
var options *SendOptions
for _, object := range how {
switch option := object.(type) {
case *SendOptions:
options = option
break
case *ReplyMarkup:
if options == nil {
options = &SendOptions{}
}
options.ReplyMarkup = option
break
default:
panic(fmt.Sprintf("telebot: %v is not a send-option", option))
}
}
return options
}
func embedSendOptions(params map[string]string, opt *SendOptions) {
if opt == nil {
return
}
if opt.ReplyTo.ID != 0 {
params["reply_to_message_id"] = strconv.Itoa(opt.ReplyTo.ID)
}
if opt.DisableWebPagePreview {
params["disable_web_page_preview"] = "true"
}
if opt.DisableNotification {
params["disable_notification"] = "true"
}
if opt.ParseMode != ModeDefault {
params["parse_mode"] = string(opt.ParseMode)
}
if opt.ReplyMarkup != nil {
forceReply := opt.ReplyMarkup.ForceReply
customKeyboard := (opt.ReplyMarkup.CustomKeyboard != nil)
inlineKeyboard := opt.ReplyMarkup.InlineKeyboard != nil
hiddenKeyboard := opt.ReplyMarkup.HideCustomKeyboard
if forceReply || customKeyboard || hiddenKeyboard || inlineKeyboard {
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
}
}
}