|
| 1 | +# Telegram Bot API |
| 2 | + |
| 3 | +The Telegram Bot API provides an HTTP API for creating Telegram Bots. |
| 4 | + |
| 5 | +If you've got any questions about bots or would like to report an issue with your bot, kindly contact us at @BotSupport in Telegram. |
| 6 | + |
| 7 | +Please note that only global Bot API issues that affect all bots are suitable for this repository. |
| 8 | + |
| 9 | +To learn how to use it, please see our [examples](https://github.com/raminsa/telegram-bot-api/tree/main/examples). |
| 10 | + |
| 11 | +Bot API 7.7 recent changes [July 7, 2024](https://core.telegram.org/bots/api#july-7-2024). |
| 12 | + |
| 13 | +## Table of Contents |
| 14 | +- [Installation](#installation) |
| 15 | +- [Documentation](#documentation) |
| 16 | +- [Example](#example) |
| 17 | +- [Custom Client](#custom-client) |
| 18 | +- [Debug](#debug) |
| 19 | + |
| 20 | +<a name="installation"></a> |
| 21 | +## Installation |
| 22 | +`go get github.com/raminsa/telegram-bot-api`. |
| 23 | + |
| 24 | +<a name="documentation"></a> |
| 25 | +## Documentation |
| 26 | +See [Bots: An introduction for developers](https://core.telegram.org/bots) for a brief description of Telegram Bots and their features. |
| 27 | + |
| 28 | +See the [Telegram Bot API documentation](https://core.telegram.org/bots/api) for a description of the Bot API interface and a complete list of available classes, methods and updates. |
| 29 | + |
| 30 | +See the [Telegram Bot API server build instruction generator](https://tdlib.github.io/telegram-bot-api/build.html) for detailed instructions on how to build the Telegram Bot API server. |
| 31 | + |
| 32 | +Subscribe to [@BotNews](https://t.me/botnews) to be the first to know about the latest updates and join the discussion in [@BotTalk](https://t.me/bottalk). |
| 33 | + |
| 34 | + |
| 35 | +<a name="example"></a> |
| 36 | +## Example |
| 37 | + |
| 38 | +use get update method (simple): |
| 39 | +```go |
| 40 | +package main |
| 41 | + |
| 42 | +import ( |
| 43 | + "fmt" |
| 44 | + "log" |
| 45 | + |
| 46 | + "github.com/raminsa/telegram-bot-api/telegram" |
| 47 | +) |
| 48 | + |
| 49 | +func main() { |
| 50 | + tg, err := telegram.New("BotToken") |
| 51 | + if err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + getUpdates := tg.NewGetUpdates() |
| 56 | + getUpdates.Offset = 1 |
| 57 | + getUpdates.Timeout = 60 |
| 58 | + getUpdates.Limit = 100 |
| 59 | + updates := tg.GetUpdatesChan(getUpdates) |
| 60 | + for update := range updates { |
| 61 | + fmt.Println(update.UpdateID, getUpdates.Offset) |
| 62 | + if update.UpdateID >= getUpdates.Offset { |
| 63 | + getUpdates.Offset = update.UpdateID + 1 |
| 64 | + } |
| 65 | + if update.Message != nil { |
| 66 | + fmt.Println(update.Message.Text) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +use webhook method (http): |
| 73 | +```go |
| 74 | +package main |
| 75 | + |
| 76 | +import ( |
| 77 | + "fmt" |
| 78 | + "log" |
| 79 | + "net/http" |
| 80 | + |
| 81 | + "github.com/raminsa/telegram-bot-api/telegram" |
| 82 | +) |
| 83 | + |
| 84 | +func main() { |
| 85 | + fmt.Println("start at port:", "BotPortNumber") |
| 86 | + err := http.ListenAndServe("BotPortNumber", http.HandlerFunc(handleWebhook)) |
| 87 | + if err != nil { |
| 88 | + log.Fatal(err) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func handleWebhook(w http.ResponseWriter, r *http.Request) { |
| 93 | + update, err := telegram.HandleUpdate(r) |
| 94 | + if err != nil { |
| 95 | + err = telegram.HandleUpdateError(w, err) |
| 96 | + if err != nil { |
| 97 | + //handle err |
| 98 | + } |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + if update.Message != nil { |
| 103 | + fmt.Println(update.Message.Text) |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +use webhook method (https): |
| 109 | +```go |
| 110 | +package main |
| 111 | + |
| 112 | +import ( |
| 113 | + "fmt" |
| 114 | + "log" |
| 115 | + "net/http" |
| 116 | + |
| 117 | + "github.com/raminsa/telegram-bot-api/telegram" |
| 118 | +) |
| 119 | + |
| 120 | +func main() { |
| 121 | + fmt.Println("start at port:", "BotPortNumber") |
| 122 | + err := http.ListenAndServeTLS("BotPortNumber", "BotCertFile", "BotKeyFile", http.HandlerFunc(handleWebhook)) |
| 123 | + if err != nil { |
| 124 | + log.Fatal(err) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func handleWebhook(w http.ResponseWriter, r *http.Request) { |
| 129 | + update, err := telegram.HandleUpdate(r) |
| 130 | + if err != nil { |
| 131 | + err := telegram.HandleUpdateError(w, err) |
| 132 | + if err != nil { |
| 133 | + //handle err |
| 134 | + } |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + if update.Message != nil { |
| 139 | + fmt.Println(update.Message.Text) |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +to generate your cert file use this. See [self-signed](https://core.telegram.org/bots/self-signed) guide for details.: |
| 145 | + |
| 146 | + openssl req -newkey rsa:2048 -sha256 -nodes -keyout <file.key> -x509 -days 36500 -out <file.pem> -subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=<server_address>" |
| 147 | + |
| 148 | + |
| 149 | +use a channel to handle all requests (Avoid ReadTimeoutExpired error), http.ListenAndServe() supports concurrency: |
| 150 | +```go |
| 151 | +package main |
| 152 | + |
| 153 | +import ( |
| 154 | + "fmt" |
| 155 | + "net/http" |
| 156 | + |
| 157 | + "github.com/raminsa/telegram-bot-api/telegram" |
| 158 | + "github.com/raminsa/telegram-bot-api/types" |
| 159 | +) |
| 160 | + |
| 161 | +func main() { |
| 162 | + fmt.Println("start at port:", "BotPortNumber") |
| 163 | + updates := listenForWebhook(100) |
| 164 | + go http.ListenAndServeTLS("BotPortNumber", "BotCertFile", "BotKeyFile", nil) |
| 165 | + for update := range updates { |
| 166 | + if update.Message != nil { |
| 167 | + fmt.Println(update.Message.Text) |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func listenForWebhook(maxWebhookConnections int) types.UpdatesChannel { |
| 173 | + ch := make(chan types.Update, maxWebhookConnections) |
| 174 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 175 | + if update, err := telegram.HandleUpdate(r); err != nil { |
| 176 | + return |
| 177 | + } else { |
| 178 | + ch <- *update |
| 179 | + } |
| 180 | + }) |
| 181 | + |
| 182 | + return ch |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +<a name="custom-client"></a> |
| 187 | +## Custom Client |
| 188 | +use client with custom options: |
| 189 | +```go |
| 190 | +package main |
| 191 | + |
| 192 | +import ( |
| 193 | + "fmt" |
| 194 | + "log" |
| 195 | + |
| 196 | + "github.com/raminsa/telegram-bot-api/telegram" |
| 197 | +) |
| 198 | + |
| 199 | +func main() { |
| 200 | + client := telegram.Client() |
| 201 | + client.BaseUrl = "baseUrl" |
| 202 | + client.Proxy = "proxy" |
| 203 | + client.ForceV4 = true |
| 204 | + client.DisableSSLVerify = true |
| 205 | + client.ForceAttemptHTTP2 = true |
| 206 | + tg, err := telegram.NewWithCustomClient("BotToken", client) |
| 207 | + if err != nil { |
| 208 | + log.Fatal(err) |
| 209 | + } |
| 210 | + |
| 211 | + me, err := tg.GetMe() |
| 212 | + if err != nil { |
| 213 | + log.Fatal(err) |
| 214 | + } |
| 215 | + |
| 216 | + fmt.Println("botID:", me.ID, "botUsername:", me.UserName) |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +<a name="debug"></a> |
| 221 | +## Debug |
| 222 | + |
| 223 | +use debug option and write to local file: |
| 224 | +```go |
| 225 | +package main |
| 226 | + |
| 227 | +import ( |
| 228 | + "fmt" |
| 229 | + "log" |
| 230 | + |
| 231 | + "github.com/raminsa/telegram-bot-api/telegram" |
| 232 | +) |
| 233 | + |
| 234 | +func main() { |
| 235 | + tg, err := telegram.NewWithBaseUrl("BotToken", "baseUrl") |
| 236 | + if err != nil { |
| 237 | + log.Fatal(err) |
| 238 | + } |
| 239 | + |
| 240 | + //active debug mode |
| 241 | + tg.Bot.Debug = true |
| 242 | + |
| 243 | + me, err := tg.GetMe() |
| 244 | + if err != nil { |
| 245 | + log.Fatal(err) |
| 246 | + } |
| 247 | + |
| 248 | + fmt.Println("botID:", me.ID, "botUsername:", me.UserName) |
| 249 | + |
| 250 | + //access debug log |
| 251 | + //method 1: write to console |
| 252 | + fmt.Println(tg.GetLoggerFile()) |
| 253 | + |
| 254 | + //method 2: write to file |
| 255 | + err = tg.WriteLoggerFile("fileName") |
| 256 | + if err != nil { |
| 257 | + log.Fatal(err) |
| 258 | + } |
| 259 | +} |
| 260 | +``` |
0 commit comments