Go library for Telegram Bot API

Overview

tbot - Telegram Bot Server GoDoc Go Report Card GitHub Actions

logo

Features

  • Full Telegram Bot API 4.7 support
  • Zero dependency
  • Type-safe API client with functional options
  • Capture messages by regexp
  • Middlewares support
  • Can be used with go modules
  • Support for external logger
  • MIT licensed

Installation

With go modules:

go get github.com/yanzay/tbot/v2

Without go modules:

go get github.com/yanzay/tbot

Support

Join telegram group to get support or just to say thank you.

Documentation

Documentation: https://yanzay.github.io/tbot-doc/.

Full specification: godoc.

Usage

Simple usage example:

package main

import (
	"log"
	"os"
	"time"

	"github.com/yanzay/tbot/v2"
)

func main() {
	bot := tbot.New(os.Getenv("TELEGRAM_TOKEN"))
	c := bot.Client()
	bot.HandleMessage(".*yo.*", func(m *tbot.Message) {
		c.SendChatAction(m.Chat.ID, tbot.ActionTyping)
		time.Sleep(1 * time.Second)
		c.SendMessage(m.Chat.ID, "hello!")
	})
	err := bot.Start()
	if err != nil {
		log.Fatal(err)
	}
}

Examples

Please take a look inside examples folder.

