Refactor APIs to add support for inline keyboards (not complete though)

This commit is contained in:
Hamcha 2018-12-12 17:30:38 +01:00
parent 0ddd8993c8
commit 10ef1429c3
Signed by: hamcha
GPG key ID: A40413D21021EAEE
4 changed files with 54 additions and 30 deletions

2
api.go
View file

@ -175,7 +175,7 @@ type APIInlineQueryResultPhoto struct {
Description string `json:"description,omitempty"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
ReplyMarkup *APIInlineKeyboardMarkup `json:"reply_markup,omitempty"`
ReplyMarkup interface{} `json:"reply_markup,omitempty"`
//TODO inputMessageContent
}

View file

@ -35,31 +35,47 @@ func (b *Broker) Close() {
b.Socket.Close()
}
type MessageOptions struct {
ReplyID *int64
ReplyMarkup interface{}
}
// SendTextMessage sends a HTML-styles text message to a chat.
// A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int64) {
b.sendCmd(ClientCommand{
Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{
func (b *Broker) SendTextMessage(chat *APIChat, text string, extra *MessageOptions) {
msgdata := &ClientTextMessageData{
Text: text,
ChatID: chat.ChatID,
ReplyID: original,
},
}
if extra != nil {
msgdata.ReplyID = extra.ReplyID
msgdata.ReplyMarkup = extra.ReplyMarkup
}
b.sendCmd(ClientCommand{
Type: CmdSendTextMessage,
TextMessageData: msgdata,
})
}
// SendPhoto sends a photo with an optional caption to a chat.
// A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, original *int64) {
b.sendCmd(ClientCommand{
Type: CmdSendPhoto,
PhotoData: &ClientPhotoData{
func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, extra *MessageOptions) {
photodata := &ClientPhotoData{
ChatID: chat.ChatID,
Filename: filename,
Bytes: base64.StdEncoding.EncodeToString(data),
Caption: caption,
ReplyID: original,
},
}
if extra != nil {
photodata.ReplyID = extra.ReplyID
}
b.sendCmd(ClientCommand{
Type: CmdSendPhoto,
PhotoData: photodata,
})
}

View file

@ -54,6 +54,7 @@ type ClientTextMessageData struct {
ChatID int64
Text string
ReplyID *int64 `json:",omitempty"`
ReplyMarkup interface{} `json:",omitempty"`
}
// ClientPhotoData is the required data for a CmdSendPhoto request

View file

@ -87,6 +87,13 @@ func (t Telegram) SendTextMessage(data ClientTextMessageData) {
if data.ReplyID != nil {
postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)}
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if !checkerr("SendTextMessage/json.Marshal", err) {
return
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err := http.PostForm(t.apiURL("sendMessage"), postdata)
checkerr("SendTextMessage/http.PostForm", err)