Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raminsa committed Sep 24, 2024
0 parents commit 61aa4b5
Show file tree
Hide file tree
Showing 147 changed files with 15,480 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.xml
*.iml
.idea
260 changes: 260 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
# Telegram Bot API

The Telegram Bot API provides an HTTP API for creating Telegram Bots.

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.

Please note that only global Bot API issues that affect all bots are suitable for this repository.

To learn how to use it, please see our [examples](https://github.com/raminsa/telegram-bot-api/tree/main/examples).

Bot API 7.7 recent changes [July 7, 2024](https://core.telegram.org/bots/api#july-7-2024).

## Table of Contents
- [Installation](#installation)
- [Documentation](#documentation)
- [Example](#example)
- [Custom Client](#custom-client)
- [Debug](#debug)

<a name="installation"></a>
## Installation
`go get github.com/raminsa/telegram-bot-api`.

<a name="documentation"></a>
## Documentation
See [Bots: An introduction for developers](https://core.telegram.org/bots) for a brief description of Telegram Bots and their features.

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.

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.

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).


<a name="example"></a>
## Example

use get update method (simple):
```go
package main

import (
"fmt"
"log"

"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
tg, err := telegram.New("BotToken")
if err != nil {
log.Fatal(err)
}

getUpdates := tg.NewGetUpdates()
getUpdates.Offset = 1
getUpdates.Timeout = 60
getUpdates.Limit = 100
updates := tg.GetUpdatesChan(getUpdates)
for update := range updates {
fmt.Println(update.UpdateID, getUpdates.Offset)
if update.UpdateID >= getUpdates.Offset {
getUpdates.Offset = update.UpdateID + 1
}
if update.Message != nil {
fmt.Println(update.Message.Text)
}
}
}
```

use webhook method (http):
```go
package main

import (
"fmt"
"log"
"net/http"

"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
fmt.Println("start at port:", "BotPortNumber")
err := http.ListenAndServe("BotPortNumber", http.HandlerFunc(handleWebhook))
if err != nil {
log.Fatal(err)
}
}

func handleWebhook(w http.ResponseWriter, r *http.Request) {
update, err := telegram.HandleUpdate(r)
if err != nil {
err = telegram.HandleUpdateError(w, err)
if err != nil {
//handle err
}
return
}

if update.Message != nil {
fmt.Println(update.Message.Text)
}
}
```

use webhook method (https):
```go
package main

import (
"fmt"
"log"
"net/http"

"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
fmt.Println("start at port:", "BotPortNumber")
err := http.ListenAndServeTLS("BotPortNumber", "BotCertFile", "BotKeyFile", http.HandlerFunc(handleWebhook))
if err != nil {
log.Fatal(err)
}
}

func handleWebhook(w http.ResponseWriter, r *http.Request) {
update, err := telegram.HandleUpdate(r)
if err != nil {
err := telegram.HandleUpdateError(w, err)
if err != nil {
//handle err
}
return
}

if update.Message != nil {
fmt.Println(update.Message.Text)
}
}
```

to generate your cert file use this. See [self-signed](https://core.telegram.org/bots/self-signed) guide for details.:

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>"


use a channel to handle all requests (Avoid ReadTimeoutExpired error), http.ListenAndServe() supports concurrency:
```go
package main

import (
"fmt"
"net/http"

"github.com/raminsa/telegram-bot-api/telegram"
"github.com/raminsa/telegram-bot-api/types"
)

func main() {
fmt.Println("start at port:", "BotPortNumber")
updates := listenForWebhook(100)
go http.ListenAndServeTLS("BotPortNumber", "BotCertFile", "BotKeyFile", nil)
for update := range updates {
if update.Message != nil {
fmt.Println(update.Message.Text)
}
}
}

func listenForWebhook(maxWebhookConnections int) types.UpdatesChannel {
ch := make(chan types.Update, maxWebhookConnections)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if update, err := telegram.HandleUpdate(r); err != nil {
return
} else {
ch <- *update
}
})

return ch
}
```

<a name="custom-client"></a>
## Custom Client
use client with custom options:
```go
package main

import (
"fmt"
"log"

"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
client := telegram.Client()
client.BaseUrl = "baseUrl"
client.Proxy = "proxy"
client.ForceV4 = true
client.DisableSSLVerify = true
client.ForceAttemptHTTP2 = true
tg, err := telegram.NewWithCustomClient("BotToken", client)
if err != nil {
log.Fatal(err)
}

me, err := tg.GetMe()
if err != nil {
log.Fatal(err)
}

fmt.Println("botID:", me.ID, "botUsername:", me.UserName)
}
```

<a name="debug"></a>
## Debug

use debug option and write to local file:
```go
package main

import (
"fmt"
"log"

"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
tg, err := telegram.NewWithBaseUrl("BotToken", "baseUrl")
if err != nil {
log.Fatal(err)
}

//active debug mode
tg.Bot.Debug = true

me, err := tg.GetMe()
if err != nil {
log.Fatal(err)
}

fmt.Println("botID:", me.ID, "botUsername:", me.UserName)

//access debug log
//method 1: write to console
fmt.Println(tg.GetLoggerFile())

//method 2: write to file
err = tg.WriteLoggerFile("fileName")
if err != nil {
log.Fatal(err)
}
}
```
21 changes: 21 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Security Policy

## Supported Versions

Use this section to tell people about which versions of your project are
currently being supported with security updates.

| Version | Supported |
|---------|--------------------|
| 5.1.x | :white_check_mark: |
| 5.0.x | :x: |
| 4.0.x | :white_check_mark: |
| < 4.0 | :x: |

## Reporting a Vulnerability

Use this section to tell people how to report a vulnerability.

Tell them where to go, how often they can expect to get an update on a
reported vulnerability, what to expect if the vulnerability is accepted or
declined, etc.
69 changes: 69 additions & 0 deletions client/Client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package client

import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
)

type Config struct {
HttpC *http.Client
Proxy string
ForceV4 bool
DisableSSLVerify bool
ForceAttemptHTTP2 bool
BaseUrl string
}

// Setup setup a new client to use telegram api
func (c *Config) Setup() error {
var proxy func(*http.Request) (*url.URL, error)
var myDC func(ctx context.Context, network, addr string) (net.Conn, error)
var TLSClientConfig *tls.Config
var forceAttemptHTTP2 bool

if c.Proxy != "" {
proxyUrl, err := url.Parse(c.Proxy)
if err != nil {
return err
}
if proxyUrl != nil {
proxy = http.ProxyURL(proxyUrl)
} else {
proxy = nil
}
} else {
proxy = nil
}

if c.ForceV4 {
myDC = func(ctx context.Context, network string, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "tcp4", addr)
}
} else {
myDC = nil
}

if c.DisableSSLVerify {
TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
TLSClientConfig = &tls.Config{InsecureSkipVerify: false}
}

if c.ForceAttemptHTTP2 {
forceAttemptHTTP2 = true
}

c.HttpC = &http.Client{
Transport: &http.Transport{
Proxy: proxy,
ForceAttemptHTTP2: forceAttemptHTTP2,
DialContext: myDC,
TLSClientConfig: TLSClientConfig,
},
}

return nil
}
Loading

0 comments on commit 61aa4b5

Please sign in to comment.