Skip to content

Telegram Bot API library in Go, but with more clean code

License

Notifications You must be signed in to change notification settings

nickname76/telegrambot

Repository files navigation

The most clean and strongly typed Telegram Bot API library in Go

Completely covers Bot API version - 6.2

Telegram Bot API documentaion: https://core.telegram.org/bots/api

Telegram deep links list: https://core.telegram.org/api/links

More Telegram deep links: https://t.me/DeepLink

DISCORD: https://discord.gg/golang (#telegrambot channel)

Documentation on this library:

Please, star this repository, if you found this library useful!

Example usage

package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"

	"github.com/nickname76/telegrambot"
)

func main() {
	api, me, err := telegrambot.NewAPI("YOUR_TELEGRAM_BOT_API_TOKEN")
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	stop := telegrambot.StartReceivingUpdates(api, func(update *telegrambot.Update, err error) {
		if err != nil {
			log.Printf("Error: %v", err)
			return
		}

		msg := update.Message
		if msg == nil {
			return
		}

		_, err = api.SendMessage(&telegrambot.SendMessageParams{
			ChatID: msg.Chat.ID,
			Text:   fmt.Sprintf("Hello %v, I am %v", msg.From.FirstName, me.FirstName),
			ReplyMarkup: &telegrambot.ReplyKeyboardMarkup{
				Keyboard: [][]*telegrambot.KeyboardButton{{
					{
						Text: "Hello",
					},
				}},
				ResizeKeyboard:  true,
				OneTimeKeyboard: true,
			},
		})

		if err != nil {
			log.Printf("Error: %v", err)
			return
		}
	})

	log.Printf("Started on %v", me.Username)

	exitCh := make(chan os.Signal, 1)
	signal.Notify(exitCh, os.Interrupt)

	<-exitCh

	// Waits for all updates handling to complete
	stop()
}

See also

If you want to develop Telegram bot, you should also see these libraries, which might be useful for you

  • Repeater - Go library for creating repeating function calls
  • QiwiP2P - Go library for Qiwi P2P API