2015-06-26 06:26:24 +02:00
package tgbotapi
import (
2022-07-03 01:37:12 +02:00
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
2015-06-26 06:26:24 +02:00
"net/url"
2022-07-03 01:37:12 +02:00
"sort"
"strings"
2015-06-26 06:26:24 +02:00
)
2015-06-26 08:53:20 +02:00
// NewMessage creates a new Message.
2015-06-26 08:10:53 +02:00
//
2015-06-26 08:53:20 +02:00
// chatID is where to send it, text is the message text.
2016-03-24 19:22:40 +01:00
func NewMessage ( chatID int64 , text string ) MessageConfig {
2015-06-26 06:26:24 +02:00
return MessageConfig {
2016-01-03 23:54:24 +01:00
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
} ,
Text : text ,
LinkPreviewOptions : LinkPreviewOptions {
IsDisabled : false ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2018-09-22 03:20:28 +02:00
// NewDeleteMessage creates a request to delete a message.
2018-04-22 12:40:58 +02:00
func NewDeleteMessage ( chatID int64 , messageID int ) DeleteMessageConfig {
return DeleteMessageConfig {
2024-01-05 21:05:00 +01:00
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
2018-04-22 12:40:58 +02:00
}
}
2024-02-14 18:35:58 +01:00
// NewDeleteMessages creates a request to delete multiple messages. The messages have to be
// in the same chat. Provide the message ids as an array of integers
func NewDeleteMessages ( chatID int64 , messageIDs [ ] int ) DeleteMessagesConfig {
return DeleteMessagesConfig {
BaseChatMessages : BaseChatMessages {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageIDs : messageIDs ,
} ,
}
}
2016-04-22 17:03:00 +02:00
// NewMessageToChannel creates a new Message that is sent to a channel
// by username.
2016-11-25 06:50:35 +01:00
//
2020-11-06 05:29:48 +01:00
// username is the username of the channel, text is the message text,
// and the username should be in the form of `@username`.
2016-04-22 17:03:00 +02:00
func NewMessageToChannel ( username string , text string ) MessageConfig {
return MessageConfig {
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig {
ChannelUsername : username ,
} } ,
2016-04-22 17:03:00 +02:00
Text : text ,
}
}
2015-06-26 08:53:20 +02:00
// NewForward creates a new forward.
2015-06-26 08:10:53 +02:00
//
2015-06-26 08:53:20 +02:00
// chatID is where to send it, fromChatID is the source chat,
// and messageID is the ID of the original message.
2016-03-24 19:22:40 +01:00
func NewForward ( chatID int64 , fromChatID int64 , messageID int ) ForwardConfig {
2015-06-26 06:26:24 +02:00
return ForwardConfig {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
FromChat : ChatConfig { ChatID : fromChatID } ,
MessageID : messageID ,
2015-06-26 06:26:24 +02:00
}
}
2020-11-06 18:36:00 +01:00
// NewCopyMessage creates a new copy message.
//
// chatID is where to send it, fromChatID is the source chat,
// and messageID is the ID of the original message.
func NewCopyMessage ( chatID int64 , fromChatID int64 , messageID int ) CopyMessageConfig {
return CopyMessageConfig {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
FromChat : ChatConfig { ChatID : fromChatID } ,
MessageID : messageID ,
2020-11-06 18:36:00 +01:00
}
}
2020-07-26 02:29:40 +02:00
// NewPhoto creates a new sendPhoto request.
2015-06-26 08:10:53 +02:00
//
2016-01-03 23:54:24 +01:00
// chatID is where to send it, file is a string path to the file,
// FileReader, or FileBytes.
2016-01-08 16:16:47 +01:00
//
// Note that you must send animated GIFs as a document.
2021-08-20 21:31:52 +02:00
func NewPhoto ( chatID int64 , file RequestFileData ) PhotoConfig {
2015-06-26 06:26:24 +02:00
return PhotoConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2020-07-26 02:29:40 +02:00
// NewPhotoToChannel creates a new photo uploader to send a photo to a channel.
2015-06-26 08:10:53 +02:00
//
2019-09-01 21:00:09 +02:00
// Note that you must send animated GIFs as a document.
2021-08-20 21:31:52 +02:00
func NewPhotoToChannel ( username string , file RequestFileData ) PhotoConfig {
2015-06-26 06:26:24 +02:00
return PhotoConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChannelUsername : username } } ,
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2020-07-26 02:29:40 +02:00
// NewAudio creates a new sendAudio request.
2021-08-20 21:31:52 +02:00
func NewAudio ( chatID int64 , file RequestFileData ) AudioConfig {
2015-06-26 06:26:24 +02:00
return AudioConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2020-07-26 02:29:40 +02:00
// NewDocument creates a new sendDocument request.
2021-08-20 21:31:52 +02:00
func NewDocument ( chatID int64 , file RequestFileData ) DocumentConfig {
2015-06-26 06:26:24 +02:00
return DocumentConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2020-07-26 02:29:40 +02:00
// NewSticker creates a new sendSticker request.
2021-08-20 21:31:52 +02:00
func NewSticker ( chatID int64 , file RequestFileData ) StickerConfig {
2015-06-26 06:26:24 +02:00
return StickerConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2023-07-04 21:21:43 +02:00
// NewCustomEmojiStickerSetThumbnal creates a new setCustomEmojiStickerSetThumbnal request
2023-07-11 11:12:23 +02:00
func NewCustomEmojiStickerSetThumbnal ( name , customEmojiID string ) SetCustomEmojiStickerSetThumbnailConfig {
return SetCustomEmojiStickerSetThumbnailConfig {
2023-07-04 21:21:43 +02:00
Name : name ,
CustomEmojiID : customEmojiID ,
}
}
// NewStickerSetTitle creates a new setStickerSetTitle request
func NewStickerSetTitle ( name , title string ) SetStickerSetTitleConfig {
return SetStickerSetTitleConfig {
Name : name ,
Title : title ,
}
}
// NewDeleteStickerSet creates a new deleteStickerSet request
func NewDeleteStickerSet ( name , title string ) DeleteStickerSetConfig {
return DeleteStickerSetConfig {
Name : name ,
}
}
2020-07-26 02:29:40 +02:00
// NewVideo creates a new sendVideo request.
2021-08-20 21:31:52 +02:00
func NewVideo ( chatID int64 , file RequestFileData ) VideoConfig {
2015-06-26 06:26:24 +02:00
return VideoConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-06-26 06:26:24 +02:00
}
}
2020-07-26 02:29:40 +02:00
// NewAnimation creates a new sendAnimation request.
2021-08-20 21:31:52 +02:00
func NewAnimation ( chatID int64 , file RequestFileData ) AnimationConfig {
2018-09-06 14:44:42 +02:00
return AnimationConfig {
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2018-09-06 14:44:42 +02:00
} ,
}
}
2020-07-26 02:29:40 +02:00
// NewVideoNote creates a new sendVideoNote request.
2017-05-22 00:04:12 +02:00
//
// chatID is where to send it, file is a string path to the file,
// FileReader, or FileBytes.
2021-08-20 21:31:52 +02:00
func NewVideoNote ( chatID int64 , length int , file RequestFileData ) VideoNoteConfig {
2017-05-22 00:04:12 +02:00
return VideoNoteConfig {
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2017-05-22 00:04:12 +02:00
} ,
Length : length ,
}
}
2020-07-26 02:29:40 +02:00
// NewVoice creates a new sendVoice request.
2021-08-20 21:31:52 +02:00
func NewVoice ( chatID int64 , file RequestFileData ) VoiceConfig {
2015-08-18 03:40:42 +02:00
return VoiceConfig {
2016-01-03 23:54:24 +01:00
BaseFile : BaseFile {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2020-07-26 04:20:05 +02:00
File : file ,
2016-01-03 23:54:24 +01:00
} ,
2015-08-18 03:40:42 +02:00
}
}
2018-09-22 03:20:28 +02:00
// NewMediaGroup creates a new media group. Files should be an array of
// two to ten InputMediaPhoto or InputMediaVideo.
func NewMediaGroup ( chatID int64 , files [ ] interface { } ) MediaGroupConfig {
return MediaGroupConfig {
2022-11-06 00:32:26 +01:00
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2022-11-06 00:32:26 +01:00
} ,
2023-07-04 21:21:43 +02:00
Media : files ,
2018-09-22 03:20:28 +02:00
}
}
// NewInputMediaPhoto creates a new InputMediaPhoto.
2021-08-20 21:31:52 +02:00
func NewInputMediaPhoto ( media RequestFileData ) InputMediaPhoto {
2018-09-22 03:20:28 +02:00
return InputMediaPhoto {
2018-10-09 06:05:24 +02:00
BaseInputMedia {
Type : "photo" ,
Media : media ,
} ,
2018-09-22 03:20:28 +02:00
}
}
// NewInputMediaVideo creates a new InputMediaVideo.
2021-08-20 21:31:52 +02:00
func NewInputMediaVideo ( media RequestFileData ) InputMediaVideo {
2018-09-22 03:20:28 +02:00
return InputMediaVideo {
2018-10-09 06:05:24 +02:00
BaseInputMedia : BaseInputMedia {
Type : "video" ,
Media : media ,
} ,
}
}
// NewInputMediaAnimation creates a new InputMediaAnimation.
2021-08-20 21:31:52 +02:00
func NewInputMediaAnimation ( media RequestFileData ) InputMediaAnimation {
2018-10-09 06:05:24 +02:00
return InputMediaAnimation {
BaseInputMedia : BaseInputMedia {
Type : "animation" ,
Media : media ,
} ,
}
}
// NewInputMediaAudio creates a new InputMediaAudio.
2021-08-20 21:31:52 +02:00
func NewInputMediaAudio ( media RequestFileData ) InputMediaAudio {
2018-10-09 06:05:24 +02:00
return InputMediaAudio {
BaseInputMedia : BaseInputMedia {
Type : "audio" ,
Media : media ,
} ,
}
}
// NewInputMediaDocument creates a new InputMediaDocument.
2021-08-20 21:31:52 +02:00
func NewInputMediaDocument ( media RequestFileData ) InputMediaDocument {
2018-10-09 06:05:24 +02:00
return InputMediaDocument {
BaseInputMedia : BaseInputMedia {
Type : "document" ,
Media : media ,
} ,
2018-09-22 03:20:28 +02:00
}
}
2016-04-13 15:22:58 +02:00
// NewContact allows you to send a shared contact.
func NewContact ( chatID int64 , phoneNumber , firstName string ) ContactConfig {
return ContactConfig {
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2016-04-13 15:22:58 +02:00
} ,
PhoneNumber : phoneNumber ,
FirstName : firstName ,
}
}
2015-06-26 08:53:20 +02:00
// NewLocation shares your location.
2015-06-26 08:10:53 +02:00
//
2015-06-26 08:53:20 +02:00
// chatID is where to send it, latitude and longitude are coordinates.
2016-03-24 19:22:40 +01:00
func NewLocation ( chatID int64 , latitude float64 , longitude float64 ) LocationConfig {
2015-06-26 06:26:24 +02:00
return LocationConfig {
2016-01-03 23:54:24 +01:00
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2016-04-13 15:22:58 +02:00
} ,
Latitude : latitude ,
Longitude : longitude ,
}
}
// NewVenue allows you to send a venue and its location.
func NewVenue ( chatID int64 , title , address string , latitude , longitude float64 ) VenueConfig {
return VenueConfig {
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2016-01-03 23:54:24 +01:00
} ,
2016-04-13 15:22:58 +02:00
Title : title ,
Address : address ,
2015-11-20 16:30:50 +01:00
Latitude : latitude ,
Longitude : longitude ,
2015-06-26 06:26:24 +02:00
}
}
2015-06-26 08:53:20 +02:00
// NewChatAction sets a chat action.
2015-06-26 08:10:53 +02:00
// Actions last for 5 seconds, or until your next action.
//
2016-01-03 23:54:24 +01:00
// chatID is where to send it, action should be set via Chat constants.
2016-03-24 19:22:40 +01:00
func NewChatAction ( chatID int64 , action string ) ChatActionConfig {
2015-06-26 06:26:24 +02:00
return ChatActionConfig {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat { ChatConfig : ChatConfig { ChatID : chatID } } ,
2015-11-20 15:31:01 +01:00
Action : action ,
2015-06-26 06:26:24 +02:00
}
}
2015-06-26 08:53:20 +02:00
// NewUserProfilePhotos gets user profile photos.
2015-06-26 08:10:53 +02:00
//
2015-06-26 08:53:20 +02:00
// userID is the ID of the user you wish to get profile photos from.
2021-03-09 18:38:15 +01:00
func NewUserProfilePhotos ( userID int64 ) UserProfilePhotosConfig {
2015-06-26 06:26:24 +02:00
return UserProfilePhotosConfig {
2015-06-26 08:53:20 +02:00
UserID : userID ,
2015-06-26 06:26:24 +02:00
Offset : 0 ,
Limit : 0 ,
}
}
2015-06-26 08:53:20 +02:00
// NewUpdate gets updates since the last Offset.
2015-06-26 08:10:53 +02:00
//
2015-06-26 08:53:20 +02:00
// offset is the last Update ID to include.
// You likely want to set this to the last Update ID plus 1.
2015-06-26 06:26:24 +02:00
func NewUpdate ( offset int ) UpdateConfig {
return UpdateConfig {
Offset : offset ,
Limit : 0 ,
Timeout : 0 ,
}
}
2015-06-26 08:53:20 +02:00
// NewWebhook creates a new webhook.
2015-06-26 08:10:53 +02:00
//
// link is the url parsable link you wish to get the updates.
2020-09-30 22:19:29 +02:00
func NewWebhook ( link string ) ( WebhookConfig , error ) {
u , err := url . Parse ( link )
if err != nil {
return WebhookConfig { } , err
}
2015-06-26 06:26:24 +02:00
return WebhookConfig {
2015-11-21 12:26:39 +01:00
URL : u ,
2020-09-30 22:19:29 +02:00
} , nil
2015-06-26 06:26:24 +02:00
}
2015-09-07 18:44:29 +02:00
2015-09-07 19:47:31 +02:00
// NewWebhookWithCert creates a new webhook with a certificate.
2015-09-07 18:44:29 +02:00
//
// link is the url you wish to get webhooks,
2016-01-03 23:54:24 +01:00
// file contains a string to a file, FileReader, or FileBytes.
2021-08-20 21:31:52 +02:00
func NewWebhookWithCert ( link string , file RequestFileData ) ( WebhookConfig , error ) {
2020-09-30 22:19:29 +02:00
u , err := url . Parse ( link )
if err != nil {
return WebhookConfig { } , err
}
2015-09-07 18:44:29 +02:00
return WebhookConfig {
URL : u ,
Certificate : file ,
2020-09-30 22:19:29 +02:00
} , nil
2015-09-07 18:44:29 +02:00
}
2016-01-18 07:06:48 +01:00
// NewInlineQueryResultArticle creates a new inline query article.
2016-06-02 22:41:55 +02:00
func NewInlineQueryResultArticle ( id , title , messageText string ) InlineQueryResultArticle {
2016-01-18 07:06:48 +01:00
return InlineQueryResultArticle {
2016-04-12 15:56:05 +02:00
Type : "article" ,
ID : id ,
Title : title ,
InputMessageContent : InputTextMessageContent {
Text : messageText ,
2016-06-02 22:41:55 +02:00
} ,
}
}
// NewInlineQueryResultArticleMarkdown creates a new inline query article with Markdown parsing.
func NewInlineQueryResultArticleMarkdown ( id , title , messageText string ) InlineQueryResultArticle {
return InlineQueryResultArticle {
Type : "article" ,
ID : id ,
Title : title ,
InputMessageContent : InputTextMessageContent {
2016-06-02 22:57:39 +02:00
Text : messageText ,
2016-06-02 22:41:55 +02:00
ParseMode : "Markdown" ,
} ,
}
}
2020-07-01 19:03:05 +02:00
// NewInlineQueryResultArticleMarkdownV2 creates a new inline query article with MarkdownV2 parsing.
func NewInlineQueryResultArticleMarkdownV2 ( id , title , messageText string ) InlineQueryResultArticle {
return InlineQueryResultArticle {
Type : "article" ,
ID : id ,
Title : title ,
InputMessageContent : InputTextMessageContent {
Text : messageText ,
ParseMode : "MarkdownV2" ,
} ,
}
}
2016-06-02 22:41:55 +02:00
// NewInlineQueryResultArticleHTML creates a new inline query article with HTML parsing.
func NewInlineQueryResultArticleHTML ( id , title , messageText string ) InlineQueryResultArticle {
return InlineQueryResultArticle {
Type : "article" ,
ID : id ,
Title : title ,
InputMessageContent : InputTextMessageContent {
2016-06-02 22:57:39 +02:00
Text : messageText ,
2016-06-02 22:41:55 +02:00
ParseMode : "HTML" ,
2016-04-12 15:56:05 +02:00
} ,
2016-01-18 07:06:48 +01:00
}
}
// NewInlineQueryResultGIF creates a new inline query GIF.
func NewInlineQueryResultGIF ( id , url string ) InlineQueryResultGIF {
return InlineQueryResultGIF {
Type : "gif" ,
ID : id ,
URL : url ,
}
}
2018-07-04 12:48:19 +02:00
// NewInlineQueryResultCachedGIF create a new inline query with cached photo.
func NewInlineQueryResultCachedGIF ( id , gifID string ) InlineQueryResultCachedGIF {
return InlineQueryResultCachedGIF {
2018-07-04 12:49:37 +02:00
Type : "gif" ,
ID : id ,
2020-11-06 06:18:30 +01:00
GIFID : gifID ,
2018-07-04 12:48:19 +02:00
}
}
2016-01-18 07:06:48 +01:00
// NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
func NewInlineQueryResultMPEG4GIF ( id , url string ) InlineQueryResultMPEG4GIF {
return InlineQueryResultMPEG4GIF {
Type : "mpeg4_gif" ,
ID : id ,
URL : url ,
}
}
2020-11-06 05:29:48 +01:00
// NewInlineQueryResultCachedMPEG4GIF create a new inline query with cached MPEG4 GIF.
2020-11-06 06:18:30 +01:00
func NewInlineQueryResultCachedMPEG4GIF ( id , MPEG4GIFID string ) InlineQueryResultCachedMPEG4GIF {
2020-11-06 05:29:48 +01:00
return InlineQueryResultCachedMPEG4GIF {
Type : "mpeg4_gif" ,
ID : id ,
2020-11-06 06:18:30 +01:00
MPEG4FileID : MPEG4GIFID ,
2018-07-04 12:48:19 +02:00
}
}
2016-01-18 07:06:48 +01:00
// NewInlineQueryResultPhoto creates a new inline query photo.
func NewInlineQueryResultPhoto ( id , url string ) InlineQueryResultPhoto {
return InlineQueryResultPhoto {
Type : "photo" ,
ID : id ,
URL : url ,
}
}
2016-07-26 20:44:48 +02:00
// NewInlineQueryResultPhotoWithThumb creates a new inline query photo.
func NewInlineQueryResultPhotoWithThumb ( id , url , thumb string ) InlineQueryResultPhoto {
return InlineQueryResultPhoto {
Type : "photo" ,
ID : id ,
URL : url ,
ThumbURL : thumb ,
}
}
2018-07-04 12:48:19 +02:00
// NewInlineQueryResultCachedPhoto create a new inline query with cached photo.
func NewInlineQueryResultCachedPhoto ( id , photoID string ) InlineQueryResultCachedPhoto {
return InlineQueryResultCachedPhoto {
2018-07-04 12:49:37 +02:00
Type : "photo" ,
ID : id ,
2018-07-04 12:48:19 +02:00
PhotoID : photoID ,
}
}
2016-01-18 07:06:48 +01:00
// NewInlineQueryResultVideo creates a new inline query video.
func NewInlineQueryResultVideo ( id , url string ) InlineQueryResultVideo {
return InlineQueryResultVideo {
Type : "video" ,
ID : id ,
URL : url ,
}
}
2016-04-13 16:01:46 +02:00
2018-07-04 12:48:19 +02:00
// NewInlineQueryResultCachedVideo create a new inline query with cached video.
func NewInlineQueryResultCachedVideo ( id , videoID , title string ) InlineQueryResultCachedVideo {
return InlineQueryResultCachedVideo {
2018-07-04 12:49:37 +02:00
Type : "video" ,
ID : id ,
2018-07-04 12:48:19 +02:00
VideoID : videoID ,
2018-07-04 12:49:37 +02:00
Title : title ,
2018-07-04 12:48:19 +02:00
}
}
2020-01-05 06:35:15 +01:00
// NewInlineQueryResultCachedSticker create a new inline query with cached sticker.
func NewInlineQueryResultCachedSticker ( id , stickerID , title string ) InlineQueryResultCachedSticker {
return InlineQueryResultCachedSticker {
Type : "sticker" ,
ID : id ,
StickerID : stickerID ,
Title : title ,
}
}
2016-04-14 21:00:45 +02:00
// NewInlineQueryResultAudio creates a new inline query audio.
func NewInlineQueryResultAudio ( id , url , title string ) InlineQueryResultAudio {
return InlineQueryResultAudio {
Type : "audio" ,
ID : id ,
URL : url ,
Title : title ,
}
}
2018-07-04 12:48:19 +02:00
// NewInlineQueryResultCachedAudio create a new inline query with cached photo.
func NewInlineQueryResultCachedAudio ( id , audioID string ) InlineQueryResultCachedAudio {
return InlineQueryResultCachedAudio {
2018-07-04 12:49:37 +02:00
Type : "audio" ,
ID : id ,
2018-07-04 12:48:19 +02:00
AudioID : audioID ,
}
}
2016-04-14 21:00:45 +02:00
// NewInlineQueryResultVoice creates a new inline query voice.
func NewInlineQueryResultVoice ( id , url , title string ) InlineQueryResultVoice {
return InlineQueryResultVoice {
Type : "voice" ,
ID : id ,
URL : url ,
Title : title ,
}
}
2018-07-04 12:48:19 +02:00
// NewInlineQueryResultCachedVoice create a new inline query with cached photo.
func NewInlineQueryResultCachedVoice ( id , voiceID , title string ) InlineQueryResultCachedVoice {
return InlineQueryResultCachedVoice {
2018-07-04 12:49:37 +02:00
Type : "voice" ,
ID : id ,
2018-07-04 12:48:19 +02:00
VoiceID : voiceID ,
2018-07-04 12:49:37 +02:00
Title : title ,
2018-07-04 12:48:19 +02:00
}
}
2016-04-14 21:00:45 +02:00
// NewInlineQueryResultDocument creates a new inline query document.
func NewInlineQueryResultDocument ( id , url , title , mimeType string ) InlineQueryResultDocument {
return InlineQueryResultDocument {
Type : "document" ,
ID : id ,
URL : url ,
Title : title ,
MimeType : mimeType ,
}
}
2018-07-04 12:48:19 +02:00
// NewInlineQueryResultCachedDocument create a new inline query with cached photo.
func NewInlineQueryResultCachedDocument ( id , documentID , title string ) InlineQueryResultCachedDocument {
return InlineQueryResultCachedDocument {
2018-07-04 12:49:37 +02:00
Type : "document" ,
ID : id ,
2018-07-04 12:48:19 +02:00
DocumentID : documentID ,
2018-07-04 12:49:37 +02:00
Title : title ,
2018-07-04 12:48:19 +02:00
}
}
2016-04-14 21:00:45 +02:00
// NewInlineQueryResultLocation creates a new inline query location.
func NewInlineQueryResultLocation ( id , title string , latitude , longitude float64 ) InlineQueryResultLocation {
return InlineQueryResultLocation {
Type : "location" ,
ID : id ,
Title : title ,
Latitude : latitude ,
Longitude : longitude ,
}
}
2020-01-06 17:28:13 +01:00
// NewInlineQueryResultVenue creates a new inline query venue.
func NewInlineQueryResultVenue ( id , title , address string , latitude , longitude float64 ) InlineQueryResultVenue {
return InlineQueryResultVenue {
Type : "venue" ,
ID : id ,
Title : title ,
Address : address ,
Latitude : latitude ,
Longitude : longitude ,
}
}
2016-04-13 16:01:46 +02:00
// NewEditMessageText allows you to edit the text of a message.
func NewEditMessageText ( chatID int64 , messageID int , text string ) EditMessageTextConfig {
return EditMessageTextConfig {
BaseEdit : BaseEdit {
2024-01-05 21:05:00 +01:00
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
2016-04-13 16:01:46 +02:00
} ,
Text : text ,
}
}
2022-10-19 10:58:52 +02:00
// NewEditMessageTextAndMarkup allows you to edit the text and reply markup of a message.
2020-11-06 05:29:48 +01:00
func NewEditMessageTextAndMarkup ( chatID int64 , messageID int , text string , replyMarkup InlineKeyboardMarkup ) EditMessageTextConfig {
return EditMessageTextConfig {
BaseEdit : BaseEdit {
2024-01-05 21:05:00 +01:00
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
2020-11-06 05:29:48 +01:00
ReplyMarkup : & replyMarkup ,
} ,
Text : text ,
}
}
2016-04-13 16:01:46 +02:00
// NewEditMessageCaption allows you to edit the caption of a message.
func NewEditMessageCaption ( chatID int64 , messageID int , caption string ) EditMessageCaptionConfig {
return EditMessageCaptionConfig {
BaseEdit : BaseEdit {
2024-01-05 21:05:00 +01:00
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
2016-04-13 16:01:46 +02:00
} ,
Caption : caption ,
}
}
// NewEditMessageReplyMarkup allows you to edit the inline
// keyboard markup.
func NewEditMessageReplyMarkup ( chatID int64 , messageID int , replyMarkup InlineKeyboardMarkup ) EditMessageReplyMarkupConfig {
return EditMessageReplyMarkupConfig {
BaseEdit : BaseEdit {
2024-01-05 21:05:00 +01:00
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
2016-04-19 09:19:36 +02:00
ReplyMarkup : & replyMarkup ,
2016-04-13 16:01:46 +02:00
} ,
}
}
2016-04-14 22:59:28 +02:00
2016-11-25 06:50:35 +01:00
// NewRemoveKeyboard hides the keyboard, with the option for being selective
// or hiding for everyone.
func NewRemoveKeyboard ( selective bool ) ReplyKeyboardRemove {
return ReplyKeyboardRemove {
RemoveKeyboard : true ,
Selective : selective ,
}
}
2016-04-14 22:59:28 +02:00
// NewKeyboardButton creates a regular keyboard button.
func NewKeyboardButton ( text string ) KeyboardButton {
return KeyboardButton {
Text : text ,
}
}
2022-05-18 16:45:52 +02:00
// NewKeyboardButtonWebApp creates a keyboard button with text
// which goes to a WebApp.
func NewKeyboardButtonWebApp ( text string , webapp WebAppInfo ) KeyboardButton {
return KeyboardButton {
Text : text ,
WebApp : & webapp ,
}
}
2016-04-14 22:59:28 +02:00
// NewKeyboardButtonContact creates a keyboard button that requests
// user contact information upon click.
func NewKeyboardButtonContact ( text string ) KeyboardButton {
return KeyboardButton {
Text : text ,
RequestContact : true ,
}
}
// NewKeyboardButtonLocation creates a keyboard button that requests
// user location information upon click.
func NewKeyboardButtonLocation ( text string ) KeyboardButton {
return KeyboardButton {
Text : text ,
RequestLocation : true ,
}
}
// NewKeyboardButtonRow creates a row of keyboard buttons.
func NewKeyboardButtonRow ( buttons ... KeyboardButton ) [ ] KeyboardButton {
var row [ ] KeyboardButton
2016-04-21 09:40:12 +02:00
row = append ( row , buttons ... )
2016-04-14 22:59:28 +02:00
return row
}
// NewReplyKeyboard creates a new regular keyboard with sane defaults.
func NewReplyKeyboard ( rows ... [ ] KeyboardButton ) ReplyKeyboardMarkup {
var keyboard [ ] [ ] KeyboardButton
2016-04-21 09:40:12 +02:00
keyboard = append ( keyboard , rows ... )
2016-04-14 22:59:28 +02:00
return ReplyKeyboardMarkup {
ResizeKeyboard : true ,
Keyboard : keyboard ,
}
}
2020-07-21 10:35:48 +02:00
// NewOneTimeReplyKeyboard creates a new one time keyboard.
2019-08-11 12:20:40 +02:00
func NewOneTimeReplyKeyboard ( rows ... [ ] KeyboardButton ) ReplyKeyboardMarkup {
2020-07-21 21:20:12 +02:00
markup := NewReplyKeyboard ( rows ... )
markup . OneTimeKeyboard = true
return markup
2019-08-11 12:20:40 +02:00
}
2016-04-14 22:59:28 +02:00
// NewInlineKeyboardButtonData creates an inline keyboard button with text
// and data for a callback.
func NewInlineKeyboardButtonData ( text , data string ) InlineKeyboardButton {
return InlineKeyboardButton {
Text : text ,
CallbackData : & data ,
}
}
2022-05-18 16:45:52 +02:00
// NewInlineKeyboardButtonWebApp creates an inline keyboard button with text
// which goes to a WebApp.
func NewInlineKeyboardButtonWebApp ( text string , webapp WebAppInfo ) InlineKeyboardButton {
return InlineKeyboardButton {
Text : text ,
WebApp : & webapp ,
}
}
2023-07-05 10:23:14 +02:00
// NewInlineKeyboardButtonSwitchInlineQueryChoosenChat creates an inline keyboard button with text
// which goes to a SwitchInlineQueryChosenChat.
func NewInlineKeyboardButtonSwitchInlineQueryChoosenChat ( text string , switchInlineQueryChosenChat SwitchInlineQueryChosenChat ) InlineKeyboardButton {
return InlineKeyboardButton {
Text : text ,
SwitchInlineQueryChosenChat : & switchInlineQueryChosenChat ,
}
}
2019-09-06 15:41:36 +02:00
// NewInlineKeyboardButtonLoginURL creates an inline keyboard button with text
// which goes to a LoginURL.
2021-03-11 04:07:51 +01:00
func NewInlineKeyboardButtonLoginURL ( text string , loginURL LoginURL ) InlineKeyboardButton {
2019-09-06 15:41:36 +02:00
return InlineKeyboardButton {
Text : text ,
2021-03-11 04:07:51 +01:00
LoginURL : & loginURL ,
2019-09-06 15:41:36 +02:00
}
}
2021-03-11 04:01:47 +01:00
// NewInlineKeyboardButtonURL creates an inline keyboard button with text
2016-04-14 22:59:28 +02:00
// which goes to a URL.
func NewInlineKeyboardButtonURL ( text , url string ) InlineKeyboardButton {
return InlineKeyboardButton {
Text : text ,
URL : & url ,
}
}
// NewInlineKeyboardButtonSwitch creates an inline keyboard button with
// text which allows the user to switch to a chat or return to a chat.
func NewInlineKeyboardButtonSwitch ( text , sw string ) InlineKeyboardButton {
return InlineKeyboardButton {
Text : text ,
SwitchInlineQuery : & sw ,
}
}
// NewInlineKeyboardRow creates an inline keyboard row with buttons.
func NewInlineKeyboardRow ( buttons ... InlineKeyboardButton ) [ ] InlineKeyboardButton {
var row [ ] InlineKeyboardButton
2016-04-21 09:40:12 +02:00
row = append ( row , buttons ... )
2016-04-14 22:59:28 +02:00
return row
}
// NewInlineKeyboardMarkup creates a new inline keyboard.
func NewInlineKeyboardMarkup ( rows ... [ ] InlineKeyboardButton ) InlineKeyboardMarkup {
var keyboard [ ] [ ] InlineKeyboardButton
2016-04-21 09:40:12 +02:00
keyboard = append ( keyboard , rows ... )
2016-04-14 22:59:28 +02:00
return InlineKeyboardMarkup {
InlineKeyboard : keyboard ,
}
}
2016-04-14 23:30:41 +02:00
// NewCallback creates a new callback message.
func NewCallback ( id , text string ) CallbackConfig {
return CallbackConfig {
CallbackQueryID : id ,
Text : text ,
ShowAlert : false ,
}
}
// NewCallbackWithAlert creates a new callback message that alerts
// the user.
func NewCallbackWithAlert ( id , text string ) CallbackConfig {
return CallbackConfig {
CallbackQueryID : id ,
Text : text ,
ShowAlert : true ,
}
}
2017-06-02 22:18:42 +02:00
2018-01-09 04:51:17 +01:00
// NewInvoice creates a new Invoice request to the user.
2018-10-09 06:32:34 +02:00
func NewInvoice ( chatID int64 , title , description , payload , providerToken , startParameter , currency string , prices [ ] LabeledPrice ) InvoiceConfig {
2017-06-02 22:18:42 +02:00
return InvoiceConfig {
2024-01-05 21:05:00 +01:00
BaseChat : BaseChat {
ChatConfig : ChatConfig { ChatID : chatID } ,
} ,
2017-06-03 08:59:40 +02:00
Title : title ,
Description : description ,
Payload : payload ,
ProviderToken : providerToken ,
StartParameter : startParameter ,
Currency : currency ,
Prices : prices }
2017-06-02 22:18:42 +02:00
}
2018-03-05 16:04:37 +01:00
2018-03-26 19:30:16 +02:00
// NewChatTitle allows you to update the title of a chat.
func NewChatTitle ( chatID int64 , title string ) SetChatTitleConfig {
return SetChatTitleConfig {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
Title : title ,
2018-03-26 19:30:16 +02:00
}
}
// NewChatDescription allows you to update the description of a chat.
func NewChatDescription ( chatID int64 , description string ) SetChatDescriptionConfig {
return SetChatDescriptionConfig {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
2018-03-26 19:30:16 +02:00
Description : description ,
}
}
2024-01-07 08:37:27 +01:00
func NewPinChatMessage ( chatID int64 , messageID int , disableNotification bool ) PinChatMessageConfig {
return PinChatMessageConfig {
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
DisableNotification : disableNotification ,
}
}
func NewUnpinChatMessage ( chatID int64 , messageID int ) UnpinChatMessageConfig {
return UnpinChatMessageConfig {
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
}
}
func NewGetChatMember ( chatID , userID int64 ) GetChatMemberConfig {
return GetChatMemberConfig {
ChatConfigWithUser : ChatConfigWithUser {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
UserID : userID ,
} ,
}
}
func NewChatMember ( chatID , userID int64 ) ChatMemberConfig {
return ChatMemberConfig {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
UserID : userID ,
}
}
2018-03-26 19:30:16 +02:00
// NewChatPhoto allows you to update the photo for a chat.
2021-08-20 21:31:52 +02:00
func NewChatPhoto ( chatID int64 , photo RequestFileData ) SetChatPhotoConfig {
2018-03-26 19:30:16 +02:00
return SetChatPhotoConfig {
BaseFile : BaseFile {
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2018-03-26 19:30:16 +02:00
} ,
File : photo ,
} ,
}
}
// NewDeleteChatPhoto allows you to delete the photo for a chat.
2021-08-20 21:31:52 +02:00
func NewDeleteChatPhoto ( chatID int64 ) DeleteChatPhotoConfig {
2018-03-26 19:30:16 +02:00
return DeleteChatPhotoConfig {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
2018-03-26 19:30:16 +02:00
}
}
2019-04-14 21:46:45 +02:00
// NewPoll allows you to create a new poll.
func NewPoll ( chatID int64 , question string , options ... string ) SendPollConfig {
return SendPollConfig {
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2019-04-14 21:46:45 +02:00
} ,
2020-01-25 04:42:19 +01:00
Question : question ,
Options : options ,
IsAnonymous : true , // This is Telegram's default.
2019-04-14 21:46:45 +02:00
}
}
// NewStopPoll allows you to stop a poll.
func NewStopPoll ( chatID int64 , messageID int ) StopPollConfig {
return StopPollConfig {
BaseEdit {
2024-01-05 21:05:00 +01:00
BaseChatMessage : BaseChatMessage {
ChatConfig : ChatConfig {
ChatID : chatID ,
} ,
MessageID : messageID ,
} ,
2019-04-14 21:46:45 +02:00
} ,
}
}
2020-03-30 22:35:53 +02:00
2020-07-21 21:04:01 +02:00
// NewDice allows you to send a random dice roll.
func NewDice ( chatID int64 ) DiceConfig {
return DiceConfig {
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2020-07-21 21:04:01 +02:00
} ,
}
}
// NewDiceWithEmoji allows you to send a random roll of one of many types.
//
// Emoji may be 🎲 (1-6), 🎯 (1-6), or 🏀 (1-5).
func NewDiceWithEmoji ( chatID int64 , emoji string ) DiceConfig {
2020-03-30 22:35:53 +02:00
return DiceConfig {
2020-04-24 20:18:26 +02:00
BaseChat : BaseChat {
2024-01-05 21:05:00 +01:00
ChatConfig : ChatConfig { ChatID : chatID } ,
2020-03-30 22:35:53 +02:00
} ,
2020-07-21 21:04:01 +02:00
Emoji : emoji ,
2020-03-30 22:35:53 +02:00
}
}
2021-06-27 21:15:09 +02:00
// NewBotCommandScopeDefault represents the default scope of bot commands.
func NewBotCommandScopeDefault ( ) BotCommandScope {
return BotCommandScope { Type : "default" }
}
// NewBotCommandScopeAllPrivateChats represents the scope of bot commands,
// covering all private chats.
func NewBotCommandScopeAllPrivateChats ( ) BotCommandScope {
return BotCommandScope { Type : "all_private_chats" }
}
// NewBotCommandScopeAllGroupChats represents the scope of bot commands,
// covering all group and supergroup chats.
func NewBotCommandScopeAllGroupChats ( ) BotCommandScope {
return BotCommandScope { Type : "all_group_chats" }
}
// NewBotCommandScopeAllChatAdministrators represents the scope of bot commands,
// covering all group and supergroup chat administrators.
func NewBotCommandScopeAllChatAdministrators ( ) BotCommandScope {
return BotCommandScope { Type : "all_chat_administrators" }
}
// NewBotCommandScopeChat represents the scope of bot commands, covering a
// specific chat.
func NewBotCommandScopeChat ( chatID int64 ) BotCommandScope {
return BotCommandScope {
Type : "chat" ,
ChatID : chatID ,
}
}
// NewBotCommandScopeChatAdministrators represents the scope of bot commands,
// covering all administrators of a specific group or supergroup chat.
func NewBotCommandScopeChatAdministrators ( chatID int64 ) BotCommandScope {
return BotCommandScope {
Type : "chat_administrators" ,
ChatID : chatID ,
}
}
// NewBotCommandScopeChatMember represents the scope of bot commands, covering a
// specific member of a group or supergroup chat.
func NewBotCommandScopeChatMember ( chatID , userID int64 ) BotCommandScope {
return BotCommandScope {
Type : "chat_member" ,
ChatID : chatID ,
UserID : userID ,
}
}
2023-07-04 21:21:43 +02:00
// NewSetMyDescription allows you to change the bot's description, which is shown in the chat with the bot if the chat is empty.
func NewSetMyDescription ( description , languageCode string ) SetMyDescriptionConfig {
return SetMyDescriptionConfig {
Description : description ,
LanguageCode : languageCode ,
}
}
// NewGetMyDescription returns the current bot description for the given user language
func NewGetMyDescription ( languageCode string ) GetMyDescriptionConfig {
return GetMyDescriptionConfig {
LanguageCode : languageCode ,
}
}
// NewSetMyShortDescription allows you change the bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot.
func NewSetMyShortDescription ( shortDescription , languageCode string ) SetMyShortDescriptionConfig {
return SetMyShortDescriptionConfig {
ShortDescription : shortDescription ,
LanguageCode : languageCode ,
}
}
// NewGetMyShortDescription returns the current bot short description for the given user language.
func NewGetMyShortDescription ( languageCode string ) GetMyShortDescriptionConfig {
return GetMyShortDescriptionConfig {
LanguageCode : languageCode ,
}
}
2023-07-05 10:23:14 +02:00
// NewGetMyName get the current bot name for the given user language
func NewGetMyName ( languageCode string ) GetMyNameConfig {
return GetMyNameConfig {
LanguageCode : languageCode ,
}
}
// NewSetMyName change the bot's name
func NewSetMyName ( languageCode , name string ) SetMyNameConfig {
return SetMyNameConfig {
Name : name ,
LanguageCode : languageCode ,
}
}
2021-06-27 21:15:09 +02:00
// NewGetMyCommandsWithScope allows you to set the registered commands for a
// given scope.
func NewGetMyCommandsWithScope ( scope BotCommandScope ) GetMyCommandsConfig {
return GetMyCommandsConfig { Scope : & scope }
}
// NewGetMyCommandsWithScopeAndLanguage allows you to set the registered
// commands for a given scope and language code.
func NewGetMyCommandsWithScopeAndLanguage ( scope BotCommandScope , languageCode string ) GetMyCommandsConfig {
return GetMyCommandsConfig { Scope : & scope , LanguageCode : languageCode }
}
2020-03-30 22:35:53 +02:00
// NewSetMyCommands allows you to set the registered commands.
func NewSetMyCommands ( commands ... BotCommand ) SetMyCommandsConfig {
2021-06-27 21:15:09 +02:00
return SetMyCommandsConfig { Commands : commands }
}
2021-11-08 23:34:56 +01:00
// NewSetMyCommandsWithScope allows you to set the registered commands for a given scope.
2021-06-27 21:15:09 +02:00
func NewSetMyCommandsWithScope ( scope BotCommandScope , commands ... BotCommand ) SetMyCommandsConfig {
return SetMyCommandsConfig { Commands : commands , Scope : & scope }
}
2021-11-08 23:34:56 +01:00
// NewSetMyCommandsWithScopeAndLanguage allows you to set the registered commands for a given scope
2021-06-27 21:15:09 +02:00
// and language code.
func NewSetMyCommandsWithScopeAndLanguage ( scope BotCommandScope , languageCode string , commands ... BotCommand ) SetMyCommandsConfig {
return SetMyCommandsConfig { Commands : commands , Scope : & scope , LanguageCode : languageCode }
}
// NewDeleteMyCommands allows you to delete the registered commands.
func NewDeleteMyCommands ( ) DeleteMyCommandsConfig {
return DeleteMyCommandsConfig { }
}
2021-11-08 23:34:56 +01:00
// NewDeleteMyCommandsWithScope allows you to delete the registered commands for a given
2021-06-27 21:15:09 +02:00
// scope.
func NewDeleteMyCommandsWithScope ( scope BotCommandScope ) DeleteMyCommandsConfig {
return DeleteMyCommandsConfig { Scope : & scope }
}
2021-11-08 23:34:56 +01:00
// NewDeleteMyCommandsWithScopeAndLanguage allows you to delete the registered commands for a given
2021-06-27 21:15:09 +02:00
// scope and language code.
func NewDeleteMyCommandsWithScopeAndLanguage ( scope BotCommandScope , languageCode string ) DeleteMyCommandsConfig {
return DeleteMyCommandsConfig { Scope : & scope , LanguageCode : languageCode }
2020-03-30 22:35:53 +02:00
}
2022-07-03 01:37:12 +02:00
// ValidateWebAppData validate data received via the Web App
// https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
func ValidateWebAppData ( token , telegramInitData string ) ( bool , error ) {
initData , err := url . ParseQuery ( telegramInitData )
if err != nil {
return false , fmt . Errorf ( "error parsing data %w" , err )
}
dataCheckString := make ( [ ] string , 0 , len ( initData ) )
for k , v := range initData {
if k == "hash" {
continue
}
if len ( v ) > 0 {
dataCheckString = append ( dataCheckString , fmt . Sprintf ( "%s=%s" , k , v [ 0 ] ) )
}
}
sort . Strings ( dataCheckString )
secret := hmac . New ( sha256 . New , [ ] byte ( "WebAppData" ) )
secret . Write ( [ ] byte ( token ) )
hHash := hmac . New ( sha256 . New , secret . Sum ( nil ) )
hHash . Write ( [ ] byte ( strings . Join ( dataCheckString , "\n" ) ) )
hash := hex . EncodeToString ( hHash . Sum ( nil ) )
if initData . Get ( "hash" ) != hash {
return false , errors . New ( "hash not equal" )
}
return true , nil
}