项目作者: KaikyuLotus

项目描述 :
Golang Telegram Bot library - In Development
高级语言: Go
项目地址: git://github.com/KaikyuLotus/go-bot.git
创建时间: 2018-05-16T09:02:52Z
项目社区:https://github.com/KaikyuLotus/go-bot

开源协议:MIT License

下载


GoBot

This is just a small training project and it’s not intended for everyday use, not yet.

Please feel free to give me some tips.

This is my first Go project.

Examples

These examples are outdated, please be patient.

Hello World Command

  1. package main
  2. import . "github.com/kaikyudev/gobot"
  3. func main() {
  4. // instantiate our bot with out custom costructor
  5. var bot = NewBot("TOKEN")
  6. // Add our funcion in response to /start
  7. bot.AddCommandHandler("start", start)
  8. // Start getting updates, pass true to clean pending updates, otherwise pass false
  9. bot.StartPolling(true)
  10. // Wait while our bot runs
  11. bot.Idle()
  12. }
  13. func start(bot *Bot, update Update) {
  14. msgID := update.Message.MessageID
  15. chatID := update.Message.Chat.ID
  16. // Typing...
  17. bot.SendChatAction(update.Message.Chat.ID, Typing)
  18. // With SendMessageArgs{} you can pass extra args for sendMessage method
  19. bot.SendMessage(chatID, "*Markdown*", SendMessageArgs{ReplyToMessageID:msgID, ParseMode:Markdown})
  20. } // That's it!

Getting Updates

  1. package main
  2. import (
  3. . "github.com/kaikyudev/gobot"
  4. "log"
  5. )
  6. func main() {
  7. var bot = NewBot("TOKEN")
  8. // Add our funcion in response to any update
  9. bot.SetUpdateHandler(updateHandler)
  10. bot.StartPolling(true)
  11. bot.Idle()
  12. }
  13. func updateHandler(bot *Bot, update Update) {
  14. // Log every update
  15. log.Printf("[%s] %s", update.Message.From.Username, update.Message.Text)
  16. }

Handling Panics

  1. package main
  2. import (
  3. . "github.com/kaikyudev/gobot"
  4. "log"
  5. )
  6. func main() {
  7. var bot = NewBot("TOKEN")
  8. // Add our funcion in response to every panic
  9. bot.SetErrorHandler(errorHandler)
  10. bot.AddCommandHandler("panic", panicFoo)
  11. bot.StartPolling(true)
  12. bot.Idle()
  13. }
  14. func errorHandler(bot *Bot, update Update, errorMessage string) {
  15. // Log every panic
  16. log.Printf("Update #%d has caused a Panic with error message %s", update.UpdateID, error)
  17. }
  18. func panicFoo(bot *Bot, update Update){
  19. log.Print("Starting panic...")
  20. panic(0)
  21. }

What is working

  • Basic API requests
  • Methods: GetMe, GetUpdates, SendMessage, SendChatAction, SendPhoto, SendDocument [more to come…]
  • Getting updates
  • Commands handling
  • Errors handling

Not much more…