bot_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. package tgbotapi
  2. import (
  3. "net/http"
  4. "os"
  5. "testing"
  6. "time"
  7. )
  8. const (
  9. TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
  10. ChatID = 76918703
  11. Channel = "@tgbotapitest"
  12. SupergroupChatID = -1001120141283
  13. ReplyToMessageID = 35
  14. ExistingPhotoFileID = "AgACAgIAAxkDAAEBFUZhIALQ9pZN4BUe8ZSzUU_2foSo1AACnrMxG0BucEhezsBWOgcikQEAAwIAA20AAyAE"
  15. ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
  16. ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
  17. ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"
  18. ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
  19. ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
  20. ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
  21. )
  22. type testLogger struct {
  23. t *testing.T
  24. }
  25. func (t testLogger) Println(v ...interface{}) {
  26. t.t.Log(v...)
  27. }
  28. func (t testLogger) Printf(format string, v ...interface{}) {
  29. t.t.Logf(format, v...)
  30. }
  31. func getBot(t *testing.T) (*BotAPI, error) {
  32. bot, err := NewBotAPI(TestToken)
  33. bot.Debug = true
  34. logger := testLogger{t}
  35. SetLogger(logger)
  36. if err != nil {
  37. t.Error(err)
  38. }
  39. return bot, err
  40. }
  41. func TestNewBotAPI_notoken(t *testing.T) {
  42. _, err := NewBotAPI("")
  43. if err == nil {
  44. t.Error(err)
  45. }
  46. }
  47. func TestGetUpdates(t *testing.T) {
  48. bot, _ := getBot(t)
  49. u := NewUpdate(0)
  50. _, err := bot.GetUpdates(u)
  51. if err != nil {
  52. t.Error(err)
  53. }
  54. }
  55. func TestSendWithMessage(t *testing.T) {
  56. bot, _ := getBot(t)
  57. msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
  58. msg.ParseMode = ModeMarkdown
  59. _, err := bot.Send(msg)
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. }
  64. func TestSendWithMessageReply(t *testing.T) {
  65. bot, _ := getBot(t)
  66. msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
  67. msg.ReplyToMessageID = ReplyToMessageID
  68. _, err := bot.Send(msg)
  69. if err != nil {
  70. t.Error(err)
  71. }
  72. }
  73. func TestSendWithMessageForward(t *testing.T) {
  74. bot, _ := getBot(t)
  75. msg := NewForward(ChatID, ChatID, ReplyToMessageID)
  76. _, err := bot.Send(msg)
  77. if err != nil {
  78. t.Error(err)
  79. }
  80. }
  81. func TestCopyMessage(t *testing.T) {
  82. bot, _ := getBot(t)
  83. msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
  84. message, err := bot.Send(msg)
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. copyMessageConfig := NewCopyMessage(SupergroupChatID, message.Chat.ID, message.MessageID)
  89. messageID, err := bot.CopyMessage(copyMessageConfig)
  90. if err != nil {
  91. t.Error(err)
  92. }
  93. if messageID.MessageID == message.MessageID {
  94. t.Error("copied message ID was the same as original message")
  95. }
  96. }
  97. func TestSendWithNewPhoto(t *testing.T) {
  98. bot, _ := getBot(t)
  99. msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
  100. msg.Caption = "Test"
  101. _, err := bot.Send(msg)
  102. if err != nil {
  103. t.Error(err)
  104. }
  105. }
  106. func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
  107. bot, _ := getBot(t)
  108. data, _ := os.ReadFile("tests/image.jpg")
  109. b := FileBytes{Name: "image.jpg", Bytes: data}
  110. msg := NewPhoto(ChatID, b)
  111. msg.Caption = "Test"
  112. _, err := bot.Send(msg)
  113. if err != nil {
  114. t.Error(err)
  115. }
  116. }
  117. func TestSendWithNewPhotoWithFileReader(t *testing.T) {
  118. bot, _ := getBot(t)
  119. f, _ := os.Open("tests/image.jpg")
  120. reader := FileReader{Name: "image.jpg", Reader: f}
  121. msg := NewPhoto(ChatID, reader)
  122. msg.Caption = "Test"
  123. _, err := bot.Send(msg)
  124. if err != nil {
  125. t.Error(err)
  126. }
  127. }
  128. func TestSendWithNewPhotoReply(t *testing.T) {
  129. bot, _ := getBot(t)
  130. msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
  131. msg.ReplyToMessageID = ReplyToMessageID
  132. _, err := bot.Send(msg)
  133. if err != nil {
  134. t.Error(err)
  135. }
  136. }
  137. func TestSendNewPhotoToChannel(t *testing.T) {
  138. bot, _ := getBot(t)
  139. msg := NewPhotoToChannel(Channel, FilePath("tests/image.jpg"))
  140. msg.Caption = "Test"
  141. _, err := bot.Send(msg)
  142. if err != nil {
  143. t.Error(err)
  144. t.Fail()
  145. }
  146. }
  147. func TestSendNewPhotoToChannelFileBytes(t *testing.T) {
  148. bot, _ := getBot(t)
  149. data, _ := os.ReadFile("tests/image.jpg")
  150. b := FileBytes{Name: "image.jpg", Bytes: data}
  151. msg := NewPhotoToChannel(Channel, b)
  152. msg.Caption = "Test"
  153. _, err := bot.Send(msg)
  154. if err != nil {
  155. t.Error(err)
  156. t.Fail()
  157. }
  158. }
  159. func TestSendNewPhotoToChannelFileReader(t *testing.T) {
  160. bot, _ := getBot(t)
  161. f, _ := os.Open("tests/image.jpg")
  162. reader := FileReader{Name: "image.jpg", Reader: f}
  163. msg := NewPhotoToChannel(Channel, reader)
  164. msg.Caption = "Test"
  165. _, err := bot.Send(msg)
  166. if err != nil {
  167. t.Error(err)
  168. t.Fail()
  169. }
  170. }
  171. func TestSendWithExistingPhoto(t *testing.T) {
  172. bot, _ := getBot(t)
  173. msg := NewPhoto(ChatID, FileID(ExistingPhotoFileID))
  174. msg.Caption = "Test"
  175. _, err := bot.Send(msg)
  176. if err != nil {
  177. t.Error(err)
  178. }
  179. }
  180. func TestSendWithNewDocument(t *testing.T) {
  181. bot, _ := getBot(t)
  182. msg := NewDocument(ChatID, FilePath("tests/image.jpg"))
  183. _, err := bot.Send(msg)
  184. if err != nil {
  185. t.Error(err)
  186. }
  187. }
  188. func TestSendWithNewDocumentAndThumb(t *testing.T) {
  189. bot, _ := getBot(t)
  190. msg := NewDocument(ChatID, FilePath("tests/voice.ogg"))
  191. msg.Thumb = FilePath("tests/image.jpg")
  192. _, err := bot.Send(msg)
  193. if err != nil {
  194. t.Error(err)
  195. }
  196. }
  197. func TestSendWithExistingDocument(t *testing.T) {
  198. bot, _ := getBot(t)
  199. msg := NewDocument(ChatID, FileID(ExistingDocumentFileID))
  200. _, err := bot.Send(msg)
  201. if err != nil {
  202. t.Error(err)
  203. }
  204. }
  205. func TestSendWithNewAudio(t *testing.T) {
  206. bot, _ := getBot(t)
  207. msg := NewAudio(ChatID, FilePath("tests/audio.mp3"))
  208. msg.Title = "TEST"
  209. msg.Duration = 10
  210. msg.Performer = "TEST"
  211. _, err := bot.Send(msg)
  212. if err != nil {
  213. t.Error(err)
  214. }
  215. }
  216. func TestSendWithExistingAudio(t *testing.T) {
  217. bot, _ := getBot(t)
  218. msg := NewAudio(ChatID, FileID(ExistingAudioFileID))
  219. msg.Title = "TEST"
  220. msg.Duration = 10
  221. msg.Performer = "TEST"
  222. _, err := bot.Send(msg)
  223. if err != nil {
  224. t.Error(err)
  225. }
  226. }
  227. func TestSendWithNewVoice(t *testing.T) {
  228. bot, _ := getBot(t)
  229. msg := NewVoice(ChatID, FilePath("tests/voice.ogg"))
  230. msg.Duration = 10
  231. _, err := bot.Send(msg)
  232. if err != nil {
  233. t.Error(err)
  234. }
  235. }
  236. func TestSendWithExistingVoice(t *testing.T) {
  237. bot, _ := getBot(t)
  238. msg := NewVoice(ChatID, FileID(ExistingVoiceFileID))
  239. msg.Duration = 10
  240. _, err := bot.Send(msg)
  241. if err != nil {
  242. t.Error(err)
  243. }
  244. }
  245. func TestSendWithContact(t *testing.T) {
  246. bot, _ := getBot(t)
  247. contact := NewContact(ChatID, "5551234567", "Test")
  248. if _, err := bot.Send(contact); err != nil {
  249. t.Error(err)
  250. }
  251. }
  252. func TestSendWithLocation(t *testing.T) {
  253. bot, _ := getBot(t)
  254. _, err := bot.Send(NewLocation(ChatID, 40, 40))
  255. if err != nil {
  256. t.Error(err)
  257. }
  258. }
  259. func TestSendWithVenue(t *testing.T) {
  260. bot, _ := getBot(t)
  261. venue := NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
  262. if _, err := bot.Send(venue); err != nil {
  263. t.Error(err)
  264. }
  265. }
  266. func TestSendWithNewVideo(t *testing.T) {
  267. bot, _ := getBot(t)
  268. msg := NewVideo(ChatID, FilePath("tests/video.mp4"))
  269. msg.Duration = 10
  270. msg.Caption = "TEST"
  271. _, err := bot.Send(msg)
  272. if err != nil {
  273. t.Error(err)
  274. }
  275. }
  276. func TestSendWithExistingVideo(t *testing.T) {
  277. bot, _ := getBot(t)
  278. msg := NewVideo(ChatID, FileID(ExistingVideoFileID))
  279. msg.Duration = 10
  280. msg.Caption = "TEST"
  281. _, err := bot.Send(msg)
  282. if err != nil {
  283. t.Error(err)
  284. }
  285. }
  286. func TestSendWithNewVideoNote(t *testing.T) {
  287. bot, _ := getBot(t)
  288. msg := NewVideoNote(ChatID, 240, FilePath("tests/videonote.mp4"))
  289. msg.Duration = 10
  290. _, err := bot.Send(msg)
  291. if err != nil {
  292. t.Error(err)
  293. }
  294. }
  295. func TestSendWithExistingVideoNote(t *testing.T) {
  296. bot, _ := getBot(t)
  297. msg := NewVideoNote(ChatID, 240, FileID(ExistingVideoNoteFileID))
  298. msg.Duration = 10
  299. _, err := bot.Send(msg)
  300. if err != nil {
  301. t.Error(err)
  302. }
  303. }
  304. func TestSendWithNewSticker(t *testing.T) {
  305. bot, _ := getBot(t)
  306. msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
  307. _, err := bot.Send(msg)
  308. if err != nil {
  309. t.Error(err)
  310. }
  311. }
  312. func TestSendWithExistingSticker(t *testing.T) {
  313. bot, _ := getBot(t)
  314. msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
  315. _, err := bot.Send(msg)
  316. if err != nil {
  317. t.Error(err)
  318. }
  319. }
  320. func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
  321. bot, _ := getBot(t)
  322. msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
  323. msg.ReplyMarkup = ReplyKeyboardRemove{
  324. RemoveKeyboard: true,
  325. Selective: false,
  326. }
  327. _, err := bot.Send(msg)
  328. if err != nil {
  329. t.Error(err)
  330. }
  331. }
  332. func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
  333. bot, _ := getBot(t)
  334. msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
  335. msg.ReplyMarkup = ReplyKeyboardRemove{
  336. RemoveKeyboard: true,
  337. Selective: false,
  338. }
  339. _, err := bot.Send(msg)
  340. if err != nil {
  341. t.Error(err)
  342. }
  343. }
  344. func TestSendWithDice(t *testing.T) {
  345. bot, _ := getBot(t)
  346. msg := NewDice(ChatID)
  347. _, err := bot.Send(msg)
  348. if err != nil {
  349. t.Error(err)
  350. t.Fail()
  351. }
  352. }
  353. func TestSendWithDiceWithEmoji(t *testing.T) {
  354. bot, _ := getBot(t)
  355. msg := NewDiceWithEmoji(ChatID, "🏀")
  356. _, err := bot.Send(msg)
  357. if err != nil {
  358. t.Error(err)
  359. t.Fail()
  360. }
  361. }
  362. func TestGetFile(t *testing.T) {
  363. bot, _ := getBot(t)
  364. file := FileConfig{
  365. FileID: ExistingPhotoFileID,
  366. }
  367. _, err := bot.GetFile(file)
  368. if err != nil {
  369. t.Error(err)
  370. }
  371. }
  372. func TestSendChatConfig(t *testing.T) {
  373. bot, _ := getBot(t)
  374. _, err := bot.Request(NewChatAction(ChatID, ChatTyping))
  375. if err != nil {
  376. t.Error(err)
  377. }
  378. }
  379. // TODO: identify why this isn't working
  380. // func TestSendEditMessage(t *testing.T) {
  381. // bot, _ := getBot(t)
  382. // msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
  383. // if err != nil {
  384. // t.Error(err)
  385. // }
  386. // edit := EditMessageTextConfig{
  387. // BaseEdit: BaseEdit{
  388. // ChatID: ChatID,
  389. // MessageID: msg.MessageID,
  390. // },
  391. // Text: "Updated text.",
  392. // }
  393. // _, err = bot.Send(edit)
  394. // if err != nil {
  395. // t.Error(err)
  396. // }
  397. // }
  398. func TestGetUserProfilePhotos(t *testing.T) {
  399. bot, _ := getBot(t)
  400. _, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
  401. if err != nil {
  402. t.Error(err)
  403. }
  404. }
  405. func TestSetWebhookWithCert(t *testing.T) {
  406. bot, _ := getBot(t)
  407. time.Sleep(time.Second * 2)
  408. bot.Request(DeleteWebhookConfig{})
  409. wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, FilePath("tests/cert.pem"))
  410. if err != nil {
  411. t.Error(err)
  412. }
  413. _, err = bot.Request(wh)
  414. if err != nil {
  415. t.Error(err)
  416. }
  417. _, err = bot.GetWebhookInfo()
  418. if err != nil {
  419. t.Error(err)
  420. }
  421. bot.Request(DeleteWebhookConfig{})
  422. }
  423. func TestSetWebhookWithoutCert(t *testing.T) {
  424. bot, _ := getBot(t)
  425. time.Sleep(time.Second * 2)
  426. bot.Request(DeleteWebhookConfig{})
  427. wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
  428. if err != nil {
  429. t.Error(err)
  430. }
  431. _, err = bot.Request(wh)
  432. if err != nil {
  433. t.Error(err)
  434. }
  435. info, err := bot.GetWebhookInfo()
  436. if err != nil {
  437. t.Error(err)
  438. }
  439. if info.MaxConnections == 0 {
  440. t.Errorf("Expected maximum connections to be greater than 0")
  441. }
  442. if info.LastErrorDate != 0 {
  443. t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
  444. }
  445. bot.Request(DeleteWebhookConfig{})
  446. }
  447. func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
  448. bot, _ := getBot(t)
  449. cfg := NewMediaGroup(ChatID, []interface{}{
  450. NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
  451. NewInputMediaPhoto(FilePath("tests/image.jpg")),
  452. NewInputMediaVideo(FilePath("tests/video.mp4")),
  453. })
  454. messages, err := bot.SendMediaGroup(cfg)
  455. if err != nil {
  456. t.Error(err)
  457. }
  458. if messages == nil {
  459. t.Error("No received messages")
  460. }
  461. if len(messages) != len(cfg.Media) {
  462. t.Errorf("Different number of messages: %d", len(messages))
  463. }
  464. }
  465. func TestSendWithMediaGroupDocument(t *testing.T) {
  466. bot, _ := getBot(t)
  467. cfg := NewMediaGroup(ChatID, []interface{}{
  468. NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
  469. NewInputMediaDocument(FilePath("tests/image.jpg")),
  470. })
  471. messages, err := bot.SendMediaGroup(cfg)
  472. if err != nil {
  473. t.Error(err)
  474. }
  475. if messages == nil {
  476. t.Error("No received messages")
  477. }
  478. if len(messages) != len(cfg.Media) {
  479. t.Errorf("Different number of messages: %d", len(messages))
  480. }
  481. }
  482. func TestSendWithMediaGroupAudio(t *testing.T) {
  483. bot, _ := getBot(t)
  484. cfg := NewMediaGroup(ChatID, []interface{}{
  485. NewInputMediaAudio(FilePath("tests/audio.mp3")),
  486. NewInputMediaAudio(FilePath("tests/audio.mp3")),
  487. })
  488. messages, err := bot.SendMediaGroup(cfg)
  489. if err != nil {
  490. t.Error(err)
  491. }
  492. if messages == nil {
  493. t.Error("No received messages")
  494. }
  495. if len(messages) != len(cfg.Media) {
  496. t.Errorf("Different number of messages: %d", len(messages))
  497. }
  498. }
  499. func ExampleNewBotAPI() {
  500. bot, err := NewBotAPI("MyAwesomeBotToken")
  501. if err != nil {
  502. panic(err)
  503. }
  504. bot.Debug = true
  505. log.Printf("Authorized on account %s", bot.Self.UserName)
  506. u := NewUpdate(0)
  507. u.Timeout = 60
  508. updates := bot.GetUpdatesChan(u)
  509. // Optional: wait for updates and clear them if you don't want to handle
  510. // a large backlog of old messages
  511. time.Sleep(time.Millisecond * 500)
  512. updates.Clear()
  513. for update := range updates {
  514. if update.Message == nil {
  515. continue
  516. }
  517. log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
  518. msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
  519. msg.ReplyToMessageID = update.Message.MessageID
  520. bot.Send(msg)
  521. }
  522. }
  523. func ExampleNewWebhook() {
  524. bot, err := NewBotAPI("MyAwesomeBotToken")
  525. if err != nil {
  526. panic(err)
  527. }
  528. bot.Debug = true
  529. log.Printf("Authorized on account %s", bot.Self.UserName)
  530. wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
  531. if err != nil {
  532. panic(err)
  533. }
  534. _, err = bot.Request(wh)
  535. if err != nil {
  536. panic(err)
  537. }
  538. info, err := bot.GetWebhookInfo()
  539. if err != nil {
  540. panic(err)
  541. }
  542. if info.LastErrorDate != 0 {
  543. log.Printf("failed to set webhook: %s", info.LastErrorMessage)
  544. }
  545. updates := bot.ListenForWebhook("/" + bot.Token)
  546. go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
  547. for update := range updates {
  548. log.Printf("%+v\n", update)
  549. }
  550. }
  551. func ExampleWebhookHandler() {
  552. bot, err := NewBotAPI("MyAwesomeBotToken")
  553. if err != nil {
  554. panic(err)
  555. }
  556. bot.Debug = true
  557. log.Printf("Authorized on account %s", bot.Self.UserName)
  558. wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
  559. if err != nil {
  560. panic(err)
  561. }
  562. _, err = bot.Request(wh)
  563. if err != nil {
  564. panic(err)
  565. }
  566. info, err := bot.GetWebhookInfo()
  567. if err != nil {
  568. panic(err)
  569. }
  570. if info.LastErrorDate != 0 {
  571. log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
  572. }
  573. http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
  574. update, err := bot.HandleUpdate(r)
  575. if err != nil {
  576. log.Printf("%+v\n", err.Error())
  577. } else {
  578. log.Printf("%+v\n", *update)
  579. }
  580. })
  581. go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
  582. }
  583. func ExampleInlineConfig() {
  584. bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
  585. if err != nil {
  586. panic(err)
  587. }
  588. log.Printf("Authorized on account %s", bot.Self.UserName)
  589. u := NewUpdate(0)
  590. u.Timeout = 60
  591. updates := bot.GetUpdatesChan(u)
  592. for update := range updates {
  593. if update.InlineQuery == nil { // if no inline query, ignore it
  594. continue
  595. }
  596. article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
  597. article.Description = update.InlineQuery.Query
  598. inlineConf := InlineConfig{
  599. InlineQueryID: update.InlineQuery.ID,
  600. IsPersonal: true,
  601. CacheTime: 0,
  602. Results: []interface{}{article},
  603. }
  604. if _, err := bot.Request(inlineConf); err != nil {
  605. log.Println(err)
  606. }
  607. }
  608. }
  609. func TestDeleteMessage(t *testing.T) {
  610. bot, _ := getBot(t)
  611. msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
  612. msg.ParseMode = ModeMarkdown
  613. message, _ := bot.Send(msg)
  614. deleteMessageConfig := DeleteMessageConfig{
  615. ChatID: message.Chat.ID,
  616. MessageID: message.MessageID,
  617. }
  618. _, err := bot.Request(deleteMessageConfig)
  619. if err != nil {
  620. t.Error(err)
  621. }
  622. }
  623. func TestPinChatMessage(t *testing.T) {
  624. bot, _ := getBot(t)
  625. msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
  626. msg.ParseMode = ModeMarkdown
  627. message, _ := bot.Send(msg)
  628. pinChatMessageConfig := PinChatMessageConfig{
  629. ChatID: message.Chat.ID,
  630. MessageID: message.MessageID,
  631. DisableNotification: false,
  632. }
  633. _, err := bot.Request(pinChatMessageConfig)
  634. if err != nil {
  635. t.Error(err)
  636. }
  637. }
  638. func TestUnpinChatMessage(t *testing.T) {
  639. bot, _ := getBot(t)
  640. msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
  641. msg.ParseMode = ModeMarkdown
  642. message, _ := bot.Send(msg)
  643. // We need pin message to unpin something
  644. pinChatMessageConfig := PinChatMessageConfig{
  645. ChatID: message.Chat.ID,
  646. MessageID: message.MessageID,
  647. DisableNotification: false,
  648. }
  649. if _, err := bot.Request(pinChatMessageConfig); err != nil {
  650. t.Error(err)
  651. }
  652. unpinChatMessageConfig := UnpinChatMessageConfig{
  653. ChatID: message.Chat.ID,
  654. MessageID: message.MessageID,
  655. }
  656. if _, err := bot.Request(unpinChatMessageConfig); err != nil {
  657. t.Error(err)
  658. }
  659. }
  660. func TestUnpinAllChatMessages(t *testing.T) {
  661. bot, _ := getBot(t)
  662. msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
  663. msg.ParseMode = ModeMarkdown
  664. message, _ := bot.Send(msg)
  665. pinChatMessageConfig := PinChatMessageConfig{
  666. ChatID: message.Chat.ID,
  667. MessageID: message.MessageID,
  668. DisableNotification: true,
  669. }
  670. if _, err := bot.Request(pinChatMessageConfig); err != nil {
  671. t.Error(err)
  672. }
  673. unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
  674. ChatID: message.Chat.ID,
  675. }
  676. if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
  677. t.Error(err)
  678. }
  679. }
  680. func TestPolls(t *testing.T) {
  681. bot, _ := getBot(t)
  682. poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
  683. msg, err := bot.Send(poll)
  684. if err != nil {
  685. t.Error(err)
  686. }
  687. result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
  688. if err != nil {
  689. t.Error(err)
  690. }
  691. if result.Question != "Are polls working?" {
  692. t.Error("Poll question did not match")
  693. }
  694. if !result.IsClosed {
  695. t.Error("Poll did not end")
  696. }
  697. if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
  698. t.Error("Poll options were incorrect")
  699. }
  700. }
  701. func TestSendDice(t *testing.T) {
  702. bot, _ := getBot(t)
  703. dice := NewDice(ChatID)
  704. msg, err := bot.Send(dice)
  705. if err != nil {
  706. t.Error("Unable to send dice roll")
  707. }
  708. if msg.Dice == nil {
  709. t.Error("Dice roll was not received")
  710. }
  711. }
  712. func TestCommands(t *testing.T) {
  713. bot, _ := getBot(t)
  714. setCommands := NewSetMyCommands(BotCommand{
  715. Command: "test",
  716. Description: "a test command",
  717. })
  718. if _, err := bot.Request(setCommands); err != nil {
  719. t.Error("Unable to set commands")
  720. }
  721. commands, err := bot.GetMyCommands()
  722. if err != nil {
  723. t.Error("Unable to get commands")
  724. }
  725. if len(commands) != 1 {
  726. t.Error("Incorrect number of commands returned")
  727. }
  728. if commands[0].Command != "test" || commands[0].Description != "a test command" {
  729. t.Error("Commands were incorrectly set")
  730. }
  731. setCommands = NewSetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats(), BotCommand{
  732. Command: "private",
  733. Description: "a private command",
  734. })
  735. if _, err := bot.Request(setCommands); err != nil {
  736. t.Error("Unable to set commands")
  737. }
  738. commands, err = bot.GetMyCommandsWithConfig(NewGetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats()))
  739. if err != nil {
  740. t.Error("Unable to get commands")
  741. }
  742. if len(commands) != 1 {
  743. t.Error("Incorrect number of commands returned")
  744. }
  745. if commands[0].Command != "private" || commands[0].Description != "a private command" {
  746. t.Error("Commands were incorrectly set")
  747. }
  748. }
  749. // TODO: figure out why test is failing
  750. //
  751. // func TestEditMessageMedia(t *testing.T) {
  752. // bot, _ := getBot(t)
  753. // msg := NewPhoto(ChatID, "tests/image.jpg")
  754. // msg.Caption = "Test"
  755. // m, err := bot.Send(msg)
  756. // if err != nil {
  757. // t.Error(err)
  758. // }
  759. // edit := EditMessageMediaConfig{
  760. // BaseEdit: BaseEdit{
  761. // ChatID: ChatID,
  762. // MessageID: m.MessageID,
  763. // },
  764. // Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
  765. // }
  766. // _, err = bot.Request(edit)
  767. // if err != nil {
  768. // t.Error(err)
  769. // }
  770. // }
  771. func TestPrepareInputMediaForParams(t *testing.T) {
  772. media := []interface{}{
  773. NewInputMediaPhoto(FilePath("tests/image.jpg")),
  774. NewInputMediaVideo(FileID("test")),
  775. }
  776. prepared := prepareInputMediaForParams(media)
  777. if media[0].(InputMediaPhoto).Media != FilePath("tests/image.jpg") {
  778. t.Error("Original media was changed")
  779. }
  780. if prepared[0].(InputMediaPhoto).Media != fileAttach("attach://file-0") {
  781. t.Error("New media was not replaced")
  782. }
  783. if prepared[1].(InputMediaVideo).Media != FileID("test") {
  784. t.Error("Passthrough value was not the same")
  785. }
  786. }