Go
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples Added userid and chatid based auth with example usage (#27) Mar 1, 2018
internal/adapter missed return in case of send MessageAudio May 1, 2018
model Added InlineKeyboard support and ReplyLocaiton Feb 18, 2018
.gitignore Added support for Contact and Location buttons. Feb 10, 2018
.travis.yml Add codecov.io. May 9, 2017
LICENSE Add license file via addalicense.com Nov 29, 2016
README.md Fix spell. May 18, 2017
auth.go Added userid and chatid based auth with example usage (#27) Mar 1, 2018
auth_test.go Added userid and chatid based auth with example usage (#27) Mar 1, 2018
default_mux.go Add Reset method for mux. May 24, 2017
default_mux_test.go Fixed packages name for successful merge. Feb 10, 2018
handler.go Add alias support for RouterMux. May 19, 2017
handler_test.go Fix message types and cleanup. May 9, 2017
help_handler.go Fix build. May 8, 2017
help_handler_test.go Fix build. May 8, 2017
logo.png Improve readme. May 15, 2017
main_test.go Added stub SendRaw in mockBot for tests Feb 18, 2018
message.go Added InlineKeyboard support and ReplyLocaiton Feb 18, 2018
message_processor.go Fixed packages name for successful merge. Feb 10, 2018
message_processor_test.go Extract BotAdapter interface. May 9, 2017
message_test.go Added userid and chatid based auth with example usage (#27) Mar 1, 2018
mux.go Add Reset method for mux. May 24, 2017
router_mux.go Fix aliases with spaces in RouterMux. Sep 29, 2017
router_mux_test.go Fixed packages name for successful merge. Feb 10, 2018
server.go Returned lost SendMessage method back Feb 18, 2018
server_test.go Parametrize webhook server address. May 12, 2017
session_storage.go Add Reset method for mux. May 24, 2017

README.md

tbot - Telegram Bot Server Build Status Go Report Card codecov

GoDoc

tbot is a Telegram bot server.

It feels just like net/http. You define routes and handlers and it works. Middleware support included.

Hipster logo

logo

Installation

go get -u github.com/yanzay/tbot

Support

For a brief introduction, take a look the blog post on medium.

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

Usage

It feels like net/http Server, so it's easy to use:

package main

import (
	"log"
	"os"
	"time"

	"github.com/yanzay/tbot"
)

func main() {
	token := os.Getenv("TELEGRAM_TOKEN")
	// Create new telegram bot server using token
	bot, err := tbot.NewServer(token)
	if err != nil {
		log.Fatal(err)
	}

	// Use whitelist for Auth middleware, allow to interact only with user1 and user2
	whitelist := []string{"yanzay", "user2"}
	bot.AddMiddleware(tbot.NewAuth(whitelist))

	// Yo handler works without slash, simple text response
	bot.Handle("yo", "YO!")

	// Handle with HiHandler function
	bot.HandleFunc("/hi", HiHandler)
	// Handler can accept varialbes
	bot.HandleFunc("/say {text}", SayHandler)
	// Bot can send stickers, photos, music
	bot.HandleFunc("/sticker", StickerHandler)
	bot.HandleFunc("/photo", PhotoHandler)
	bot.HandleFunc("/keyboard", KeyboardHandler)

	// Use file handler to handle user uploads
	bot.HandleFile(FileHandler)

	// Set default handler if you want to process unmatched input
	bot.HandleDefault(EchoHandler)

	// Start listening for messages
	err = bot.ListenAndServe()
	log.Fatal(err)
}

func HiHandler(message *tbot.Message) {
	// Handler can reply with several messages
	message.Replyf("Hello, %s!", message.From)
	time.Sleep(1 * time.Second)
	message.Reply("What's up?")
}

func SayHandler(message *tbot.Message) {
	// Message contain it's varialbes from curly brackets
	message.Reply(message.Vars["text"])
}

func EchoHandler(message *tbot.Message) {
	message.Reply(message.Text())
}

func StickerHandler(message *tbot.Message) {
	message.ReplySticker("sticker.png")
}

func PhotoHandler(message *tbot.Message) {
	message.ReplyPhoto("photo.jpg", "it's me")
}

func KeyboardHandler(message *tbot.Message) {
	buttons := [][]string{
		{"Some", "Test", "Buttons"},
		{"Another", "Row"},
	}
	message.ReplyKeyboard("Buttons example", buttons)
}

func FileHandler(message *tbot.Message) {
	err := message.Download("./uploads")
	if err != nil {
		message.Replyf("Error handling file: %q", err)
		return
	}
	message.Reply("Thanks for uploading!")
}

See full documentation on godoc.