-
-
Save toannd96/9f88a6043908478e8254998df2dd378b to your computer and use it in GitHub Desktop.
telegram-bot-api pagination example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" | |
) | |
var data = []string{"DummyData1", "DummyData2", "DummyData3", "DummyData4", "DummyData5", "DummyData6", "DummyData7", "DummyData8", "DummyData9", "DummyData10"} | |
var count = 2 | |
var maxPages = len(data) / count | |
var chatId = int64(0) // <--- Place Chat Id Here | |
var bot *tgbotapi.BotAPI | |
func main() { | |
var token = "" // <-- Place Bot Token Here | |
var err error | |
bot, err = tgbotapi.NewBotAPI(token) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
SendDummyData(chatId, 0, 2, nil) | |
u := tgbotapi.NewUpdate(0) | |
u.Timeout = 60 | |
updates, err := bot.GetUpdatesChan(u) | |
for update := range updates { | |
if update.CallbackQuery != nil { | |
CallbackQueryHandler(update.CallbackQuery) | |
continue | |
} | |
} | |
} | |
func CallbackQueryHandler(query *tgbotapi.CallbackQuery) { | |
split := strings.Split(query.Data, ":") | |
if split[0] == "pager" { | |
HandleNavigationCallbackQuery(query.Message.MessageID, split[1:]...) | |
return | |
} | |
} | |
func HandleNavigationCallbackQuery(messageId int, data ...string) { | |
pagerType := data[0] | |
currentPage, _ := strconv.Atoi(data[1]) | |
itemsPerPage, _ := strconv.Atoi(data[2]) | |
if pagerType == "next" { | |
nextPage := currentPage + 1 | |
if nextPage < maxPages { | |
SendDummyData(chatId, nextPage, itemsPerPage, &messageId) | |
} | |
} | |
if pagerType == "prev" { | |
previousPage := currentPage - 1 | |
if previousPage >= 0 { | |
SendDummyData(chatId, previousPage, itemsPerPage, &messageId) | |
} | |
} | |
} | |
func DummyDataTextMarkup(currentPage, count int) (text string, markup tgbotapi.InlineKeyboardMarkup) { | |
text = strings.Join(data[currentPage*count:currentPage*count+count], "\n") | |
var rows []tgbotapi.InlineKeyboardButton | |
if currentPage > 0 { | |
rows = append(rows, tgbotapi.NewInlineKeyboardButtonData("Previous", fmt.Sprintf("pager:prev:%d:%d", currentPage, count))) | |
} | |
if currentPage < maxPages-1 { | |
rows = append(rows, tgbotapi.NewInlineKeyboardButtonData("Next", fmt.Sprintf("pager:next:%d:%d", currentPage, count))) | |
} | |
markup = tgbotapi.NewInlineKeyboardMarkup(rows) | |
return | |
} | |
func SendDummyData(chatId int64, currentPage, count int, messageId *int) { | |
text, keyboard := DummyDataTextMarkup(currentPage, count) | |
var cfg tgbotapi.Chattable | |
if messageId == nil { | |
msg := tgbotapi.NewMessage(81997375, text) | |
msg.ReplyMarkup = keyboard | |
cfg = msg | |
} else { | |
msg := tgbotapi.NewEditMessageText(chatId, *messageId, text) | |
msg.ReplyMarkup = &keyboard | |
cfg = msg | |
} | |
bot.Send(cfg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment