Categorygithub.com/animber-coder/echosphere/v2
package
2.4.0
Repository: https://github.com/animber-coder/echosphere.git
Documentation: pkg.go.dev

# README

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

Library for telegram bots written in pure go

Fetch with

go get github.com/animber-coder/echosphere/v2

Usage

Long Polling

A very simple implementation:

package main

import (
    "log"

    "github.com/animber-coder/echosphere/v2"
)

type bot struct {
    chatId int64
    echosphere.API
}

const token = "YOUR TELEGRAM TOKEN"

func newBot(chatId int64) echosphere.Bot {
    return &bot{
        chatId,
        echosphere.NewAPI(token),
    }
}

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

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

Also proof of concept with self destruction for low ram usage

package main

import (
    "log"
    "time"

    "github.com/animber-coder/echosphere/v2"
)

type bot struct {
    chatId int64
    echosphere.API
}

const token = "YOUR TELEGRAM TOKEN"

var dsp echosphere.Dispatcher

func newBot(chatId int64) echosphere.Bot {
    var bot = &bot{
        chatId,
        echosphere.NewAPI(token),
    }
    go bot.selfDestruct(time.After(time.Hour))
    return bot
}

func (b *bot) selfDestruct(timech <- chan time.Time) {
    select {
    case <-timech:
        b.SendMessage("goodbye", b.chatId)
        dsp.DelSession(b.chatId)
    }
}

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

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

Webhook

package main

import "github.com/animber-coder/echosphere/v2"

type bot struct {
	chatId int64
	echosphere.API
}

const token = "YOUR TELEGRAM TOKEN"

func newBot(chatId int64) echosphere.Bot {
	return &bot{
		chatId,
		echosphere.NewAPI(token),
	}
}

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

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