Skip to content

Commit 61aa4b5

Browse files
committed
Initial commit
0 parents  commit 61aa4b5

File tree

147 files changed

+15480
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+15480
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.xml
2+
*.iml
3+
.idea

README.md

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
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+
```

SECURITY.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
Use this section to tell people about which versions of your project are
6+
currently being supported with security updates.
7+
8+
| Version | Supported |
9+
|---------|--------------------|
10+
| 5.1.x | :white_check_mark: |
11+
| 5.0.x | :x: |
12+
| 4.0.x | :white_check_mark: |
13+
| < 4.0 | :x: |
14+
15+
## Reporting a Vulnerability
16+
17+
Use this section to tell people how to report a vulnerability.
18+
19+
Tell them where to go, how often they can expect to get an update on a
20+
reported vulnerability, what to expect if the vulnerability is accepted or
21+
declined, etc.

client/Client.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"net"
7+
"net/http"
8+
"net/url"
9+
)
10+
11+
type Config struct {
12+
HttpC *http.Client
13+
Proxy string
14+
ForceV4 bool
15+
DisableSSLVerify bool
16+
ForceAttemptHTTP2 bool
17+
BaseUrl string
18+
}
19+
20+
// Setup setup a new client to use telegram api
21+
func (c *Config) Setup() error {
22+
var proxy func(*http.Request) (*url.URL, error)
23+
var myDC func(ctx context.Context, network, addr string) (net.Conn, error)
24+
var TLSClientConfig *tls.Config
25+
var forceAttemptHTTP2 bool
26+
27+
if c.Proxy != "" {
28+
proxyUrl, err := url.Parse(c.Proxy)
29+
if err != nil {
30+
return err
31+
}
32+
if proxyUrl != nil {
33+
proxy = http.ProxyURL(proxyUrl)
34+
} else {
35+
proxy = nil
36+
}
37+
} else {
38+
proxy = nil
39+
}
40+
41+
if c.ForceV4 {
42+
myDC = func(ctx context.Context, network string, addr string) (net.Conn, error) {
43+
return (&net.Dialer{}).DialContext(ctx, "tcp4", addr)
44+
}
45+
} else {
46+
myDC = nil
47+
}
48+
49+
if c.DisableSSLVerify {
50+
TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
51+
} else {
52+
TLSClientConfig = &tls.Config{InsecureSkipVerify: false}
53+
}
54+
55+
if c.ForceAttemptHTTP2 {
56+
forceAttemptHTTP2 = true
57+
}
58+
59+
c.HttpC = &http.Client{
60+
Transport: &http.Transport{
61+
Proxy: proxy,
62+
ForceAttemptHTTP2: forceAttemptHTTP2,
63+
DialContext: myDC,
64+
TLSClientConfig: TLSClientConfig,
65+
},
66+
}
67+
68+
return nil
69+
}

0 commit comments

Comments
 (0)