Skip to content

udev-21/ya-golang-tbot-api

Repository files navigation

Telegram bot api client for golang

yet another Golang framework for Telegram Bot API client (Telegram Passport included)

I've seen, there's a lot good frameworks for this stuff, but unfortunately without passport :(

go get -u github.com/udev-21/ya-golang-tbot-api

Disclaimer

This project is my PET project and under MIT license :).

Overview

This is an API client library for Telegram Bot API.

Of course PR's and Forks are welcome :)

Features

  • Full Telegram Bot API 6.0 support
  • Zero dependency
  • Filters

Simple Echo Bot

All examples you can find under examples folder here's one of them:

package main

import (
	gtbotapi "github.com/udev-21/ya-golang-tbot-api"
	"github.com/udev-21/ya-golang-tbot-api/filter"
	"github.com/udev-21/ya-golang-tbot-api/method"
)

func handle(ctx gtbotapi.Context) error {
	text := "pong" // response text
	payload := method.NewSendMessage(text)

	payload.WithReplyToMessageID(ctx.Message().MessageID) //  reply to specific message

	receiver := ctx.Chat() // choose message reciever chat
	_, err := ctx.Send(receiver, payload)
	// apiResponse, err := ctx.Send(reciver, payload) // use this one if you need process api response

	return err
}

// replace value with your BOT TOKEN which gives you @botfather on telegram 
const TOKEN = "BOT:TOKEN" 

func main() {
	bot := gtbotapi.NewBotAPI(TOKEN)

	// if you send "ping" any chat this one will reply as "pong"
	bot.Handle(filter.OnText("ping"), handle)

	bot.Start()
}

Filters

Full list of filters you can find in here

Here's some examples for using them:

// filter for: any text message from anywhere
bot.Handle(filter.OnAnyText, yourHandlerFunc)

// filter for: only supergroup and any text message
bot.Handle(filter.OnAnyText, yourHandlerFunc,filter.OnlySupergroup)

// filter for: only (supergroup or group) and any text message
bot.Handle(filter.OnAnyText, yourHandlerFunc,filter.OnlySupergroupOrGroup) 

// filter for: only private and any text message
bot.Handle(filter.OnAnyText, yourHandlerFunc, filter.OnlyPrivate) 

// filter for: only private and text message == "ping"
bot.Handle(filter.OnText("ping"), yourHandlerFunc, filter.OnlyPrivate)