1
0
telegram/toolbox.go

281 lines
5.9 KiB
Go
Raw Normal View History

2017-10-05 14:25:52 +00:00
package telegram
import (
"log"
2017-10-12 09:47:50 +00:00
"net/url"
"time"
2017-10-09 05:55:33 +00:00
2017-10-12 09:47:50 +00:00
router "github.com/buaazp/fasthttprouter"
2017-10-09 05:55:33 +00:00
json "github.com/pquerna/ffjson/ffjson"
2017-10-12 09:47:50 +00:00
http "github.com/valyala/fasthttp"
)
2017-10-12 09:47:50 +00:00
type UpdatesChannel <-chan *Update
2017-10-06 08:48:49 +00:00
func NewAnswerCallback(id string) *AnswerCallbackQueryParameters {
2017-10-12 09:47:50 +00:00
return &AnswerCallbackQueryParameters{
CallbackQueryID: id}
2017-10-06 08:48:49 +00:00
}
func NewAnswerInline(id string, results ...InlineQueryResult) *AnswerInlineQueryParameters {
2017-10-12 09:47:50 +00:00
return &AnswerInlineQueryParameters{
InlineQueryID: id,
Results: results,
}
2017-10-06 08:48:49 +00:00
}
func NewAnswerPreCheckout(id string, ok bool) *AnswerPreCheckoutQueryParameters {
2017-10-12 09:47:50 +00:00
return &AnswerPreCheckoutQueryParameters{
PreCheckoutQueryID: id,
Ok: ok,
}
2017-10-06 08:48:49 +00:00
}
func NewAnswerShipping(id string, ok bool) *AnswerShippingQueryParameters {
2017-10-12 09:47:50 +00:00
return &AnswerShippingQueryParameters{
ShippingQueryID: id,
Ok: ok,
}
2017-10-06 08:48:49 +00:00
}
2017-10-06 08:49:07 +00:00
func NewMessage(chatID int64, text string) *SendMessageParameters {
2017-10-12 09:47:50 +00:00
return &SendMessageParameters{
ChatID: chatID,
Text: text,
}
2017-10-06 08:49:07 +00:00
}
func NewMessageForward(from, to int64, messageID int) *ForwardMessageParameters {
2017-10-12 09:47:50 +00:00
return &ForwardMessageParameters{
FromChatID: from,
ChatID: to,
MessageID: messageID,
}
}
2017-10-06 08:49:23 +00:00
func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices ...LabeledPrice) *SendInvoiceParameters {
return &SendInvoiceParameters{
ChatID: chatID,
Title: title,
Description: description,
Payload: payload,
ProviderToken: providerToken,
StartParameter: startParameter,
Currency: currency,
Prices: prices,
}
}
2017-10-05 14:25:52 +00:00
func NewReplyKeyboard(rows ...[]KeyboardButton) *ReplyKeyboardMarkup {
var keyboard [][]KeyboardButton
keyboard = append(keyboard, rows...)
2017-10-12 09:47:50 +00:00
return &ReplyKeyboardMarkup{
Keyboard: keyboard,
ResizeKeyboard: true,
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewReplyKeyboardRow(buttons ...KeyboardButton) []KeyboardButton {
2017-10-05 14:25:52 +00:00
var row []KeyboardButton
row = append(row, buttons...)
return row
}
2017-10-09 05:55:33 +00:00
func NewReplyKeyboardButton(text string) KeyboardButton {
2017-10-12 09:47:50 +00:00
return KeyboardButton{
Text: text,
}
2017-10-09 05:55:33 +00:00
}
func NewReplyKeyboardButtonContact(text string) KeyboardButton {
2017-10-12 09:47:50 +00:00
return KeyboardButton{
Text: text,
RequestContact: true,
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewReplyKeyboardButtonLocation(text string) KeyboardButton {
2017-10-12 09:47:50 +00:00
return KeyboardButton{
Text: text,
RequestLocation: true,
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboard(rows ...[]InlineKeyboardButton) [][]InlineKeyboardButton {
var keyboard [][]InlineKeyboardButton
keyboard = append(keyboard, rows...)
return keyboard
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboardRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton {
2017-10-05 14:25:52 +00:00
var row []InlineKeyboardButton
row = append(row, buttons...)
return row
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboardButton(text, data string) InlineKeyboardButton {
2017-10-12 09:47:50 +00:00
return InlineKeyboardButton{
Text: text,
CallbackData: data,
}
2017-10-09 05:55:33 +00:00
}
func NewInlineKeyboardButtonURL(text, url string) InlineKeyboardButton {
2017-10-12 09:47:50 +00:00
return InlineKeyboardButton{
Text: text,
URL: url,
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboardButtonSwitch(text, query string) InlineKeyboardButton {
2017-10-12 09:47:50 +00:00
return InlineKeyboardButton{
Text: text,
SwitchInlineQuery: query,
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboardButtonSwitchSelf(text, query string) InlineKeyboardButton {
2017-10-12 09:47:50 +00:00
return InlineKeyboardButton{
Text: text,
SwitchInlineQueryCurrentChat: query,
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboardButtonGame(text string) InlineKeyboardButton {
2017-10-12 09:47:50 +00:00
return InlineKeyboardButton{
Text: text,
CallbackGame: &CallbackGame{},
}
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func NewInlineKeyboardButtonPay(text string) InlineKeyboardButton {
2017-10-12 09:47:50 +00:00
return InlineKeyboardButton{
Text: text,
Pay: true,
}
2017-10-05 14:25:52 +00:00
}
2017-10-12 09:47:50 +00:00
func NewWebhook(url string, file interface{}) *SetWebhookParameters {
var input InputFile
input = file
return &SetWebhookParameters{
URL: url,
Certificate: &input,
}
2017-10-05 14:25:52 +00:00
}
func (bot *Bot) NewWebhookChannel(endpoint string, params *GetUpdatesParameters) UpdatesChannel {
if params == nil {
params = &GetUpdatesParameters{
Limit: 100,
Timeout: 60,
}
}
channel := make(chan *Update, params.Limit)
/* From go-telegram-bot-api
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body)
var update Update
json.Unmarshal(bytes, &update)
channel <- update
})
*/
r := router.New()
r.Handle("POST", endpoint, func(ctx *http.RequestCtx) {
var update Update
json.Unmarshal(ctx.Request.Body(), &update)
channel <- &update
})
return channel
2017-10-05 14:25:52 +00:00
}
2017-10-09 05:55:33 +00:00
func (bot *Bot) NewLongPollingChannel(params *GetUpdatesParameters) UpdatesChannel {
if params == nil {
params = &GetUpdatesParameters{
Limit: 100,
Timeout: 60,
}
}
channel := make(chan *Update, params.Limit)
go func() {
for {
updates, err := bot.GetUpdates(params)
if err != nil {
log.Println(err.Error())
log.Println("failed to get updates, retrying in 3 seconds...")
time.Sleep(time.Second * 3)
continue
}
for _, update := range updates {
if update.ID >= params.Offset {
params.Offset = update.ID + 1
channel <- &update
}
}
}
}()
return channel
}
2017-10-09 05:56:44 +00:00
func (msg *Message) IsCommand() bool {
if len(msg.Entities) <= 0 {
return false
}
2017-10-12 09:47:50 +00:00
if msg.Entities[0].Type != EntityBotCommand &&
msg.Entities[0].Offset != 0 {
2017-10-09 05:56:44 +00:00
return false
}
return true
}
func (msg *Message) Command() string {
if len(msg.Entities) <= 0 {
return ""
}
2017-10-12 09:47:50 +00:00
if msg.Entities[0].Type != EntityBotCommand &&
msg.Entities[0].Offset != 0 {
2017-10-09 05:56:44 +00:00
return ""
}
2017-10-12 09:47:50 +00:00
return string([]rune(msg.Text)[:msg.Entities[0].Length])
2017-10-09 05:56:44 +00:00
}
func (chat *Chat) IsPrivate() bool {
return chat.Type == ChatPrivate
}
func (chat *Chat) IsGroup() bool {
return chat.Type == ChatGroup
}
func (chat *Chat) IsSuperGroup() bool {
return chat.Type == ChatSuperGroup
}
func (chat *Chat) IsChannel() bool {
return chat.Type == ChatChannel
}
func (entity *MessageEntity) ParseURL() (*url.URL, error) {
if entity.Type != EntityTextLink {
return nil, nil
}
return url.Parse(entity.URL)
}