configs.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  1. package tgbotapi
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/url"
  7. "os"
  8. "strconv"
  9. )
  10. // Telegram constants
  11. const (
  12. // APIEndpoint is the endpoint for all API methods,
  13. // with formatting for Sprintf.
  14. APIEndpoint = "https://api.telegram.org/bot%s/%s"
  15. // FileEndpoint is the endpoint for downloading a file from Telegram.
  16. FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
  17. )
  18. // Constant values for ChatActions
  19. const (
  20. ChatTyping = "typing"
  21. ChatUploadPhoto = "upload_photo"
  22. ChatRecordVideo = "record_video"
  23. ChatUploadVideo = "upload_video"
  24. ChatRecordVoice = "record_voice"
  25. ChatUploadVoice = "upload_voice"
  26. ChatUploadDocument = "upload_document"
  27. ChatChooseSticker = "choose_sticker"
  28. ChatFindLocation = "find_location"
  29. ChatRecordVideoNote = "record_video_note"
  30. ChatUploadVideoNote = "upload_video_note"
  31. )
  32. // API errors
  33. const (
  34. // ErrAPIForbidden happens when a token is bad
  35. ErrAPIForbidden = "forbidden"
  36. )
  37. // Constant values for ParseMode in MessageConfig
  38. const (
  39. ModeMarkdown = "Markdown"
  40. ModeMarkdownV2 = "MarkdownV2"
  41. ModeHTML = "HTML"
  42. )
  43. // Constant values for update types
  44. const (
  45. // UpdateTypeMessage is new incoming message of any kind — text, photo, sticker, etc.
  46. UpdateTypeMessage = "message"
  47. // UpdateTypeEditedMessage is new version of a message that is known to the bot and was edited
  48. UpdateTypeEditedMessage = "edited_message"
  49. // UpdateTypeChannelPost is new incoming channel post of any kind — text, photo, sticker, etc.
  50. UpdateTypeChannelPost = "channel_post"
  51. // UpdateTypeEditedChannelPost is new version of a channel post that is known to the bot and was edited
  52. UpdateTypeEditedChannelPost = "edited_channel_post"
  53. // UpdateTypeInlineQuery is new incoming inline query
  54. UpdateTypeInlineQuery = "inline_query"
  55. // UpdateTypeChosenInlineResult i the result of an inline query that was chosen by a user and sent to their
  56. // chat partner. Please see the documentation on the feedback collecting for
  57. // details on how to enable these updates for your bot.
  58. UpdateTypeChosenInlineResult = "chosen_inline_result"
  59. // UpdateTypeCallbackQuery is new incoming callback query
  60. UpdateTypeCallbackQuery = "callback_query"
  61. // UpdateTypeShippingQuery is new incoming shipping query. Only for invoices with flexible price
  62. UpdateTypeShippingQuery = "shipping_query"
  63. // UpdateTypePreCheckoutQuery is new incoming pre-checkout query. Contains full information about checkout
  64. UpdateTypePreCheckoutQuery = "pre_checkout_query"
  65. // UpdateTypePoll is new poll state. Bots receive only updates about stopped polls and polls
  66. // which are sent by the bot
  67. UpdateTypePoll = "poll"
  68. // UpdateTypePollAnswer is when user changed their answer in a non-anonymous poll. Bots receive new votes
  69. // only in polls that were sent by the bot itself.
  70. UpdateTypePollAnswer = "poll_answer"
  71. // UpdateTypeMyChatMember is when the bot's chat member status was updated in a chat. For private chats, this
  72. // update is received only when the bot is blocked or unblocked by the user.
  73. UpdateTypeMyChatMember = "my_chat_member"
  74. // UpdateTypeChatMember is when the bot must be an administrator in the chat and must explicitly specify
  75. // this update in the list of allowed_updates to receive these updates.
  76. UpdateTypeChatMember = "chat_member"
  77. )
  78. // Library errors
  79. const (
  80. ErrBadURL = "bad or empty url"
  81. )
  82. // Chattable is any config type that can be sent.
  83. type Chattable interface {
  84. params() (Params, error)
  85. method() string
  86. }
  87. // Fileable is any config type that can be sent that includes a file.
  88. type Fileable interface {
  89. Chattable
  90. files() []RequestFile
  91. }
  92. // RequestFile represents a file associated with a field name.
  93. type RequestFile struct {
  94. // The file field name.
  95. Name string
  96. // The file data to include.
  97. Data RequestFileData
  98. }
  99. // RequestFileData represents the data to be used for a file.
  100. type RequestFileData interface {
  101. // NeedsUpload shows if the file needs to be uploaded.
  102. NeedsUpload() bool
  103. // UploadData gets the file name and an `io.Reader` for the file to be uploaded. This
  104. // must only be called when the file needs to be uploaded.
  105. UploadData() (string, io.Reader, error)
  106. // SendData gets the file data to send when a file does not need to be uploaded. This
  107. // must only be called when the file does not need to be uploaded.
  108. SendData() string
  109. }
  110. // FileBytes contains information about a set of bytes to upload
  111. // as a File.
  112. type FileBytes struct {
  113. Name string
  114. Bytes []byte
  115. }
  116. func (fb FileBytes) NeedsUpload() bool {
  117. return true
  118. }
  119. func (fb FileBytes) UploadData() (string, io.Reader, error) {
  120. return fb.Name, bytes.NewReader(fb.Bytes), nil
  121. }
  122. func (fb FileBytes) SendData() string {
  123. panic("FileBytes must be uploaded")
  124. }
  125. // FileReader contains information about a reader to upload as a File.
  126. type FileReader struct {
  127. Name string
  128. Reader io.Reader
  129. }
  130. func (fr FileReader) NeedsUpload() bool {
  131. return true
  132. }
  133. func (fr FileReader) UploadData() (string, io.Reader, error) {
  134. return fr.Name, fr.Reader, nil
  135. }
  136. func (fr FileReader) SendData() string {
  137. panic("FileReader must be uploaded")
  138. }
  139. // FilePath is a path to a local file.
  140. type FilePath string
  141. func (fp FilePath) NeedsUpload() bool {
  142. return true
  143. }
  144. func (fp FilePath) UploadData() (string, io.Reader, error) {
  145. fileHandle, err := os.Open(string(fp))
  146. if err != nil {
  147. return "", nil, err
  148. }
  149. name := fileHandle.Name()
  150. return name, fileHandle, err
  151. }
  152. func (fp FilePath) SendData() string {
  153. panic("FilePath must be uploaded")
  154. }
  155. // FileURL is a URL to use as a file for a request.
  156. type FileURL string
  157. func (fu FileURL) NeedsUpload() bool {
  158. return false
  159. }
  160. func (fu FileURL) UploadData() (string, io.Reader, error) {
  161. panic("FileURL cannot be uploaded")
  162. }
  163. func (fu FileURL) SendData() string {
  164. return string(fu)
  165. }
  166. // FileID is an ID of a file already uploaded to Telegram.
  167. type FileID string
  168. func (fi FileID) NeedsUpload() bool {
  169. return false
  170. }
  171. func (fi FileID) UploadData() (string, io.Reader, error) {
  172. panic("FileID cannot be uploaded")
  173. }
  174. func (fi FileID) SendData() string {
  175. return string(fi)
  176. }
  177. // fileAttach is an internal file type used for processed media groups.
  178. type fileAttach string
  179. func (fa fileAttach) NeedsUpload() bool {
  180. return false
  181. }
  182. func (fa fileAttach) UploadData() (string, io.Reader, error) {
  183. panic("fileAttach cannot be uploaded")
  184. }
  185. func (fa fileAttach) SendData() string {
  186. return string(fa)
  187. }
  188. // LogOutConfig is a request to log out of the cloud Bot API server.
  189. //
  190. // Note that you may not log back in for at least 10 minutes.
  191. type LogOutConfig struct{}
  192. func (LogOutConfig) method() string {
  193. return "logOut"
  194. }
  195. func (LogOutConfig) params() (Params, error) {
  196. return nil, nil
  197. }
  198. // CloseConfig is a request to close the bot instance on a local server.
  199. //
  200. // Note that you may not close an instance for the first 10 minutes after the
  201. // bot has started.
  202. type CloseConfig struct{}
  203. func (CloseConfig) method() string {
  204. return "close"
  205. }
  206. func (CloseConfig) params() (Params, error) {
  207. return nil, nil
  208. }
  209. // BaseChat is base type for all chat config types.
  210. type BaseChat struct {
  211. ChatID int64 // required
  212. ChannelUsername string
  213. ProtectContent bool
  214. ReplyToMessageID int
  215. ReplyMarkup interface{}
  216. DisableNotification bool
  217. AllowSendingWithoutReply bool
  218. }
  219. func (chat *BaseChat) params() (Params, error) {
  220. params := make(Params)
  221. params.AddFirstValid("chat_id", chat.ChatID, chat.ChannelUsername)
  222. params.AddNonZero("reply_to_message_id", chat.ReplyToMessageID)
  223. params.AddBool("disable_notification", chat.DisableNotification)
  224. params.AddBool("allow_sending_without_reply", chat.AllowSendingWithoutReply)
  225. params.AddBool("protect_content", chat.ProtectContent)
  226. err := params.AddInterface("reply_markup", chat.ReplyMarkup)
  227. return params, err
  228. }
  229. // BaseFile is a base type for all file config types.
  230. type BaseFile struct {
  231. BaseChat
  232. File RequestFileData
  233. }
  234. func (file BaseFile) params() (Params, error) {
  235. return file.BaseChat.params()
  236. }
  237. // BaseEdit is base type of all chat edits.
  238. type BaseEdit struct {
  239. ChatID int64
  240. ChannelUsername string
  241. MessageID int
  242. InlineMessageID string
  243. ReplyMarkup *InlineKeyboardMarkup
  244. }
  245. func (edit BaseEdit) params() (Params, error) {
  246. params := make(Params)
  247. if edit.InlineMessageID != "" {
  248. params["inline_message_id"] = edit.InlineMessageID
  249. } else {
  250. params.AddFirstValid("chat_id", edit.ChatID, edit.ChannelUsername)
  251. params.AddNonZero("message_id", edit.MessageID)
  252. }
  253. err := params.AddInterface("reply_markup", edit.ReplyMarkup)
  254. return params, err
  255. }
  256. // MessageConfig contains information about a SendMessage request.
  257. type MessageConfig struct {
  258. BaseChat
  259. Text string
  260. ParseMode string
  261. Entities []MessageEntity
  262. DisableWebPagePreview bool
  263. }
  264. func (config MessageConfig) params() (Params, error) {
  265. params, err := config.BaseChat.params()
  266. if err != nil {
  267. return params, err
  268. }
  269. params.AddNonEmpty("text", config.Text)
  270. params.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
  271. params.AddNonEmpty("parse_mode", config.ParseMode)
  272. err = params.AddInterface("entities", config.Entities)
  273. return params, err
  274. }
  275. func (config MessageConfig) method() string {
  276. return "sendMessage"
  277. }
  278. // ForwardConfig contains information about a ForwardMessage request.
  279. type ForwardConfig struct {
  280. BaseChat
  281. FromChatID int64 // required
  282. FromChannelUsername string
  283. MessageID int // required
  284. }
  285. func (config ForwardConfig) params() (Params, error) {
  286. params, err := config.BaseChat.params()
  287. if err != nil {
  288. return params, err
  289. }
  290. params.AddNonZero64("from_chat_id", config.FromChatID)
  291. params.AddNonZero("message_id", config.MessageID)
  292. return params, nil
  293. }
  294. func (config ForwardConfig) method() string {
  295. return "forwardMessage"
  296. }
  297. // CopyMessageConfig contains information about a copyMessage request.
  298. type CopyMessageConfig struct {
  299. BaseChat
  300. FromChatID int64
  301. FromChannelUsername string
  302. MessageID int
  303. Caption string
  304. ParseMode string
  305. CaptionEntities []MessageEntity
  306. }
  307. func (config CopyMessageConfig) params() (Params, error) {
  308. params, err := config.BaseChat.params()
  309. if err != nil {
  310. return params, err
  311. }
  312. params.AddFirstValid("from_chat_id", config.FromChatID, config.FromChannelUsername)
  313. params.AddNonZero("message_id", config.MessageID)
  314. params.AddNonEmpty("caption", config.Caption)
  315. params.AddNonEmpty("parse_mode", config.ParseMode)
  316. err = params.AddInterface("caption_entities", config.CaptionEntities)
  317. return params, err
  318. }
  319. func (config CopyMessageConfig) method() string {
  320. return "copyMessage"
  321. }
  322. // PhotoConfig contains information about a SendPhoto request.
  323. type PhotoConfig struct {
  324. BaseFile
  325. Thumb RequestFileData
  326. Caption string
  327. ParseMode string
  328. CaptionEntities []MessageEntity
  329. }
  330. func (config PhotoConfig) params() (Params, error) {
  331. params, err := config.BaseFile.params()
  332. if err != nil {
  333. return params, err
  334. }
  335. params.AddNonEmpty("caption", config.Caption)
  336. params.AddNonEmpty("parse_mode", config.ParseMode)
  337. err = params.AddInterface("caption_entities", config.CaptionEntities)
  338. return params, err
  339. }
  340. func (config PhotoConfig) method() string {
  341. return "sendPhoto"
  342. }
  343. func (config PhotoConfig) files() []RequestFile {
  344. files := []RequestFile{{
  345. Name: "photo",
  346. Data: config.File,
  347. }}
  348. if config.Thumb != nil {
  349. files = append(files, RequestFile{
  350. Name: "thumb",
  351. Data: config.Thumb,
  352. })
  353. }
  354. return files
  355. }
  356. // AudioConfig contains information about a SendAudio request.
  357. type AudioConfig struct {
  358. BaseFile
  359. Thumb RequestFileData
  360. Caption string
  361. ParseMode string
  362. CaptionEntities []MessageEntity
  363. Duration int
  364. Performer string
  365. Title string
  366. }
  367. func (config AudioConfig) params() (Params, error) {
  368. params, err := config.BaseChat.params()
  369. if err != nil {
  370. return params, err
  371. }
  372. params.AddNonZero("duration", config.Duration)
  373. params.AddNonEmpty("performer", config.Performer)
  374. params.AddNonEmpty("title", config.Title)
  375. params.AddNonEmpty("caption", config.Caption)
  376. params.AddNonEmpty("parse_mode", config.ParseMode)
  377. err = params.AddInterface("caption_entities", config.CaptionEntities)
  378. return params, err
  379. }
  380. func (config AudioConfig) method() string {
  381. return "sendAudio"
  382. }
  383. func (config AudioConfig) files() []RequestFile {
  384. files := []RequestFile{{
  385. Name: "audio",
  386. Data: config.File,
  387. }}
  388. if config.Thumb != nil {
  389. files = append(files, RequestFile{
  390. Name: "thumb",
  391. Data: config.Thumb,
  392. })
  393. }
  394. return files
  395. }
  396. // DocumentConfig contains information about a SendDocument request.
  397. type DocumentConfig struct {
  398. BaseFile
  399. Thumb RequestFileData
  400. Caption string
  401. ParseMode string
  402. CaptionEntities []MessageEntity
  403. DisableContentTypeDetection bool
  404. }
  405. func (config DocumentConfig) params() (Params, error) {
  406. params, err := config.BaseFile.params()
  407. params.AddNonEmpty("caption", config.Caption)
  408. params.AddNonEmpty("parse_mode", config.ParseMode)
  409. params.AddBool("disable_content_type_detection", config.DisableContentTypeDetection)
  410. return params, err
  411. }
  412. func (config DocumentConfig) method() string {
  413. return "sendDocument"
  414. }
  415. func (config DocumentConfig) files() []RequestFile {
  416. files := []RequestFile{{
  417. Name: "document",
  418. Data: config.File,
  419. }}
  420. if config.Thumb != nil {
  421. files = append(files, RequestFile{
  422. Name: "thumb",
  423. Data: config.Thumb,
  424. })
  425. }
  426. return files
  427. }
  428. // StickerConfig contains information about a SendSticker request.
  429. type StickerConfig struct {
  430. BaseFile
  431. }
  432. func (config StickerConfig) params() (Params, error) {
  433. return config.BaseChat.params()
  434. }
  435. func (config StickerConfig) method() string {
  436. return "sendSticker"
  437. }
  438. func (config StickerConfig) files() []RequestFile {
  439. return []RequestFile{{
  440. Name: "sticker",
  441. Data: config.File,
  442. }}
  443. }
  444. // VideoConfig contains information about a SendVideo request.
  445. type VideoConfig struct {
  446. BaseFile
  447. Thumb RequestFileData
  448. Duration int
  449. Caption string
  450. ParseMode string
  451. CaptionEntities []MessageEntity
  452. SupportsStreaming bool
  453. }
  454. func (config VideoConfig) params() (Params, error) {
  455. params, err := config.BaseChat.params()
  456. if err != nil {
  457. return params, err
  458. }
  459. params.AddNonZero("duration", config.Duration)
  460. params.AddNonEmpty("caption", config.Caption)
  461. params.AddNonEmpty("parse_mode", config.ParseMode)
  462. params.AddBool("supports_streaming", config.SupportsStreaming)
  463. err = params.AddInterface("caption_entities", config.CaptionEntities)
  464. return params, err
  465. }
  466. func (config VideoConfig) method() string {
  467. return "sendVideo"
  468. }
  469. func (config VideoConfig) files() []RequestFile {
  470. files := []RequestFile{{
  471. Name: "video",
  472. Data: config.File,
  473. }}
  474. if config.Thumb != nil {
  475. files = append(files, RequestFile{
  476. Name: "thumb",
  477. Data: config.Thumb,
  478. })
  479. }
  480. return files
  481. }
  482. // AnimationConfig contains information about a SendAnimation request.
  483. type AnimationConfig struct {
  484. BaseFile
  485. Duration int
  486. Thumb RequestFileData
  487. Caption string
  488. ParseMode string
  489. CaptionEntities []MessageEntity
  490. }
  491. func (config AnimationConfig) params() (Params, error) {
  492. params, err := config.BaseChat.params()
  493. if err != nil {
  494. return params, err
  495. }
  496. params.AddNonZero("duration", config.Duration)
  497. params.AddNonEmpty("caption", config.Caption)
  498. params.AddNonEmpty("parse_mode", config.ParseMode)
  499. err = params.AddInterface("caption_entities", config.CaptionEntities)
  500. return params, err
  501. }
  502. func (config AnimationConfig) method() string {
  503. return "sendAnimation"
  504. }
  505. func (config AnimationConfig) files() []RequestFile {
  506. files := []RequestFile{{
  507. Name: "animation",
  508. Data: config.File,
  509. }}
  510. if config.Thumb != nil {
  511. files = append(files, RequestFile{
  512. Name: "thumb",
  513. Data: config.Thumb,
  514. })
  515. }
  516. return files
  517. }
  518. // VideoNoteConfig contains information about a SendVideoNote request.
  519. type VideoNoteConfig struct {
  520. BaseFile
  521. Thumb RequestFileData
  522. Duration int
  523. Length int
  524. }
  525. func (config VideoNoteConfig) params() (Params, error) {
  526. params, err := config.BaseChat.params()
  527. params.AddNonZero("duration", config.Duration)
  528. params.AddNonZero("length", config.Length)
  529. return params, err
  530. }
  531. func (config VideoNoteConfig) method() string {
  532. return "sendVideoNote"
  533. }
  534. func (config VideoNoteConfig) files() []RequestFile {
  535. files := []RequestFile{{
  536. Name: "video_note",
  537. Data: config.File,
  538. }}
  539. if config.Thumb != nil {
  540. files = append(files, RequestFile{
  541. Name: "thumb",
  542. Data: config.Thumb,
  543. })
  544. }
  545. return files
  546. }
  547. // VoiceConfig contains information about a SendVoice request.
  548. type VoiceConfig struct {
  549. BaseFile
  550. Thumb RequestFileData
  551. Caption string
  552. ParseMode string
  553. CaptionEntities []MessageEntity
  554. Duration int
  555. }
  556. func (config VoiceConfig) params() (Params, error) {
  557. params, err := config.BaseChat.params()
  558. if err != nil {
  559. return params, err
  560. }
  561. params.AddNonZero("duration", config.Duration)
  562. params.AddNonEmpty("caption", config.Caption)
  563. params.AddNonEmpty("parse_mode", config.ParseMode)
  564. err = params.AddInterface("caption_entities", config.CaptionEntities)
  565. return params, err
  566. }
  567. func (config VoiceConfig) method() string {
  568. return "sendVoice"
  569. }
  570. func (config VoiceConfig) files() []RequestFile {
  571. files := []RequestFile{{
  572. Name: "voice",
  573. Data: config.File,
  574. }}
  575. if config.Thumb != nil {
  576. files = append(files, RequestFile{
  577. Name: "thumb",
  578. Data: config.Thumb,
  579. })
  580. }
  581. return files
  582. }
  583. // LocationConfig contains information about a SendLocation request.
  584. type LocationConfig struct {
  585. BaseChat
  586. Latitude float64 // required
  587. Longitude float64 // required
  588. HorizontalAccuracy float64 // optional
  589. LivePeriod int // optional
  590. Heading int // optional
  591. ProximityAlertRadius int // optional
  592. }
  593. func (config LocationConfig) params() (Params, error) {
  594. params, err := config.BaseChat.params()
  595. params.AddNonZeroFloat("latitude", config.Latitude)
  596. params.AddNonZeroFloat("longitude", config.Longitude)
  597. params.AddNonZeroFloat("horizontal_accuracy", config.HorizontalAccuracy)
  598. params.AddNonZero("live_period", config.LivePeriod)
  599. params.AddNonZero("heading", config.Heading)
  600. params.AddNonZero("proximity_alert_radius", config.ProximityAlertRadius)
  601. return params, err
  602. }
  603. func (config LocationConfig) method() string {
  604. return "sendLocation"
  605. }
  606. // EditMessageLiveLocationConfig allows you to update a live location.
  607. type EditMessageLiveLocationConfig struct {
  608. BaseEdit
  609. Latitude float64 // required
  610. Longitude float64 // required
  611. HorizontalAccuracy float64 // optional
  612. Heading int // optional
  613. ProximityAlertRadius int // optional
  614. }
  615. func (config EditMessageLiveLocationConfig) params() (Params, error) {
  616. params, err := config.BaseEdit.params()
  617. params.AddNonZeroFloat("latitude", config.Latitude)
  618. params.AddNonZeroFloat("longitude", config.Longitude)
  619. params.AddNonZeroFloat("horizontal_accuracy", config.HorizontalAccuracy)
  620. params.AddNonZero("heading", config.Heading)
  621. params.AddNonZero("proximity_alert_radius", config.ProximityAlertRadius)
  622. return params, err
  623. }
  624. func (config EditMessageLiveLocationConfig) method() string {
  625. return "editMessageLiveLocation"
  626. }
  627. // StopMessageLiveLocationConfig stops updating a live location.
  628. type StopMessageLiveLocationConfig struct {
  629. BaseEdit
  630. }
  631. func (config StopMessageLiveLocationConfig) params() (Params, error) {
  632. return config.BaseEdit.params()
  633. }
  634. func (config StopMessageLiveLocationConfig) method() string {
  635. return "stopMessageLiveLocation"
  636. }
  637. // VenueConfig contains information about a SendVenue request.
  638. type VenueConfig struct {
  639. BaseChat
  640. Latitude float64 // required
  641. Longitude float64 // required
  642. Title string // required
  643. Address string // required
  644. FoursquareID string
  645. FoursquareType string
  646. GooglePlaceID string
  647. GooglePlaceType string
  648. }
  649. func (config VenueConfig) params() (Params, error) {
  650. params, err := config.BaseChat.params()
  651. params.AddNonZeroFloat("latitude", config.Latitude)
  652. params.AddNonZeroFloat("longitude", config.Longitude)
  653. params["title"] = config.Title
  654. params["address"] = config.Address
  655. params.AddNonEmpty("foursquare_id", config.FoursquareID)
  656. params.AddNonEmpty("foursquare_type", config.FoursquareType)
  657. params.AddNonEmpty("google_place_id", config.GooglePlaceID)
  658. params.AddNonEmpty("google_place_type", config.GooglePlaceType)
  659. return params, err
  660. }
  661. func (config VenueConfig) method() string {
  662. return "sendVenue"
  663. }
  664. // ContactConfig allows you to send a contact.
  665. type ContactConfig struct {
  666. BaseChat
  667. PhoneNumber string
  668. FirstName string
  669. LastName string
  670. VCard string
  671. }
  672. func (config ContactConfig) params() (Params, error) {
  673. params, err := config.BaseChat.params()
  674. params["phone_number"] = config.PhoneNumber
  675. params["first_name"] = config.FirstName
  676. params.AddNonEmpty("last_name", config.LastName)
  677. params.AddNonEmpty("vcard", config.VCard)
  678. return params, err
  679. }
  680. func (config ContactConfig) method() string {
  681. return "sendContact"
  682. }
  683. // SendPollConfig allows you to send a poll.
  684. type SendPollConfig struct {
  685. BaseChat
  686. Question string
  687. Options []string
  688. IsAnonymous bool
  689. Type string
  690. AllowsMultipleAnswers bool
  691. CorrectOptionID int64
  692. Explanation string
  693. ExplanationParseMode string
  694. ExplanationEntities []MessageEntity
  695. OpenPeriod int
  696. CloseDate int
  697. IsClosed bool
  698. }
  699. func (config SendPollConfig) params() (Params, error) {
  700. params, err := config.BaseChat.params()
  701. if err != nil {
  702. return params, err
  703. }
  704. params["question"] = config.Question
  705. if err = params.AddInterface("options", config.Options); err != nil {
  706. return params, err
  707. }
  708. params["is_anonymous"] = strconv.FormatBool(config.IsAnonymous)
  709. params.AddNonEmpty("type", config.Type)
  710. params["allows_multiple_answers"] = strconv.FormatBool(config.AllowsMultipleAnswers)
  711. params["correct_option_id"] = strconv.FormatInt(config.CorrectOptionID, 10)
  712. params.AddBool("is_closed", config.IsClosed)
  713. params.AddNonEmpty("explanation", config.Explanation)
  714. params.AddNonEmpty("explanation_parse_mode", config.ExplanationParseMode)
  715. params.AddNonZero("open_period", config.OpenPeriod)
  716. params.AddNonZero("close_date", config.CloseDate)
  717. err = params.AddInterface("explanation_entities", config.ExplanationEntities)
  718. return params, err
  719. }
  720. func (SendPollConfig) method() string {
  721. return "sendPoll"
  722. }
  723. // GameConfig allows you to send a game.
  724. type GameConfig struct {
  725. BaseChat
  726. GameShortName string
  727. }
  728. func (config GameConfig) params() (Params, error) {
  729. params, err := config.BaseChat.params()
  730. params["game_short_name"] = config.GameShortName
  731. return params, err
  732. }
  733. func (config GameConfig) method() string {
  734. return "sendGame"
  735. }
  736. // SetGameScoreConfig allows you to update the game score in a chat.
  737. type SetGameScoreConfig struct {
  738. UserID int64
  739. Score int
  740. Force bool
  741. DisableEditMessage bool
  742. ChatID int64
  743. ChannelUsername string
  744. MessageID int
  745. InlineMessageID string
  746. }
  747. func (config SetGameScoreConfig) params() (Params, error) {
  748. params := make(Params)
  749. params.AddNonZero64("user_id", config.UserID)
  750. params.AddNonZero("scrore", config.Score)
  751. params.AddBool("disable_edit_message", config.DisableEditMessage)
  752. if config.InlineMessageID != "" {
  753. params["inline_message_id"] = config.InlineMessageID
  754. } else {
  755. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  756. params.AddNonZero("message_id", config.MessageID)
  757. }
  758. return params, nil
  759. }
  760. func (config SetGameScoreConfig) method() string {
  761. return "setGameScore"
  762. }
  763. // GetGameHighScoresConfig allows you to fetch the high scores for a game.
  764. type GetGameHighScoresConfig struct {
  765. UserID int64
  766. ChatID int64
  767. ChannelUsername string
  768. MessageID int
  769. InlineMessageID string
  770. }
  771. func (config GetGameHighScoresConfig) params() (Params, error) {
  772. params := make(Params)
  773. params.AddNonZero64("user_id", config.UserID)
  774. if config.InlineMessageID != "" {
  775. params["inline_message_id"] = config.InlineMessageID
  776. } else {
  777. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  778. params.AddNonZero("message_id", config.MessageID)
  779. }
  780. return params, nil
  781. }
  782. func (config GetGameHighScoresConfig) method() string {
  783. return "getGameHighScores"
  784. }
  785. // ChatActionConfig contains information about a SendChatAction request.
  786. type ChatActionConfig struct {
  787. BaseChat
  788. Action string // required
  789. }
  790. func (config ChatActionConfig) params() (Params, error) {
  791. params, err := config.BaseChat.params()
  792. params["action"] = config.Action
  793. return params, err
  794. }
  795. func (config ChatActionConfig) method() string {
  796. return "sendChatAction"
  797. }
  798. // EditMessageTextConfig allows you to modify the text in a message.
  799. type EditMessageTextConfig struct {
  800. BaseEdit
  801. Text string
  802. ParseMode string
  803. Entities []MessageEntity
  804. DisableWebPagePreview bool
  805. }
  806. func (config EditMessageTextConfig) params() (Params, error) {
  807. params, err := config.BaseEdit.params()
  808. if err != nil {
  809. return params, err
  810. }
  811. params["text"] = config.Text
  812. params.AddNonEmpty("parse_mode", config.ParseMode)
  813. params.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
  814. err = params.AddInterface("entities", config.Entities)
  815. return params, err
  816. }
  817. func (config EditMessageTextConfig) method() string {
  818. return "editMessageText"
  819. }
  820. // EditMessageCaptionConfig allows you to modify the caption of a message.
  821. type EditMessageCaptionConfig struct {
  822. BaseEdit
  823. Caption string
  824. ParseMode string
  825. CaptionEntities []MessageEntity
  826. }
  827. func (config EditMessageCaptionConfig) params() (Params, error) {
  828. params, err := config.BaseEdit.params()
  829. if err != nil {
  830. return params, err
  831. }
  832. params["caption"] = config.Caption
  833. params.AddNonEmpty("parse_mode", config.ParseMode)
  834. err = params.AddInterface("caption_entities", config.CaptionEntities)
  835. return params, err
  836. }
  837. func (config EditMessageCaptionConfig) method() string {
  838. return "editMessageCaption"
  839. }
  840. // EditMessageMediaConfig allows you to make an editMessageMedia request.
  841. type EditMessageMediaConfig struct {
  842. BaseEdit
  843. Media interface{}
  844. }
  845. func (EditMessageMediaConfig) method() string {
  846. return "editMessageMedia"
  847. }
  848. func (config EditMessageMediaConfig) params() (Params, error) {
  849. params, err := config.BaseEdit.params()
  850. if err != nil {
  851. return params, err
  852. }
  853. err = params.AddInterface("media", prepareInputMediaParam(config.Media, 0))
  854. return params, err
  855. }
  856. func (config EditMessageMediaConfig) files() []RequestFile {
  857. return prepareInputMediaFile(config.Media, 0)
  858. }
  859. // EditMessageReplyMarkupConfig allows you to modify the reply markup
  860. // of a message.
  861. type EditMessageReplyMarkupConfig struct {
  862. BaseEdit
  863. }
  864. func (config EditMessageReplyMarkupConfig) params() (Params, error) {
  865. return config.BaseEdit.params()
  866. }
  867. func (config EditMessageReplyMarkupConfig) method() string {
  868. return "editMessageReplyMarkup"
  869. }
  870. // StopPollConfig allows you to stop a poll sent by the bot.
  871. type StopPollConfig struct {
  872. BaseEdit
  873. }
  874. func (config StopPollConfig) params() (Params, error) {
  875. return config.BaseEdit.params()
  876. }
  877. func (StopPollConfig) method() string {
  878. return "stopPoll"
  879. }
  880. // UserProfilePhotosConfig contains information about a
  881. // GetUserProfilePhotos request.
  882. type UserProfilePhotosConfig struct {
  883. UserID int64
  884. Offset int
  885. Limit int
  886. }
  887. func (UserProfilePhotosConfig) method() string {
  888. return "getUserProfilePhotos"
  889. }
  890. func (config UserProfilePhotosConfig) params() (Params, error) {
  891. params := make(Params)
  892. params.AddNonZero64("user_id", config.UserID)
  893. params.AddNonZero("offset", config.Offset)
  894. params.AddNonZero("limit", config.Limit)
  895. return params, nil
  896. }
  897. // FileConfig has information about a file hosted on Telegram.
  898. type FileConfig struct {
  899. FileID string
  900. }
  901. func (FileConfig) method() string {
  902. return "getFile"
  903. }
  904. func (config FileConfig) params() (Params, error) {
  905. params := make(Params)
  906. params["file_id"] = config.FileID
  907. return params, nil
  908. }
  909. // UpdateConfig contains information about a GetUpdates request.
  910. type UpdateConfig struct {
  911. Offset int
  912. Limit int
  913. Timeout int
  914. AllowedUpdates []string
  915. }
  916. func (UpdateConfig) method() string {
  917. return "getUpdates"
  918. }
  919. func (config UpdateConfig) params() (Params, error) {
  920. params := make(Params)
  921. params.AddNonZero("offset", config.Offset)
  922. params.AddNonZero("limit", config.Limit)
  923. params.AddNonZero("timeout", config.Timeout)
  924. params.AddInterface("allowed_updates", config.AllowedUpdates)
  925. return params, nil
  926. }
  927. // WebhookConfig contains information about a SetWebhook request.
  928. type WebhookConfig struct {
  929. URL *url.URL
  930. Certificate RequestFileData
  931. IPAddress string
  932. MaxConnections int
  933. AllowedUpdates []string
  934. DropPendingUpdates bool
  935. }
  936. func (config WebhookConfig) method() string {
  937. return "setWebhook"
  938. }
  939. func (config WebhookConfig) params() (Params, error) {
  940. params := make(Params)
  941. if config.URL != nil {
  942. params["url"] = config.URL.String()
  943. }
  944. params.AddNonEmpty("ip_address", config.IPAddress)
  945. params.AddNonZero("max_connections", config.MaxConnections)
  946. err := params.AddInterface("allowed_updates", config.AllowedUpdates)
  947. params.AddBool("drop_pending_updates", config.DropPendingUpdates)
  948. return params, err
  949. }
  950. func (config WebhookConfig) files() []RequestFile {
  951. if config.Certificate != nil {
  952. return []RequestFile{{
  953. Name: "certificate",
  954. Data: config.Certificate,
  955. }}
  956. }
  957. return nil
  958. }
  959. // DeleteWebhookConfig is a helper to delete a webhook.
  960. type DeleteWebhookConfig struct {
  961. DropPendingUpdates bool
  962. }
  963. func (config DeleteWebhookConfig) method() string {
  964. return "deleteWebhook"
  965. }
  966. func (config DeleteWebhookConfig) params() (Params, error) {
  967. params := make(Params)
  968. params.AddBool("drop_pending_updates", config.DropPendingUpdates)
  969. return params, nil
  970. }
  971. // InlineConfig contains information on making an InlineQuery response.
  972. type InlineConfig struct {
  973. InlineQueryID string `json:"inline_query_id"`
  974. Results []interface{} `json:"results"`
  975. CacheTime int `json:"cache_time"`
  976. IsPersonal bool `json:"is_personal"`
  977. NextOffset string `json:"next_offset"`
  978. SwitchPMText string `json:"switch_pm_text"`
  979. SwitchPMParameter string `json:"switch_pm_parameter"`
  980. }
  981. func (config InlineConfig) method() string {
  982. return "answerInlineQuery"
  983. }
  984. func (config InlineConfig) params() (Params, error) {
  985. params := make(Params)
  986. params["inline_query_id"] = config.InlineQueryID
  987. params.AddNonZero("cache_time", config.CacheTime)
  988. params.AddBool("is_personal", config.IsPersonal)
  989. params.AddNonEmpty("next_offset", config.NextOffset)
  990. params.AddNonEmpty("switch_pm_text", config.SwitchPMText)
  991. params.AddNonEmpty("switch_pm_parameter", config.SwitchPMParameter)
  992. err := params.AddInterface("results", config.Results)
  993. return params, err
  994. }
  995. // AnswerWebAppQueryConfig is used to set the result of an interaction with a
  996. // Web App and send a corresponding message on behalf of the user to the chat
  997. // from which the query originated.
  998. type AnswerWebAppQueryConfig struct {
  999. // WebAppQueryID is the unique identifier for the query to be answered.
  1000. WebAppQueryID string `json:"web_app_query_id"`
  1001. // Result is an InlineQueryResult object describing the message to be sent.
  1002. Result interface{} `json:"result"`
  1003. }
  1004. func (config AnswerWebAppQueryConfig) method() string {
  1005. return "answerWebAppQuery"
  1006. }
  1007. func (config AnswerWebAppQueryConfig) params() (Params, error) {
  1008. params := make(Params)
  1009. params["web_app_query_id"] = config.WebAppQueryID
  1010. err := params.AddInterface("result", config.Result)
  1011. return params, err
  1012. }
  1013. // CallbackConfig contains information on making a CallbackQuery response.
  1014. type CallbackConfig struct {
  1015. CallbackQueryID string `json:"callback_query_id"`
  1016. Text string `json:"text"`
  1017. ShowAlert bool `json:"show_alert"`
  1018. URL string `json:"url"`
  1019. CacheTime int `json:"cache_time"`
  1020. }
  1021. func (config CallbackConfig) method() string {
  1022. return "answerCallbackQuery"
  1023. }
  1024. func (config CallbackConfig) params() (Params, error) {
  1025. params := make(Params)
  1026. params["callback_query_id"] = config.CallbackQueryID
  1027. params.AddNonEmpty("text", config.Text)
  1028. params.AddBool("show_alert", config.ShowAlert)
  1029. params.AddNonEmpty("url", config.URL)
  1030. params.AddNonZero("cache_time", config.CacheTime)
  1031. return params, nil
  1032. }
  1033. // ChatMemberConfig contains information about a user in a chat for use
  1034. // with administrative functions such as kicking or unbanning a user.
  1035. type ChatMemberConfig struct {
  1036. ChatID int64
  1037. SuperGroupUsername string
  1038. ChannelUsername string
  1039. UserID int64
  1040. }
  1041. // UnbanChatMemberConfig allows you to unban a user.
  1042. type UnbanChatMemberConfig struct {
  1043. ChatMemberConfig
  1044. OnlyIfBanned bool
  1045. }
  1046. func (config UnbanChatMemberConfig) method() string {
  1047. return "unbanChatMember"
  1048. }
  1049. func (config UnbanChatMemberConfig) params() (Params, error) {
  1050. params := make(Params)
  1051. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
  1052. params.AddNonZero64("user_id", config.UserID)
  1053. params.AddBool("only_if_banned", config.OnlyIfBanned)
  1054. return params, nil
  1055. }
  1056. // BanChatMemberConfig contains extra fields to kick user.
  1057. type BanChatMemberConfig struct {
  1058. ChatMemberConfig
  1059. UntilDate int64
  1060. RevokeMessages bool
  1061. }
  1062. func (config BanChatMemberConfig) method() string {
  1063. return "banChatMember"
  1064. }
  1065. func (config BanChatMemberConfig) params() (Params, error) {
  1066. params := make(Params)
  1067. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1068. params.AddNonZero64("user_id", config.UserID)
  1069. params.AddNonZero64("until_date", config.UntilDate)
  1070. params.AddBool("revoke_messages", config.RevokeMessages)
  1071. return params, nil
  1072. }
  1073. // KickChatMemberConfig contains extra fields to ban user.
  1074. //
  1075. // This was renamed to BanChatMember in later versions of the Telegram Bot API.
  1076. type KickChatMemberConfig = BanChatMemberConfig
  1077. // RestrictChatMemberConfig contains fields to restrict members of chat
  1078. type RestrictChatMemberConfig struct {
  1079. ChatMemberConfig
  1080. UntilDate int64
  1081. Permissions *ChatPermissions
  1082. }
  1083. func (config RestrictChatMemberConfig) method() string {
  1084. return "restrictChatMember"
  1085. }
  1086. func (config RestrictChatMemberConfig) params() (Params, error) {
  1087. params := make(Params)
  1088. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
  1089. params.AddNonZero64("user_id", config.UserID)
  1090. err := params.AddInterface("permissions", config.Permissions)
  1091. params.AddNonZero64("until_date", config.UntilDate)
  1092. return params, err
  1093. }
  1094. // PromoteChatMemberConfig contains fields to promote members of chat
  1095. type PromoteChatMemberConfig struct {
  1096. ChatMemberConfig
  1097. IsAnonymous bool
  1098. CanManageChat bool
  1099. CanChangeInfo bool
  1100. CanPostMessages bool
  1101. CanEditMessages bool
  1102. CanDeleteMessages bool
  1103. CanManageVideoChats bool
  1104. CanInviteUsers bool
  1105. CanRestrictMembers bool
  1106. CanPinMessages bool
  1107. CanPromoteMembers bool
  1108. }
  1109. func (config PromoteChatMemberConfig) method() string {
  1110. return "promoteChatMember"
  1111. }
  1112. func (config PromoteChatMemberConfig) params() (Params, error) {
  1113. params := make(Params)
  1114. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
  1115. params.AddNonZero64("user_id", config.UserID)
  1116. params.AddBool("is_anonymous", config.IsAnonymous)
  1117. params.AddBool("can_manage_chat", config.CanManageChat)
  1118. params.AddBool("can_change_info", config.CanChangeInfo)
  1119. params.AddBool("can_post_messages", config.CanPostMessages)
  1120. params.AddBool("can_edit_messages", config.CanEditMessages)
  1121. params.AddBool("can_delete_messages", config.CanDeleteMessages)
  1122. params.AddBool("can_manage_video_chats", config.CanManageVideoChats)
  1123. params.AddBool("can_invite_users", config.CanInviteUsers)
  1124. params.AddBool("can_restrict_members", config.CanRestrictMembers)
  1125. params.AddBool("can_pin_messages", config.CanPinMessages)
  1126. params.AddBool("can_promote_members", config.CanPromoteMembers)
  1127. return params, nil
  1128. }
  1129. // SetChatAdministratorCustomTitle sets the title of an administrative user
  1130. // promoted by the bot for a chat.
  1131. type SetChatAdministratorCustomTitle struct {
  1132. ChatMemberConfig
  1133. CustomTitle string
  1134. }
  1135. func (SetChatAdministratorCustomTitle) method() string {
  1136. return "setChatAdministratorCustomTitle"
  1137. }
  1138. func (config SetChatAdministratorCustomTitle) params() (Params, error) {
  1139. params := make(Params)
  1140. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
  1141. params.AddNonZero64("user_id", config.UserID)
  1142. params.AddNonEmpty("custom_title", config.CustomTitle)
  1143. return params, nil
  1144. }
  1145. // BanChatSenderChatConfig bans a channel chat in a supergroup or a channel. The
  1146. // owner of the chat will not be able to send messages and join live streams on
  1147. // behalf of the chat, unless it is unbanned first. The bot must be an
  1148. // administrator in the supergroup or channel for this to work and must have the
  1149. // appropriate administrator rights.
  1150. type BanChatSenderChatConfig struct {
  1151. ChatID int64
  1152. ChannelUsername string
  1153. SenderChatID int64
  1154. UntilDate int
  1155. }
  1156. func (config BanChatSenderChatConfig) method() string {
  1157. return "banChatSenderChat"
  1158. }
  1159. func (config BanChatSenderChatConfig) params() (Params, error) {
  1160. params := make(Params)
  1161. _ = params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1162. params.AddNonZero64("sender_chat_id", config.SenderChatID)
  1163. params.AddNonZero("until_date", config.UntilDate)
  1164. return params, nil
  1165. }
  1166. // UnbanChatSenderChatConfig unbans a previously banned channel chat in a
  1167. // supergroup or channel. The bot must be an administrator for this to work and
  1168. // must have the appropriate administrator rights.
  1169. type UnbanChatSenderChatConfig struct {
  1170. ChatID int64
  1171. ChannelUsername string
  1172. SenderChatID int64
  1173. }
  1174. func (config UnbanChatSenderChatConfig) method() string {
  1175. return "unbanChatSenderChat"
  1176. }
  1177. func (config UnbanChatSenderChatConfig) params() (Params, error) {
  1178. params := make(Params)
  1179. _ = params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1180. params.AddNonZero64("sender_chat_id", config.SenderChatID)
  1181. return params, nil
  1182. }
  1183. // ChatConfig contains information about getting information on a chat.
  1184. type ChatConfig struct {
  1185. ChatID int64
  1186. SuperGroupUsername string
  1187. }
  1188. func (config ChatConfig) params() (Params, error) {
  1189. params := make(Params)
  1190. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1191. return params, nil
  1192. }
  1193. // ChatInfoConfig contains information about getting chat information.
  1194. type ChatInfoConfig struct {
  1195. ChatConfig
  1196. }
  1197. func (ChatInfoConfig) method() string {
  1198. return "getChat"
  1199. }
  1200. // ChatMemberCountConfig contains information about getting the number of users in a chat.
  1201. type ChatMemberCountConfig struct {
  1202. ChatConfig
  1203. }
  1204. func (ChatMemberCountConfig) method() string {
  1205. return "getChatMembersCount"
  1206. }
  1207. // ChatAdministratorsConfig contains information about getting chat administrators.
  1208. type ChatAdministratorsConfig struct {
  1209. ChatConfig
  1210. }
  1211. func (ChatAdministratorsConfig) method() string {
  1212. return "getChatAdministrators"
  1213. }
  1214. // SetChatPermissionsConfig allows you to set default permissions for the
  1215. // members in a group. The bot must be an administrator and have rights to
  1216. // restrict members.
  1217. type SetChatPermissionsConfig struct {
  1218. ChatConfig
  1219. Permissions *ChatPermissions
  1220. }
  1221. func (SetChatPermissionsConfig) method() string {
  1222. return "setChatPermissions"
  1223. }
  1224. func (config SetChatPermissionsConfig) params() (Params, error) {
  1225. params := make(Params)
  1226. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1227. err := params.AddInterface("permissions", config.Permissions)
  1228. return params, err
  1229. }
  1230. // ChatInviteLinkConfig contains information about getting a chat link.
  1231. //
  1232. // Note that generating a new link will revoke any previous links.
  1233. type ChatInviteLinkConfig struct {
  1234. ChatConfig
  1235. }
  1236. func (ChatInviteLinkConfig) method() string {
  1237. return "exportChatInviteLink"
  1238. }
  1239. func (config ChatInviteLinkConfig) params() (Params, error) {
  1240. params := make(Params)
  1241. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1242. return params, nil
  1243. }
  1244. // CreateChatInviteLinkConfig allows you to create an additional invite link for
  1245. // a chat. The bot must be an administrator in the chat for this to work and
  1246. // must have the appropriate admin rights. The link can be revoked using the
  1247. // RevokeChatInviteLinkConfig.
  1248. type CreateChatInviteLinkConfig struct {
  1249. ChatConfig
  1250. Name string
  1251. ExpireDate int
  1252. MemberLimit int
  1253. CreatesJoinRequest bool
  1254. }
  1255. func (CreateChatInviteLinkConfig) method() string {
  1256. return "createChatInviteLink"
  1257. }
  1258. func (config CreateChatInviteLinkConfig) params() (Params, error) {
  1259. params := make(Params)
  1260. params.AddNonEmpty("name", config.Name)
  1261. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1262. params.AddNonZero("expire_date", config.ExpireDate)
  1263. params.AddNonZero("member_limit", config.MemberLimit)
  1264. params.AddBool("creates_join_request", config.CreatesJoinRequest)
  1265. return params, nil
  1266. }
  1267. // EditChatInviteLinkConfig allows you to edit a non-primary invite link created
  1268. // by the bot. The bot must be an administrator in the chat for this to work and
  1269. // must have the appropriate admin rights.
  1270. type EditChatInviteLinkConfig struct {
  1271. ChatConfig
  1272. InviteLink string
  1273. Name string
  1274. ExpireDate int
  1275. MemberLimit int
  1276. CreatesJoinRequest bool
  1277. }
  1278. func (EditChatInviteLinkConfig) method() string {
  1279. return "editChatInviteLink"
  1280. }
  1281. func (config EditChatInviteLinkConfig) params() (Params, error) {
  1282. params := make(Params)
  1283. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1284. params.AddNonEmpty("name", config.Name)
  1285. params["invite_link"] = config.InviteLink
  1286. params.AddNonZero("expire_date", config.ExpireDate)
  1287. params.AddNonZero("member_limit", config.MemberLimit)
  1288. params.AddBool("creates_join_request", config.CreatesJoinRequest)
  1289. return params, nil
  1290. }
  1291. // RevokeChatInviteLinkConfig allows you to revoke an invite link created by the
  1292. // bot. If the primary link is revoked, a new link is automatically generated.
  1293. // The bot must be an administrator in the chat for this to work and must have
  1294. // the appropriate admin rights.
  1295. type RevokeChatInviteLinkConfig struct {
  1296. ChatConfig
  1297. InviteLink string
  1298. }
  1299. func (RevokeChatInviteLinkConfig) method() string {
  1300. return "revokeChatInviteLink"
  1301. }
  1302. func (config RevokeChatInviteLinkConfig) params() (Params, error) {
  1303. params := make(Params)
  1304. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1305. params["invite_link"] = config.InviteLink
  1306. return params, nil
  1307. }
  1308. // ApproveChatJoinRequestConfig allows you to approve a chat join request.
  1309. type ApproveChatJoinRequestConfig struct {
  1310. ChatConfig
  1311. UserID int64
  1312. }
  1313. func (ApproveChatJoinRequestConfig) method() string {
  1314. return "approveChatJoinRequest"
  1315. }
  1316. func (config ApproveChatJoinRequestConfig) params() (Params, error) {
  1317. params := make(Params)
  1318. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1319. params.AddNonZero("user_id", int(config.UserID))
  1320. return params, nil
  1321. }
  1322. // DeclineChatJoinRequest allows you to decline a chat join request.
  1323. type DeclineChatJoinRequest struct {
  1324. ChatConfig
  1325. UserID int64
  1326. }
  1327. func (DeclineChatJoinRequest) method() string {
  1328. return "declineChatJoinRequest"
  1329. }
  1330. func (config DeclineChatJoinRequest) params() (Params, error) {
  1331. params := make(Params)
  1332. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1333. params.AddNonZero("user_id", int(config.UserID))
  1334. return params, nil
  1335. }
  1336. // LeaveChatConfig allows you to leave a chat.
  1337. type LeaveChatConfig struct {
  1338. ChatID int64
  1339. ChannelUsername string
  1340. }
  1341. func (config LeaveChatConfig) method() string {
  1342. return "leaveChat"
  1343. }
  1344. func (config LeaveChatConfig) params() (Params, error) {
  1345. params := make(Params)
  1346. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1347. return params, nil
  1348. }
  1349. // ChatConfigWithUser contains information about a chat and a user.
  1350. type ChatConfigWithUser struct {
  1351. ChatID int64
  1352. SuperGroupUsername string
  1353. UserID int64
  1354. }
  1355. func (config ChatConfigWithUser) params() (Params, error) {
  1356. params := make(Params)
  1357. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1358. params.AddNonZero64("user_id", config.UserID)
  1359. return params, nil
  1360. }
  1361. // GetChatMemberConfig is information about getting a specific member in a chat.
  1362. type GetChatMemberConfig struct {
  1363. ChatConfigWithUser
  1364. }
  1365. func (GetChatMemberConfig) method() string {
  1366. return "getChatMember"
  1367. }
  1368. // InvoiceConfig contains information for sendInvoice request.
  1369. type InvoiceConfig struct {
  1370. BaseChat
  1371. Title string // required
  1372. Description string // required
  1373. Payload string // required
  1374. ProviderToken string // required
  1375. Currency string // required
  1376. Prices []LabeledPrice // required
  1377. MaxTipAmount int
  1378. SuggestedTipAmounts []int
  1379. StartParameter string
  1380. ProviderData string
  1381. PhotoURL string
  1382. PhotoSize int
  1383. PhotoWidth int
  1384. PhotoHeight int
  1385. NeedName bool
  1386. NeedPhoneNumber bool
  1387. NeedEmail bool
  1388. NeedShippingAddress bool
  1389. SendPhoneNumberToProvider bool
  1390. SendEmailToProvider bool
  1391. IsFlexible bool
  1392. }
  1393. func (config InvoiceConfig) params() (Params, error) {
  1394. params, err := config.BaseChat.params()
  1395. if err != nil {
  1396. return params, err
  1397. }
  1398. params["title"] = config.Title
  1399. params["description"] = config.Description
  1400. params["payload"] = config.Payload
  1401. params["provider_token"] = config.ProviderToken
  1402. params["currency"] = config.Currency
  1403. if err = params.AddInterface("prices", config.Prices); err != nil {
  1404. return params, err
  1405. }
  1406. params.AddNonZero("max_tip_amount", config.MaxTipAmount)
  1407. err = params.AddInterface("suggested_tip_amounts", config.SuggestedTipAmounts)
  1408. params.AddNonEmpty("start_parameter", config.StartParameter)
  1409. params.AddNonEmpty("provider_data", config.ProviderData)
  1410. params.AddNonEmpty("photo_url", config.PhotoURL)
  1411. params.AddNonZero("photo_size", config.PhotoSize)
  1412. params.AddNonZero("photo_width", config.PhotoWidth)
  1413. params.AddNonZero("photo_height", config.PhotoHeight)
  1414. params.AddBool("need_name", config.NeedName)
  1415. params.AddBool("need_phone_number", config.NeedPhoneNumber)
  1416. params.AddBool("need_email", config.NeedEmail)
  1417. params.AddBool("need_shipping_address", config.NeedShippingAddress)
  1418. params.AddBool("is_flexible", config.IsFlexible)
  1419. params.AddBool("send_phone_number_to_provider", config.SendPhoneNumberToProvider)
  1420. params.AddBool("send_email_to_provider", config.SendEmailToProvider)
  1421. return params, err
  1422. }
  1423. func (config InvoiceConfig) method() string {
  1424. return "sendInvoice"
  1425. }
  1426. // ShippingConfig contains information for answerShippingQuery request.
  1427. type ShippingConfig struct {
  1428. ShippingQueryID string // required
  1429. OK bool // required
  1430. ShippingOptions []ShippingOption
  1431. ErrorMessage string
  1432. }
  1433. func (config ShippingConfig) method() string {
  1434. return "answerShippingQuery"
  1435. }
  1436. func (config ShippingConfig) params() (Params, error) {
  1437. params := make(Params)
  1438. params["shipping_query_id"] = config.ShippingQueryID
  1439. params.AddBool("ok", config.OK)
  1440. err := params.AddInterface("shipping_options", config.ShippingOptions)
  1441. params.AddNonEmpty("error_message", config.ErrorMessage)
  1442. return params, err
  1443. }
  1444. // PreCheckoutConfig contains information for answerPreCheckoutQuery request.
  1445. type PreCheckoutConfig struct {
  1446. PreCheckoutQueryID string // required
  1447. OK bool // required
  1448. ErrorMessage string
  1449. }
  1450. func (config PreCheckoutConfig) method() string {
  1451. return "answerPreCheckoutQuery"
  1452. }
  1453. func (config PreCheckoutConfig) params() (Params, error) {
  1454. params := make(Params)
  1455. params["pre_checkout_query_id"] = config.PreCheckoutQueryID
  1456. params.AddBool("ok", config.OK)
  1457. params.AddNonEmpty("error_message", config.ErrorMessage)
  1458. return params, nil
  1459. }
  1460. // DeleteMessageConfig contains information of a message in a chat to delete.
  1461. type DeleteMessageConfig struct {
  1462. ChannelUsername string
  1463. ChatID int64
  1464. MessageID int
  1465. }
  1466. func (config DeleteMessageConfig) method() string {
  1467. return "deleteMessage"
  1468. }
  1469. func (config DeleteMessageConfig) params() (Params, error) {
  1470. params := make(Params)
  1471. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1472. params.AddNonZero("message_id", config.MessageID)
  1473. return params, nil
  1474. }
  1475. // PinChatMessageConfig contains information of a message in a chat to pin.
  1476. type PinChatMessageConfig struct {
  1477. ChatID int64
  1478. ChannelUsername string
  1479. MessageID int
  1480. DisableNotification bool
  1481. }
  1482. func (config PinChatMessageConfig) method() string {
  1483. return "pinChatMessage"
  1484. }
  1485. func (config PinChatMessageConfig) params() (Params, error) {
  1486. params := make(Params)
  1487. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1488. params.AddNonZero("message_id", config.MessageID)
  1489. params.AddBool("disable_notification", config.DisableNotification)
  1490. return params, nil
  1491. }
  1492. // UnpinChatMessageConfig contains information of a chat message to unpin.
  1493. //
  1494. // If MessageID is not specified, it will unpin the most recent pin.
  1495. type UnpinChatMessageConfig struct {
  1496. ChatID int64
  1497. ChannelUsername string
  1498. MessageID int
  1499. }
  1500. func (config UnpinChatMessageConfig) method() string {
  1501. return "unpinChatMessage"
  1502. }
  1503. func (config UnpinChatMessageConfig) params() (Params, error) {
  1504. params := make(Params)
  1505. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1506. params.AddNonZero("message_id", config.MessageID)
  1507. return params, nil
  1508. }
  1509. // UnpinAllChatMessagesConfig contains information of all messages to unpin in
  1510. // a chat.
  1511. type UnpinAllChatMessagesConfig struct {
  1512. ChatID int64
  1513. ChannelUsername string
  1514. }
  1515. func (config UnpinAllChatMessagesConfig) method() string {
  1516. return "unpinAllChatMessages"
  1517. }
  1518. func (config UnpinAllChatMessagesConfig) params() (Params, error) {
  1519. params := make(Params)
  1520. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1521. return params, nil
  1522. }
  1523. // SetChatPhotoConfig allows you to set a group, supergroup, or channel's photo.
  1524. type SetChatPhotoConfig struct {
  1525. BaseFile
  1526. }
  1527. func (config SetChatPhotoConfig) method() string {
  1528. return "setChatPhoto"
  1529. }
  1530. func (config SetChatPhotoConfig) files() []RequestFile {
  1531. return []RequestFile{{
  1532. Name: "photo",
  1533. Data: config.File,
  1534. }}
  1535. }
  1536. // DeleteChatPhotoConfig allows you to delete a group, supergroup, or channel's photo.
  1537. type DeleteChatPhotoConfig struct {
  1538. ChatID int64
  1539. ChannelUsername string
  1540. }
  1541. func (config DeleteChatPhotoConfig) method() string {
  1542. return "deleteChatPhoto"
  1543. }
  1544. func (config DeleteChatPhotoConfig) params() (Params, error) {
  1545. params := make(Params)
  1546. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1547. return params, nil
  1548. }
  1549. // SetChatTitleConfig allows you to set the title of something other than a private chat.
  1550. type SetChatTitleConfig struct {
  1551. ChatID int64
  1552. ChannelUsername string
  1553. Title string
  1554. }
  1555. func (config SetChatTitleConfig) method() string {
  1556. return "setChatTitle"
  1557. }
  1558. func (config SetChatTitleConfig) params() (Params, error) {
  1559. params := make(Params)
  1560. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1561. params["title"] = config.Title
  1562. return params, nil
  1563. }
  1564. // SetChatDescriptionConfig allows you to set the description of a supergroup or channel.
  1565. type SetChatDescriptionConfig struct {
  1566. ChatID int64
  1567. ChannelUsername string
  1568. Description string
  1569. }
  1570. func (config SetChatDescriptionConfig) method() string {
  1571. return "setChatDescription"
  1572. }
  1573. func (config SetChatDescriptionConfig) params() (Params, error) {
  1574. params := make(Params)
  1575. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1576. params["description"] = config.Description
  1577. return params, nil
  1578. }
  1579. // GetStickerSetConfig allows you to get the stickers in a set.
  1580. type GetStickerSetConfig struct {
  1581. Name string
  1582. }
  1583. func (config GetStickerSetConfig) method() string {
  1584. return "getStickerSet"
  1585. }
  1586. func (config GetStickerSetConfig) params() (Params, error) {
  1587. params := make(Params)
  1588. params["name"] = config.Name
  1589. return params, nil
  1590. }
  1591. // UploadStickerConfig allows you to upload a sticker for use in a set later.
  1592. type UploadStickerConfig struct {
  1593. UserID int64
  1594. PNGSticker RequestFileData
  1595. }
  1596. func (config UploadStickerConfig) method() string {
  1597. return "uploadStickerFile"
  1598. }
  1599. func (config UploadStickerConfig) params() (Params, error) {
  1600. params := make(Params)
  1601. params.AddNonZero64("user_id", config.UserID)
  1602. return params, nil
  1603. }
  1604. func (config UploadStickerConfig) files() []RequestFile {
  1605. return []RequestFile{{
  1606. Name: "png_sticker",
  1607. Data: config.PNGSticker,
  1608. }}
  1609. }
  1610. // NewStickerSetConfig allows creating a new sticker set.
  1611. //
  1612. // You must set either PNGSticker or TGSSticker.
  1613. type NewStickerSetConfig struct {
  1614. UserID int64
  1615. Name string
  1616. Title string
  1617. PNGSticker RequestFileData
  1618. TGSSticker RequestFileData
  1619. Emojis string
  1620. ContainsMasks bool
  1621. MaskPosition *MaskPosition
  1622. }
  1623. func (config NewStickerSetConfig) method() string {
  1624. return "createNewStickerSet"
  1625. }
  1626. func (config NewStickerSetConfig) params() (Params, error) {
  1627. params := make(Params)
  1628. params.AddNonZero64("user_id", config.UserID)
  1629. params["name"] = config.Name
  1630. params["title"] = config.Title
  1631. params["emojis"] = config.Emojis
  1632. params.AddBool("contains_masks", config.ContainsMasks)
  1633. err := params.AddInterface("mask_position", config.MaskPosition)
  1634. return params, err
  1635. }
  1636. func (config NewStickerSetConfig) files() []RequestFile {
  1637. if config.PNGSticker != nil {
  1638. return []RequestFile{{
  1639. Name: "png_sticker",
  1640. Data: config.PNGSticker,
  1641. }}
  1642. }
  1643. return []RequestFile{{
  1644. Name: "tgs_sticker",
  1645. Data: config.TGSSticker,
  1646. }}
  1647. }
  1648. // AddStickerConfig allows you to add a sticker to a set.
  1649. type AddStickerConfig struct {
  1650. UserID int64
  1651. Name string
  1652. PNGSticker RequestFileData
  1653. TGSSticker RequestFileData
  1654. Emojis string
  1655. MaskPosition *MaskPosition
  1656. }
  1657. func (config AddStickerConfig) method() string {
  1658. return "addStickerToSet"
  1659. }
  1660. func (config AddStickerConfig) params() (Params, error) {
  1661. params := make(Params)
  1662. params.AddNonZero64("user_id", config.UserID)
  1663. params["name"] = config.Name
  1664. params["emojis"] = config.Emojis
  1665. err := params.AddInterface("mask_position", config.MaskPosition)
  1666. return params, err
  1667. }
  1668. func (config AddStickerConfig) files() []RequestFile {
  1669. if config.PNGSticker != nil {
  1670. return []RequestFile{{
  1671. Name: "png_sticker",
  1672. Data: config.PNGSticker,
  1673. }}
  1674. }
  1675. return []RequestFile{{
  1676. Name: "tgs_sticker",
  1677. Data: config.TGSSticker,
  1678. }}
  1679. }
  1680. // SetStickerPositionConfig allows you to change the position of a sticker in a set.
  1681. type SetStickerPositionConfig struct {
  1682. Sticker string
  1683. Position int
  1684. }
  1685. func (config SetStickerPositionConfig) method() string {
  1686. return "setStickerPositionInSet"
  1687. }
  1688. func (config SetStickerPositionConfig) params() (Params, error) {
  1689. params := make(Params)
  1690. params["sticker"] = config.Sticker
  1691. params.AddNonZero("position", config.Position)
  1692. return params, nil
  1693. }
  1694. // DeleteStickerConfig allows you to delete a sticker from a set.
  1695. type DeleteStickerConfig struct {
  1696. Sticker string
  1697. }
  1698. func (config DeleteStickerConfig) method() string {
  1699. return "deleteStickerFromSet"
  1700. }
  1701. func (config DeleteStickerConfig) params() (Params, error) {
  1702. params := make(Params)
  1703. params["sticker"] = config.Sticker
  1704. return params, nil
  1705. }
  1706. // SetStickerSetThumbConfig allows you to set the thumbnail for a sticker set.
  1707. type SetStickerSetThumbConfig struct {
  1708. Name string
  1709. UserID int64
  1710. Thumb RequestFileData
  1711. }
  1712. func (config SetStickerSetThumbConfig) method() string {
  1713. return "setStickerSetThumb"
  1714. }
  1715. func (config SetStickerSetThumbConfig) params() (Params, error) {
  1716. params := make(Params)
  1717. params["name"] = config.Name
  1718. params.AddNonZero64("user_id", config.UserID)
  1719. return params, nil
  1720. }
  1721. func (config SetStickerSetThumbConfig) files() []RequestFile {
  1722. return []RequestFile{{
  1723. Name: "thumb",
  1724. Data: config.Thumb,
  1725. }}
  1726. }
  1727. // SetChatStickerSetConfig allows you to set the sticker set for a supergroup.
  1728. type SetChatStickerSetConfig struct {
  1729. ChatID int64
  1730. SuperGroupUsername string
  1731. StickerSetName string
  1732. }
  1733. func (config SetChatStickerSetConfig) method() string {
  1734. return "setChatStickerSet"
  1735. }
  1736. func (config SetChatStickerSetConfig) params() (Params, error) {
  1737. params := make(Params)
  1738. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1739. params["sticker_set_name"] = config.StickerSetName
  1740. return params, nil
  1741. }
  1742. // DeleteChatStickerSetConfig allows you to remove a supergroup's sticker set.
  1743. type DeleteChatStickerSetConfig struct {
  1744. ChatID int64
  1745. SuperGroupUsername string
  1746. }
  1747. func (config DeleteChatStickerSetConfig) method() string {
  1748. return "deleteChatStickerSet"
  1749. }
  1750. func (config DeleteChatStickerSetConfig) params() (Params, error) {
  1751. params := make(Params)
  1752. params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
  1753. return params, nil
  1754. }
  1755. // MediaGroupConfig allows you to send a group of media.
  1756. //
  1757. // Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo).
  1758. type MediaGroupConfig struct {
  1759. ChatID int64
  1760. ChannelUsername string
  1761. Media []interface{}
  1762. DisableNotification bool
  1763. ReplyToMessageID int
  1764. }
  1765. func (config MediaGroupConfig) method() string {
  1766. return "sendMediaGroup"
  1767. }
  1768. func (config MediaGroupConfig) params() (Params, error) {
  1769. params := make(Params)
  1770. params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1771. params.AddBool("disable_notification", config.DisableNotification)
  1772. params.AddNonZero("reply_to_message_id", config.ReplyToMessageID)
  1773. err := params.AddInterface("media", prepareInputMediaForParams(config.Media))
  1774. return params, err
  1775. }
  1776. func (config MediaGroupConfig) files() []RequestFile {
  1777. return prepareInputMediaForFiles(config.Media)
  1778. }
  1779. // DiceConfig contains information about a sendDice request.
  1780. type DiceConfig struct {
  1781. BaseChat
  1782. // Emoji on which the dice throw animation is based.
  1783. // Currently, must be one of 🎲, 🎯, 🏀, ⚽, 🎳, or 🎰.
  1784. // Dice can have values 1-6 for 🎲, 🎯, and 🎳, values 1-5 for 🏀 and ⚽,
  1785. // and values 1-64 for 🎰.
  1786. // Defaults to “🎲”
  1787. Emoji string
  1788. }
  1789. func (config DiceConfig) method() string {
  1790. return "sendDice"
  1791. }
  1792. func (config DiceConfig) params() (Params, error) {
  1793. params, err := config.BaseChat.params()
  1794. if err != nil {
  1795. return params, err
  1796. }
  1797. params.AddNonEmpty("emoji", config.Emoji)
  1798. return params, err
  1799. }
  1800. // GetMyCommandsConfig gets a list of the currently registered commands.
  1801. type GetMyCommandsConfig struct {
  1802. Scope *BotCommandScope
  1803. LanguageCode string
  1804. }
  1805. func (config GetMyCommandsConfig) method() string {
  1806. return "getMyCommands"
  1807. }
  1808. func (config GetMyCommandsConfig) params() (Params, error) {
  1809. params := make(Params)
  1810. err := params.AddInterface("scope", config.Scope)
  1811. params.AddNonEmpty("language_code", config.LanguageCode)
  1812. return params, err
  1813. }
  1814. // SetMyCommandsConfig sets a list of commands the bot understands.
  1815. type SetMyCommandsConfig struct {
  1816. Commands []BotCommand
  1817. Scope *BotCommandScope
  1818. LanguageCode string
  1819. }
  1820. func (config SetMyCommandsConfig) method() string {
  1821. return "setMyCommands"
  1822. }
  1823. func (config SetMyCommandsConfig) params() (Params, error) {
  1824. params := make(Params)
  1825. if err := params.AddInterface("commands", config.Commands); err != nil {
  1826. return params, err
  1827. }
  1828. err := params.AddInterface("scope", config.Scope)
  1829. params.AddNonEmpty("language_code", config.LanguageCode)
  1830. return params, err
  1831. }
  1832. type DeleteMyCommandsConfig struct {
  1833. Scope *BotCommandScope
  1834. LanguageCode string
  1835. }
  1836. func (config DeleteMyCommandsConfig) method() string {
  1837. return "deleteMyCommands"
  1838. }
  1839. func (config DeleteMyCommandsConfig) params() (Params, error) {
  1840. params := make(Params)
  1841. err := params.AddInterface("scope", config.Scope)
  1842. params.AddNonEmpty("language_code", config.LanguageCode)
  1843. return params, err
  1844. }
  1845. // SetChatMenuButtonConfig changes the bot's menu button in a private chat,
  1846. // or the default menu button.
  1847. type SetChatMenuButtonConfig struct {
  1848. ChatID int64
  1849. ChannelUsername string
  1850. MenuButton *MenuButton
  1851. }
  1852. func (config SetChatMenuButtonConfig) method() string {
  1853. return "setChatMenuButton"
  1854. }
  1855. func (config SetChatMenuButtonConfig) params() (Params, error) {
  1856. params := make(Params)
  1857. if err := params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername); err != nil {
  1858. return params, err
  1859. }
  1860. err := params.AddInterface("menu_button", config.MenuButton)
  1861. return params, err
  1862. }
  1863. type GetChatMenuButtonConfig struct {
  1864. ChatID int64
  1865. ChannelUsername string
  1866. }
  1867. func (config GetChatMenuButtonConfig) method() string {
  1868. return "getChatMenuButton"
  1869. }
  1870. func (config GetChatMenuButtonConfig) params() (Params, error) {
  1871. params := make(Params)
  1872. err := params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
  1873. return params, err
  1874. }
  1875. type SetMyDefaultAdministratorRightsConfig struct {
  1876. Rights ChatAdministratorRights
  1877. ForChannels bool
  1878. }
  1879. func (config SetMyDefaultAdministratorRightsConfig) method() string {
  1880. return "setMyDefaultAdministratorRights"
  1881. }
  1882. func (config SetMyDefaultAdministratorRightsConfig) params() (Params, error) {
  1883. params := make(Params)
  1884. err := params.AddInterface("rights", config.Rights)
  1885. params.AddBool("for_channels", config.ForChannels)
  1886. return params, err
  1887. }
  1888. type GetMyDefaultAdministratorRightsConfig struct {
  1889. ForChannels bool
  1890. }
  1891. func (config GetMyDefaultAdministratorRightsConfig) method() string {
  1892. return "getMyDefaultAdministratorRights"
  1893. }
  1894. func (config GetMyDefaultAdministratorRightsConfig) params() (Params, error) {
  1895. params := make(Params)
  1896. params.AddBool("for_channels", config.ForChannels)
  1897. return params, nil
  1898. }
  1899. // prepareInputMediaParam evaluates a single InputMedia and determines if it
  1900. // needs to be modified for a successful upload. If it returns nil, then the
  1901. // value does not need to be included in the params. Otherwise, it will return
  1902. // the same type as was originally provided.
  1903. //
  1904. // The idx is used to calculate the file field name. If you only have a single
  1905. // file, 0 may be used. It is formatted into "attach://file-%d" for the primary
  1906. // media and "attach://file-%d-thumb" for thumbnails.
  1907. //
  1908. // It is expected to be used in conjunction with prepareInputMediaFile.
  1909. func prepareInputMediaParam(inputMedia interface{}, idx int) interface{} {
  1910. switch m := inputMedia.(type) {
  1911. case InputMediaPhoto:
  1912. if m.Media.NeedsUpload() {
  1913. m.Media = fileAttach(fmt.Sprintf("attach://file-%d", idx))
  1914. }
  1915. return m
  1916. case InputMediaVideo:
  1917. if m.Media.NeedsUpload() {
  1918. m.Media = fileAttach(fmt.Sprintf("attach://file-%d", idx))
  1919. }
  1920. if m.Thumb != nil && m.Thumb.NeedsUpload() {
  1921. m.Thumb = fileAttach(fmt.Sprintf("attach://file-%d-thumb", idx))
  1922. }
  1923. return m
  1924. case InputMediaAudio:
  1925. if m.Media.NeedsUpload() {
  1926. m.Media = fileAttach(fmt.Sprintf("attach://file-%d", idx))
  1927. }
  1928. if m.Thumb != nil && m.Thumb.NeedsUpload() {
  1929. m.Thumb = fileAttach(fmt.Sprintf("attach://file-%d-thumb", idx))
  1930. }
  1931. return m
  1932. case InputMediaDocument:
  1933. if m.Media.NeedsUpload() {
  1934. m.Media = fileAttach(fmt.Sprintf("attach://file-%d", idx))
  1935. }
  1936. if m.Thumb != nil && m.Thumb.NeedsUpload() {
  1937. m.Thumb = fileAttach(fmt.Sprintf("attach://file-%d-thumb", idx))
  1938. }
  1939. return m
  1940. }
  1941. return nil
  1942. }
  1943. // prepareInputMediaFile generates an array of RequestFile to provide for
  1944. // Fileable's files method. It returns an array as a single InputMedia may have
  1945. // multiple files, for the primary media and a thumbnail.
  1946. //
  1947. // The idx parameter is used to generate file field names. It uses the names
  1948. // "file-%d" for the main file and "file-%d-thumb" for the thumbnail.
  1949. //
  1950. // It is expected to be used in conjunction with prepareInputMediaParam.
  1951. func prepareInputMediaFile(inputMedia interface{}, idx int) []RequestFile {
  1952. files := []RequestFile{}
  1953. switch m := inputMedia.(type) {
  1954. case InputMediaPhoto:
  1955. if m.Media.NeedsUpload() {
  1956. files = append(files, RequestFile{
  1957. Name: fmt.Sprintf("file-%d", idx),
  1958. Data: m.Media,
  1959. })
  1960. }
  1961. case InputMediaVideo:
  1962. if m.Media.NeedsUpload() {
  1963. files = append(files, RequestFile{
  1964. Name: fmt.Sprintf("file-%d", idx),
  1965. Data: m.Media,
  1966. })
  1967. }
  1968. if m.Thumb != nil && m.Thumb.NeedsUpload() {
  1969. files = append(files, RequestFile{
  1970. Name: fmt.Sprintf("file-%d", idx),
  1971. Data: m.Thumb,
  1972. })
  1973. }
  1974. case InputMediaDocument:
  1975. if m.Media.NeedsUpload() {
  1976. files = append(files, RequestFile{
  1977. Name: fmt.Sprintf("file-%d", idx),
  1978. Data: m.Media,
  1979. })
  1980. }
  1981. if m.Thumb != nil && m.Thumb.NeedsUpload() {
  1982. files = append(files, RequestFile{
  1983. Name: fmt.Sprintf("file-%d", idx),
  1984. Data: m.Thumb,
  1985. })
  1986. }
  1987. case InputMediaAudio:
  1988. if m.Media.NeedsUpload() {
  1989. files = append(files, RequestFile{
  1990. Name: fmt.Sprintf("file-%d", idx),
  1991. Data: m.Media,
  1992. })
  1993. }
  1994. if m.Thumb != nil && m.Thumb.NeedsUpload() {
  1995. files = append(files, RequestFile{
  1996. Name: fmt.Sprintf("file-%d", idx),
  1997. Data: m.Thumb,
  1998. })
  1999. }
  2000. }
  2001. return files
  2002. }
  2003. // prepareInputMediaForParams calls prepareInputMediaParam for each item
  2004. // provided and returns a new array with the correct params for a request.
  2005. //
  2006. // It is expected that files will get data from the associated function,
  2007. // prepareInputMediaForFiles.
  2008. func prepareInputMediaForParams(inputMedia []interface{}) []interface{} {
  2009. newMedia := make([]interface{}, len(inputMedia))
  2010. copy(newMedia, inputMedia)
  2011. for idx, media := range inputMedia {
  2012. if param := prepareInputMediaParam(media, idx); param != nil {
  2013. newMedia[idx] = param
  2014. }
  2015. }
  2016. return newMedia
  2017. }
  2018. // prepareInputMediaForFiles calls prepareInputMediaFile for each item
  2019. // provided and returns a new array with the correct files for a request.
  2020. //
  2021. // It is expected that params will get data from the associated function,
  2022. // prepareInputMediaForParams.
  2023. func prepareInputMediaForFiles(inputMedia []interface{}) []RequestFile {
  2024. files := []RequestFile{}
  2025. for idx, media := range inputMedia {
  2026. if file := prepareInputMediaFile(media, idx); file != nil {
  2027. files = append(files, file...)
  2028. }
  2029. }
  2030. return files
  2031. }