diff --git a/examples/auth_bot/README.md b/examples/auth_bot/README.md new file mode 100644 index 0000000..9267f21 --- /dev/null +++ b/examples/auth_bot/README.md @@ -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) diff --git a/examples/auth_bot/img/bot_father.jpg b/examples/auth_bot/img/bot_father.jpg new file mode 100644 index 0000000..a3c20ba Binary files /dev/null and b/examples/auth_bot/img/bot_father.jpg differ diff --git a/examples/auth_bot/img/go_run.jpg b/examples/auth_bot/img/go_run.jpg new file mode 100644 index 0000000..4cdc6e5 Binary files /dev/null and b/examples/auth_bot/img/go_run.jpg differ diff --git a/examples/auth_bot/img/telegram_org.jpg b/examples/auth_bot/img/telegram_org.jpg new file mode 100644 index 0000000..2a68e0d Binary files /dev/null and b/examples/auth_bot/img/telegram_org.jpg differ diff --git a/examples/auth_bot/keys.pem b/examples/auth_bot/keys.pem new file mode 100644 index 0000000..68bc3ed --- /dev/null +++ b/examples/auth_bot/keys.pem @@ -0,0 +1 @@ +put here the text from "Public keys" field at https://my.telegram.org/apps \ No newline at end of file diff --git a/examples/auth_bot/main.go b/examples/auth_bot/main.go new file mode 100644 index 0000000..2a3fa60 --- /dev/null +++ b/examples/auth_bot/main.go @@ -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) +}