Comments
  • Newbie question: How to handle a series of questions

    Newbie question: How to handle a series of questions

    this is not really an issue. I am the issue myself. I am learning how to develop in Go and got your example code running. It's so convenient! I was planning to make a scavenger hunt game in telegram. This would mean that the player would get a riddle, and give the correct answer. If the answer is correct he will get another question. (similar to a pizza ordering app: question1: "what would you like to order", question 2:"would you like toppings" ,etc)

    I am a bit confused how I would do that. Would I need to use a series of: bot.HandleMessage(pattern string, handler func(*tbot.Message)). So for every question a handler?

    or just one HandleMessage, and have just one handler function that everytime parses the message and keeps track how far this user is?

    I apologize if this not the proper channel for asking this.

    wontfix 
    opened by orcak 5
  • Replyf function as in obsolete tbot

    Replyf function as in obsolete tbot

    I noticed it is quite hard to make replies when user triggered any handleMessage requests.

    Is there a Replyf function as in obsolete golang tbot library for making stateful go tbots?

    thank you!

    https://tutorials.botsfloor.com/develop-your-own-telegram-bot-with-golang-and-tbot-de726883b83c

    wontfix 
    opened by lhlhx 5
  • Can't make examples work

    Can't make examples work

    All the examples give the same error

    # command-line-arguments
    commands/main.go:11:13: undefined: tbot.UpdateHandler
    commands/main.go:12:17: undefined: tbot.Update
    commands/main.go:20:9: undefined: tbot.New
    commands/main.go:24:18: m.Chat undefined (type *tbot.Message has no field or method Chat)
    
    

    and here is the code

    package main
    
    import (
    	"log"
    	"os"
    	"time"
    
    	"github.com/yanzay/tbot"
    )
    
    func stat(h tbot.UpdateHandler) tbot.UpdateHandler {
    	return func(u *tbot.Update) {
    		start := time.Now()
    		h(u)
    		log.Printf("Handle time: %v", time.Now().Sub(start))
    	}
    }
    
    func main() {
    	bot := tbot.New(os.Getenv("TELEGRAM_TOKEN"))
    	c := bot.Client()
    	bot.Use(stat) // add stat middleware to bot
    	bot.HandleMessage("", func(m *tbot.Message) {
    		c.SendMessage(m.Chat.ID, "hello!")
    	})
    	err := bot.Start()
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    

    The same is with the example from the Readme.md

    opened by gudata 4
  • How to use InlineKeyboard

    How to use InlineKeyboard

    Hi, I'm trying to have send a message with buttons bellow it. there wasn't any example about this. I don't know how to implement this with callback buttons. can you help?

    question wontfix v1.0 
    opened by r6m 4
  • Custom http client

    Custom http client

    I've found this article about default golang http client https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779. Author recommends using http.Client with tuned timeouts in production environment. This PR allows to pass such custom client to telebot API

    opened by variar 4
  • Library not working in groups

    Library not working in groups

    Hello, I made this bot t.me/covidata19bot that monitors the pandemic data in Italy using your library, but when it comes to work in groups it doesn't work.

    So, I wanted to be sure that none of my code was involved in the problem, then I used your cowsay example as test. Here is what I tried:

    • if you create a private group and you put the bot in, it works.
    • if you convert this group into a public one the bot doesn't work anymore
    • if you convert this group again into a private one the bot still doesn't work.

    Further, if you add the bot in a group that was previously a public one, it doesn't work from the start (so as in the steps just listed, it works only when the group has never been public yet). The privacy settings in BotFather are set to enable the bot to access messages in groups.

    Now, this happens if the bot runs on a Raspberry Pi 3, but if you are on a computer the situation completely changes. So, suppose I am running the cowsay bot on Pi 3, I send messages to the bot in my group and it doesn't answer. I turn it off. Now I run the same bot from my computer (obviously compiled for my architecture). All unanswered requests are now answered.

    So, what causes this very important issue on this library?

    wontfix 
    opened by DarkFighterLuke 2
  • upload file

    upload file

    Can you provide an example of how to handle upload file/picture from user? in the previous version there was a function HandleFile(), in this it is not

    wontfix 
    opened by tierline1 2
  • Cannot install and use the library

    Cannot install and use the library

    Hi everyone, I'm trying to install the library: go get github.com/yanzay/tbot/v2 but it gaves me the following error:

    package github.com/yanzay/tbot/v2: cannot find package "github.com/yanzay/tbot/v2" in any of:
            /usr/local/go/src/github.com/yanzay/tbot/v2 (from $GOROOT)
            /root/work/src/github.com/yanzay/tbot/v2 (from $GOPATH)
    

    I'm on a Debian 9 machine and other libraries works perfect. What can I do to solve this error?

    question wontfix 
    opened by Invy55 2
  • Support Casbin as the authorization module

    Support Casbin as the authorization module

    Hi, Casbin is an authorization library that supports models like ACL, RBAC, ABAC.

    Related to RBAC, Casbin has several advantages:

    1. roles can be cascaded, aka roles can have roles.
    2. support resource roles, so users have their roles and resource have their roles too. role = group here.
    3. the permission assignments (or policy in Casbin's language) can be persisted in files or database (MySQL and Cassandra).

    And you can even customize your own access control model, for example, mix RBAC and ABAC together by using roles and attributes at the same time. It's very flexible.

    Casbin can provide more flexibility and security than the current while-list auth. I can make PR if needed. Let me know if there's any question:) Thanks.

    opened by hsluoyz 2
  • Bot is not working inside docker container

    Bot is not working inside docker container

    Hello and first of all thank you for your great work! This package is really pleasant to use even with no prior expirience in building Telegram bots.

    Recently i was trying to package my bot inside docker container and faced some strange behaviour. What expected: compiled binary of the bot could be run inside docker container. What got: container builds and runs with no errors but the bot is not responding to updates.

    Steps to reproduce with code from examples/basic/main.go:

    1. build binary on the host as usual: go build -v -o test
    2. test if our bot works when running on host: export TELEGRAM_TOKEN=realTelegramTokenHere && ./test In my tests this works and the bot replies to messages.

    If the bot works correctly on the host, we can proceed to the next step.

    1. write simple Dockerfile in the same directory as binary:
    FROM ubuntu
    WORKDIR /bot
    COPY ./test ./test
    CMD ["/bot/test"]
    

    I'm using Ubuntu as my OS so the same binary could be run on the host and in container. If host OS is not GNU/Linux we need to use two stage Dockerfile to build inside temporary container:

    FROM golang AS buildstage
    WORKDIR /src
    COPY *.go go.mod ./
    RUN go mod tidy && go build -v -o /out/test .
    FROM ubuntu
    WORKDIR /bot
    COPY --from=buildstage /out/test ./test
    CMD ["/bot/test"]
    

    and build the container: docker build --no-cache -t testbot . 4. now we can test if the bot works from container: docker run --name=testbot --rm -it -e TELEGRAM_TOKEN=realTelegramTokenHere testbot

    When running inside the container bot does not replies to messages even if we use the binary that works on the host machine. Tried different hosts, changing docker network settings to --network=host but no luck. Since we are using ubuntu container it is possible to exec bash in it, install curl and try to make direct requests to Telegram api. In my tests getMe and getUpdates requests work perfectly so this should be not network related issue.

    opened by weirdvic 1
  • Could we determine that the Webhook request comes from Telegram?

    Could we determine that the Webhook request comes from Telegram?

    I'm new to Go and Networking, and not sure about that.

    	l, err := net.Listen("tcp", s.listenAddr)
    	if err != nil {
    		return nil, err
    	}
    	go http.Serve(l, http.HandlerFunc(handler))
    
    wontfix 
    opened by dimlau 1
Releases(v2.2.0)
A bot based on Telegram Bot API written in Golang allows users to download public Instagram photos, videos, and albums without receiving the user's credentials.

InstagramRobot InstagramRobot is a bot based on Telegram Bot API written in Golang that allows users to download public Instagram photos, videos, and

FTC Team 8 Dec 16, 2021
Telego is Telegram Bot API library for Golang with full API implementation (one-to-one)

Telego • Go Telegram Bot API Telego is Telegram Bot API library for Golang with full API implementation (one-to-one) The goal of this library was to c

Artem Yadelskyi 124 Nov 22, 2022
Bot - Telegram Music Bot in Go

Telegram Music Bot in Go An example bot using gotgcalls. Setup Install the serve

null 9 Jun 28, 2022
Go library for Telegram Bot API

tbot - Telegram Bot Server Features Full Telegram Bot API 4.7 support Zero dependency Type-safe API client with functional options Capture messages by

Alexey Grachov 331 Nov 11, 2022
Flexible message router add-on for go-telegram-bot-api library.

telemux Flexible message router add-on for go-telegram-bot-api library. Table of contents Motivation Features Minimal example Documentation Changelog

Andrew Dunai 24 Oct 24, 2022
Library for working with golang telegram client + bot based on tdlib. This library was taken from the user Arman92 and changed for the current version of tdlib.

go-tdlib Golang Telegram TdLib JSON bindings Install To install, you need to run inside a docker container (it is given below) go get -u github.com/ka

Rostislav Krivets 22 Oct 30, 2022
Bot-template - A simple bot template for creating a bot which includes a config, postgresql database

bot-template This is a simple bot template for creating a bot which includes a c

Disgo 2 Sep 9, 2022
Golang telegram bot API wrapper, session-based router and middleware

go-tgbot Pure Golang telegram bot API wrapper generated from swagger definition, session-based routing and middlewares. Usage benefits No need to lear

Oleg Lebedev 118 Nov 16, 2022
Client lib for Telegram bot api

Micha Client lib for Telegram bot api. Supports Bot API v2.3.1 (of 4th Dec 2016). Simple echo bot example: package main import ( "log" "git

Andrey 23 Nov 10, 2022
Golang bindings for the Telegram Bot API

Golang bindings for the Telegram Bot API All methods are fairly self explanatory, and reading the godoc page should explain everything. If something i

null 4.1k Nov 23, 2022
Bot that polls activity API for Github organisation and pushes updates to Telegram.

git-telegram-bot Telegram bot for notifying org events Requirements (for building) Go version 1.16.x Setup If you don't have a telegram bot token yet,

Skycoin 4 Apr 8, 2022
WIP Telegram Bot API server in Go

botapi The telegram-bot-api, but in go. WIP. Reference: https://core.telegram.org/bots/api Reference implementation: https://github.com/tdlib/telegram

gotd 17 Nov 25, 2022
Golang bindings for the Telegram Bot API

Golang bindings for the Telegram Bot API All methods are fairly self explanatory, and reading the godoc page should explain everything. If something i

null 0 Nov 18, 2021
UcodeQrTelebot ver2 - Easy way to get QR and U-code using Utopia API in telegram bot

UcodeQrTelebot Easy way to get QR and U-code using Utopia API in telegram bot Us

null 1 Dec 30, 2021
The serverless OTP telegram service use telegram as OTP service, and send OTP through webhook

Setup OTP First thing, you need prepare API(webhook) with POST method, the payload format as below { "first_name": "Nolan", "last_name": "Nguyen",

Dong Nguyen 3 Jul 24, 2022
Our library to interact with a telegram bot.

gotelegrambot Here you can find our library for telegram bot's. We develop the API endpoints according to our demand and need. You are welcome to help

J&J Ideenschmiede GmbH 0 Dec 18, 2021
A Telegram bot hook for Logrus logging library in Go

logrus2telegram logrus2telegram is a Telegram bot hook for Logrus logging librar

Dmytro Krasun 6 Nov 15, 2022
Telegram bot written in Golang using gotgbot library

go_tgbot Telegram bot written in Golang using gotgbot library. How to run go get -u github.com/itsLuuke/go_tgbot rename sample.env to .env and fill in

Luke 10 Nov 4, 2022
Quote-bot - Un bot utilisant l'API Twitter pour tweeter une citation par jour sur la programmation et les mathématiques.

Description Ceci est un simple bot programmé en Golang qui tweet une citation sur la programmation tout les jours. Ce bot est host sur le compte Twitt

Liam Cornu 0 Jan 1, 2022