Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
/ go-bot Public archive

Golang Telegram Bot library - In Development

License

Notifications You must be signed in to change notification settings

KaikyuLotus/go-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

package main

import . "github.com/kaikyudev/gobot"


func main() {
    // instantiate our bot with out custom costructor
    var bot = NewBot("TOKEN")

    // Add our funcion in response to /start
    bot.AddCommandHandler("start", start)

    // Start getting updates, pass true to clean pending updates, otherwise pass false
    bot.StartPolling(true)

    // Wait while our bot runs
    bot.Idle()
}

func start(bot *Bot, update Update) {
    msgID := update.Message.MessageID
    chatID := update.Message.Chat.ID

    // Typing...
    bot.SendChatAction(update.Message.Chat.ID, Typing)

    // With SendMessageArgs{} you can pass extra args for sendMessage method
    bot.SendMessage(chatID, "*Markdown*", SendMessageArgs{ReplyToMessageID:msgID, ParseMode:Markdown})
} // That's it!

Getting Updates

package main

import (
    . "github.com/kaikyudev/gobot"
    "log"
)


func main() {
    var bot = NewBot("TOKEN")

    // Add our funcion in response to any update
    bot.SetUpdateHandler(updateHandler)

    bot.StartPolling(true)
    bot.Idle()
}

func updateHandler(bot *Bot, update Update) {
    // Log every update
    log.Printf("[%s] %s", update.Message.From.Username, update.Message.Text)
}

Handling Panics

package main

import (
    . "github.com/kaikyudev/gobot"
    "log"
)


func main() {
    var bot = NewBot("TOKEN")

    // Add our funcion in response to every panic
    bot.SetErrorHandler(errorHandler)

    bot.AddCommandHandler("panic", panicFoo)

    bot.StartPolling(true)
    bot.Idle()
}

func errorHandler(bot *Bot, update Update, errorMessage string) {
    // Log every panic
    log.Printf("Update #%d has caused a Panic with error message %s", update.UpdateID, error)
}

func panicFoo(bot *Bot, update Update){
    log.Print("Starting panic...")
    panic(0)
}

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...

About

Golang Telegram Bot library - In Development

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages