leobot

Simple Telegram Logging Bot
git clone git://bsandro.tech/leobot
Log | Files | Refs | README | LICENSE

README.md (3430B)


      1 # Golang bindings for the Telegram Bot API
      2 
      3 [![Go Reference](https://pkg.go.dev/badge/github.com/go-telegram-bot-api/telegram-bot-api/v5.svg)](https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5)
      4 [![Test](https://github.com/go-telegram-bot-api/telegram-bot-api/actions/workflows/test.yml/badge.svg)](https://github.com/go-telegram-bot-api/telegram-bot-api/actions/workflows/test.yml)
      5 
      6 All methods are fairly self-explanatory, and reading the [godoc](https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5) page should
      7 explain everything. If something isn't clear, open an issue or submit
      8 a pull request.
      9 
     10 There are more tutorials and high-level information on the website, [go-telegram-bot-api.dev](https://go-telegram-bot-api.dev).
     11 
     12 The scope of this project is just to provide a wrapper around the API
     13 without any additional features. There are other projects for creating
     14 something with plugins and command handlers without having to design
     15 all that yourself.
     16 
     17 Join [the development group](https://telegram.me/go_telegram_bot_api) if
     18 you want to ask questions or discuss development.
     19 
     20 ## Example
     21 
     22 First, ensure the library is installed and up to date by running
     23 `go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5`.
     24 
     25 This is a very simple bot that just displays any gotten updates,
     26 then replies it to that chat.
     27 
     28 ```go
     29 package main
     30 
     31 import (
     32 	"log"
     33 
     34 	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
     35 )
     36 
     37 func main() {
     38 	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
     39 	if err != nil {
     40 		log.Panic(err)
     41 	}
     42 
     43 	bot.Debug = true
     44 
     45 	log.Printf("Authorized on account %s", bot.Self.UserName)
     46 
     47 	u := tgbotapi.NewUpdate(0)
     48 	u.Timeout = 60
     49 
     50 	updates := bot.GetUpdatesChan(u)
     51 
     52 	for update := range updates {
     53 		if update.Message != nil { // If we got a message
     54 			log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
     55 
     56 			msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
     57 			msg.ReplyToMessageID = update.Message.MessageID
     58 
     59 			bot.Send(msg)
     60 		}
     61 	}
     62 }
     63 ```
     64 
     65 If you need to use webhooks (if you wish to run on Google App Engine),
     66 you may use a slightly different method.
     67 
     68 ```go
     69 package main
     70 
     71 import (
     72 	"log"
     73 	"net/http"
     74 
     75 	"github.com/go-telegram-bot-api/telegram-bot-api/v5"
     76 )
     77 
     78 func main() {
     79 	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
     80 	if err != nil {
     81 		log.Fatal(err)
     82 	}
     83 
     84 	bot.Debug = true
     85 
     86 	log.Printf("Authorized on account %s", bot.Self.UserName)
     87 
     88 	wh, _ := tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
     89 
     90 	_, err = bot.SetWebhook(wh)
     91 	if err != nil {
     92 		log.Fatal(err)
     93 	}
     94 
     95 	info, err := bot.GetWebhookInfo()
     96 	if err != nil {
     97 		log.Fatal(err)
     98 	}
     99 
    100 	if info.LastErrorDate != 0 {
    101 		log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
    102 	}
    103 
    104 	updates := bot.ListenForWebhook("/" + bot.Token)
    105 	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
    106 
    107 	for update := range updates {
    108 		log.Printf("%+v\n", update)
    109 	}
    110 }
    111 ```
    112 
    113 If you need, you may generate a self-signed certificate, as this requires
    114 HTTPS / TLS. The above example tells Telegram that this is your
    115 certificate and that it should be trusted, even though it is not
    116 properly signed.
    117 
    118     openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
    119 
    120 Now that [Let's Encrypt](https://letsencrypt.org) is available,
    121 you may wish to generate your free TLS certificate there.