All Projects → NicoNex → echotron

NicoNex / echotron

Licence: LGPL-3.0 License
An elegant and concurrent library for Telegram bots in Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to echotron

telegram
📚 Golang bindings for Telegram API
Stars: ✭ 15 (-84.21%)
Mutual labels:  package, telegram-bot, telegram-api, telegram-bots, telegram-bot-api, go-library
grouphelperbot
A Telegram Bot made to help group admins, with Italian/English support.
Stars: ✭ 26 (-72.63%)
Mutual labels:  telegram-bot, telegram-api, telegram-bots, telegram-bot-api
tdlight-telegram-bot-api
The TDLight Telegram Bot API is an actively enhanced fork of the original Bot API, featuring experimental user support, proxies, unlimited files size, and more.
Stars: ✭ 71 (-25.26%)
Mutual labels:  telegram-bot, telegram-api, telegram-bots, telegram-bot-api
Mypackbot
🤖 Your own unlimited pack of Telegram-stickers
Stars: ✭ 18 (-81.05%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
Informer
A Telegram Mass Surveillance Bot in Python
Stars: ✭ 745 (+684.21%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
Java Telegram Bot Api
Telegram Bot API for Java
Stars: ✭ 819 (+762.11%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
Lulzbot Telegram Bot
Moved to https://gitlab.com/bhavyanshu/lulzbot-telegram-bot
Stars: ✭ 18 (-81.05%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
Python Telegram
Python client for the Telegram's tdlib
Stars: ✭ 246 (+158.95%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
gotgbot
Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
Stars: ✭ 178 (+87.37%)
Mutual labels:  telegram-api, telegram-bots, telegram-bot-api
RPi-TELEBOT
Python based Telegram bot to monitor and control the raspberry pi
Stars: ✭ 19 (-80%)
Mutual labels:  telegram-bot, telegram-bots, telegram-bot-api
tdlight-java
Complete Bot and Userbot Telegram library based on TDLib
Stars: ✭ 128 (+34.74%)
Mutual labels:  telegram-api, telegram-bots, telegram-bot-api
telegram client
library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script
Stars: ✭ 38 (-60%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
Telegram
Telegram Bot API Wrapper for Scala
Stars: ✭ 310 (+226.32%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
tinjecttelegram delphi
LMCODE
Stars: ✭ 37 (-61.05%)
Mutual labels:  telegram-bot, telegram-bots, telegram-bot-api
TelegramBots-Python
TelegramBots written in Python
Stars: ✭ 15 (-84.21%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
LilSholex
A project containing web apps and Telegram API bots.
Stars: ✭ 32 (-66.32%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
telresender
A Telegram bot, which resend your message to another account
Stars: ✭ 22 (-76.84%)
Mutual labels:  telegram-api, telegram-bots, telegram-bot-api
Bybit-Auto-Trading-Bot-Ordes-placed-via-TradingView-Webhook
Python based Trading Bot that uses TradingView.com webhook JSON alerts to place orders(buy/sell/close/manage positions/TP/SL/TS etc.) on Bybit.com. Hire me directly here https://www.freelancer.com/u/Beannsofts for any assistance
Stars: ✭ 235 (+147.37%)
Mutual labels:  telegram-bot, telegram-api, telegram-bot-api
Telegram-PHP-App
App base for Telegram bots
Stars: ✭ 14 (-85.26%)
Mutual labels:  telegram-bot, telegram-api, telegram-bots
Truecaller-Smsbomber telegram bot
Telegram bot which has truecaller and smsbomber features
Stars: ✭ 30 (-68.42%)
Mutual labels:  telegram-bot, telegram-bot-api

echotron


logo

Language PkgGoDev Go Report Card License Build Status Coverage Status Mentioned in Awesome Go Telegram

Echotron is a concurrent library for telegram bots written in pure Go.

Fetch with

go get github.com/NicoNex/echotron/v3

Design

Echotron is heavily based on concurrency: for example, every call to the Update method of each bot is executed on a different goroutine. This makes sure that, even if one instance of the bot is deadlocked, the other ones keep running just fine, making the bot work for other users without any issues and/or slowdowns.

Echotron is designed to be as similar to the official Telegram API as possible, but there are some things to take into account before starting to work with this library.

  • The methods have the exact same name, but with a capital first letter, since in Go methods have to start with a capital letter to be exported. Example: sendMessage becomes SendMessage
  • The order of the parameters in some methods is different than in the official Telegram API, so refer to the docs for the correct one.
  • The only chat_id (or, in this case, chatID) type supported is int64, instead of the "Integer or String" requirement of the official API. That's because numeric IDs can't change in any way, which isn't the case with text-based usernames.
  • In some methods, you might find a InputFile type parameter. InputFile is a struct with unexported fields, since only three combination of fields are valid, which can be obtained through the methods NewInputFileID, NewInputFilePath and NewInputFileBytes.
  • In some methods, you might find a MessageIDOptions type parameter. MessageIDOptions is another struct with unexported fields, since only two combination of field are valid, which can be obtained through the methods NewMessageID and NewInlineMessageID.
  • Optional parameters can be added by passing the correct struct to each method that might request optional parameters. If you don't want to pass any optional parameter, nil is more than enough. Refer to the docs to check for each method's optional parameters struct: it's the type of the opts parameter.
  • Some parameters are hardcoded to avoid putting random stuff which isn't recognized by the Telegram API. Some notable examples are ParseMode, ChatAction and InlineQueryType. For a full list of custom hardcoded parameters, refer to the docs for each custom type: by clicking on the type's name, you'll get the source which contains the possible values for that type.

Usage

Long Polling

A very simple implementation:

package main

import (
	"log"

	"github.com/NicoNex/echotron/v3"
)

// Struct useful for managing internal states in your bot, but it could be of
// any type such as `type bot int64` if you only need to store the chatID.
type bot struct {
	chatID int64
	echotron.API
}

const token = "YOUR TELEGRAM TOKEN"

// This function needs to be of type 'echotron.NewBotFn' and is called by
// the echotron dispatcher upon any new message from a chatID that has never
// interacted with the bot before.
// This means that echotron keeps one instance of the echotron.Bot implementation
// for each chat where the bot is used.
func newBot(chatID int64) echotron.Bot {
	return &bot{
		chatID,
		echotron.NewAPI(token),
	}
}

// This method is needed to implement the echotron.Bot interface.
func (b *bot) Update(update *echotron.Update) {
	if update.Message.Text == "/start" {
		b.SendMessage("Hello world", b.chatID, nil)
	}
}

func main() {
	// This is the entry point of echotron library.
	dsp := echotron.NewDispatcher(token, newBot)
	log.Println(dsp.Poll())
}

Functional example with bot internal states:

package main

import (
	"log"
	"strings"

	"github.com/NicoNex/echotron/v3"
)

// Recursive type definition of the bot state function.
type stateFn func(*echotron.Update) stateFn

type bot struct {
	chatID int64
	state  stateFn
	name   string
	echotron.API
}

const token = "YOUR TELEGRAM TOKEN"

func newBot(chatID int64) echotron.Bot {
	bot := &bot{
		chatID: chatID,
		API:	echotron.NewAPI(token),
	}
	// We set the default state to the bot.handleMessage method.
	bot.state = bot.handleMessage
	return bot
}

func (b *bot) Update(update *echotron.Update) {
	// Here we execute the current state and set the next one.
	b.state = b.state(update)
}

func (b *bot) handleMessage(update *echotron.Update) {
	if strings.HasPrefix(update.Message.Text, "/set_name") {
		b.SendMessage("Send me my new name!", b.chatID, nil)
		// Here we return b.handleName since next time we receive a message it
		// will be the new name.
		return b.handleName
	}
	return b.handleMessage
}

func (b *bot) handleName(update *echotron.Update) {
	b.name = update.Message.Text
	b.SendMessage(fmt.Sprintf("My new name is %q", b.name), b.chatID, nil)
	// Here we return b.handleMessage since the next time we receive a message
	// it will be handled in the default way.
	return b.handleMessage
}

func main() {
	dsp := echotron.NewDispatcher(token, newBot)
	log.Println(dsp.Poll())
}

Example with self destruction for lower RAM usage:

package main

import (
	"log"
	"time"

	"github.com/NicoNex/echotron/v3"
)

type bot struct {
	chatID int64
	echotron.API
}

const token = "YOUR TELEGRAM TOKEN"

var dsp echotron.Dispatcher

func newBot(chatID int64) echotron.Bot {
	bot := &bot{
		chatID,
		echotron.NewAPI(token),
	}
	go bot.selfDestruct(time.After(time.Hour))
	return bot
}

func (b *bot) selfDestruct(timech <- chan time.Time) {
	<-timech
	b.SendMessage("goodbye", b.chatID, nil)
	dsp.DelSession(b.chatID)
}

func (b *bot) Update(update *echotron.Update) {
	if update.Message.Text == "/start" {
		b.SendMessage("Hello world", b.chatId, nil)
	}
}

func main() {
	dsp = echotron.NewDispatcher(token, newBot)
	log.Println(dsp.Poll())
}

Webhook

package main

import "github.com/NicoNex/echotron/v3"

type bot struct {
	chatID int64
	echotron.API
}

const token = "YOUR TELEGRAM TOKEN"

func newBot(chatID int64) echotron.Bot {
	return &bot{
		chatID,
		echotron.NewAPI(token),
	}
}

func (b *bot) Update(update *echotron.Update) {
	if update.Message.Text == "/start" {
		b.SendMessage("Hello world", b.chatID, nil)
	}
}

func main() {
	dsp := echotron.NewDispatcher(token, newBot)
	dsp.ListenWebhook("https://example.com:443/my_bot_token")
}

Webhook with a custom http.Server

This is an example for a custom http.Server which handles your own specified routes and also the webhook route which is specified by ListenWebhook.

package main

import (
	"github.com/NicoNex/echotron/v3"

	"context"
	"log"
	"net/http"
)

type bot struct {
	chatID int64
	echotron.API
}

const token = "YOUR TELEGRAM TOKEN"

func newBot(chatID int64) echotron.Bot {
	return &bot{
		chatID,
		echotron.NewAPI(token),
	}
}

func (b *bot) Update(update *echotron.Update) {
	if update.Message.Text == "/start" {
		b.SendMessage("Hello world", b.chatID, nil)
	}
}

func main() {
	termChan := make(chan os.Signal, 1) // Channel for terminating the app via os.Interrupt signal
	signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)

	mux := http.NewServeMux()
	mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		// Handle user login
	})
	mux.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
		// Handle user logout
	})
	mux.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
		// Tell something about your awesome telegram bot
	})

	// Set custom http.Server
	server := &http.Server{Addr: ":8080", Handler: mux}

	go func() {
		<-termChan
		// Perform some cleanup..
		if err := server.Shutdown(context.Background()); err != nil {
			log.Print(err)
		}
	}()

	// Capture the interrupt signal for app termination handling
	dsp := echotron.NewDispatcher(token, newBot)
	dsp.SetHTTPServer(server)
	// Start your custom http.Server with a registered /my_bot_token handler.
	log.Println(dsp.ListenWebhook("https://example.com/my_bot_token"))
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].