Skip to content

Commit

Permalink
Example of using Mtproto for Telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
vb64 committed Nov 28, 2020
1 parent 412949a commit b6fb067
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/auth_bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Example of using Mtproto for Telegram bot

Mtproto lib can be used with Telegram bot, that can acess to full Telegram API, not just the simplified Telegram Bot API.

First, we need get register our bot via Telegram [@BotFather](https://t.me/BotFather) and obtain *bot token* and *bot username*.

![Obtain bot token via BotFather](img/bot_father.jpg)

Next we [register our app in Telegram](https://my.telegram.org/apps) and obtain

- ApiID
- ApiHash
- IP addresses of MTProto servers
- Public keys

![App credentials](img/telegram_org.jpg)

Save text from "Public keys" field to the `keys.pem` file.

Other credentials put to `const` section of `main.go` file.

```go
const (
// from https://my.telegram.org/apps
TgAppID = XXXXX // integer value from "App api_id" field
TgAppHash = "XXXXXXXXXXXX" // string value from "App api_hash" field
TgTestServer = "149.154.167.40:443" // string value from "Test configuration" field
TgProdServer = "149.154.167.50:443" // string value from "Production configuration" field

// from https://t.me/BotFather
TgBotToken = "XXXXX" // bot token from BotFather
TgBotUserName = "YourBotUserName" // username of the bot
)
```

Then run:

```
go run main.go
```

For my bot I have next output:

![Bot info](img/go_run.jpg)
Binary file added examples/auth_bot/img/bot_father.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/auth_bot/img/go_run.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/auth_bot/img/telegram_org.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/auth_bot/keys.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
put here the text from "Public keys" field at https://my.telegram.org/apps
105 changes: 105 additions & 0 deletions examples/auth_bot/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Example of using Mtproto for Telegram bot
package main

import (
"fmt"
"os"

"github.com/xelaj/mtproto/telegram"
)

const (
// from https://my.telegram.org/apps
TgAppID = XXXXX // integer value from "App api_id" field
TgAppHash = "XXXXXXXXXXXX" // string value from "App api_hash" field
TgTestServer = "149.154.167.40:443" // string value from "Test configuration" field
TgProdServer = "149.154.167.50:443" // string value from "Production configuration" field

// from https://t.me/BotFather
TgBotToken = "XXXXX" // bot token from BotFather
TgBotUserName = "YourBotUserName" // username of the bot
)

func main() {
client, err := telegram.NewClient(telegram.ClientConfig{
// current dir must be writable
// file 'session.json' will be created here
SessionFile: "./session.json",
// file 'keys.pem' must contain text from "Public keys" field
// from https://my.telegram.org/apps
PublicKeysFile: "./keys.pem",
// we need to use production Telegram API server
// because test server don't know about our bot
ServerHost: TgProdServer,
AppID: TgAppID,
AppHash: TgAppHash,
})
if err != nil {
fmt.Println("NewClient error:", err.Error())
os.Exit(1)
}

// Trying to auth as bot with our bot token
_, err = client.AuthImportBotAuthorization(&telegram.AuthImportBotAuthorizationParams{
Flags: 1, // reserved, must be set (not 0)
ApiId: TgAppID,
ApiHash: TgAppHash,
BotAuthToken: TgBotToken,
})
if err != nil {
fmt.Println("ImportBotAuthorization error:", err.Error())
os.Exit(1)
}

// Request info about username of our bot
uname, err := client.ContactsResolveUsername(&telegram.ContactsResolveUsernameParams{Username: TgBotUserName})
if err != nil {
fmt.Println("ResolveUsername error:", err.Error())
os.Exit(1)
}

chatsCount := len(uname.Chats)
// No chats for requested username of our bot
if chatsCount > 0 {
fmt.Println("Chats number:", chatsCount)
os.Exit(1)
}

usersCount := len(uname.Users)
// Users vector must contain single item with information about our bot
if usersCount != 1 {
fmt.Println("Users number:", usersCount)
os.Exit(1)
}

user := uname.Users[0].(*telegram.UserObj)

// dump our bot info
fmt.Println("\nSelf ->", user.Self)
fmt.Println("Username ->", user.Username)
fmt.Println("FirstName ->", user.FirstName)
fmt.Println("LastName ->", user.LastName)
fmt.Println("Id ->", user.Id)
fmt.Println("Bot ->", user.Bot)
fmt.Println("Verified ->", user.Verified)
fmt.Println("Restricted ->", user.Restricted)
fmt.Println("Support ->", user.Support)
fmt.Println("Scam ->", user.Scam)
fmt.Println("BotInfoVersion ->", user.BotInfoVersion)

// fmt.Println("Contact ->", user.Contact)
// fmt.Println("MutualContact ->", user.MutualContact)
// fmt.Println("Deleted ->", user.Deleted)
// fmt.Println("BotChatHistory ->", user.BotChatHistory)
// fmt.Println("BotNochats ->", user.BotNochats)
// fmt.Println("Min ->", user.Min)
// fmt.Println("BotInlineGeo ->", user.BotInlineGeo)
// fmt.Println("ApplyMinPhoto ->", user.ApplyMinPhoto)
// fmt.Println("AccessHash ->", user.AccessHash)
// fmt.Println("Phone ->", user.Phone)
// fmt.Println("Photo ->", user.Photo)
// fmt.Println("Status ->", user.Status)
// fmt.Println("RestrictionReason ->", user.RestrictionReason)
// fmt.Println("BotInlinePlaceholder ->", user.BotInlinePlaceholder)
// fmt.Println("LangCode ->", user.LangCode)
}

0 comments on commit b6fb067

Please sign in to comment.