Authorization
User Authorization
In order to use the Telegram API, you need to authorize your client with a user account. This is done by providing the client with the user's phone number and the authorization code sent to the user's device.
package main
import (
"fmt"
"github.com/amarnathcjd/gogram/telegram"
)
func main() {
client, _ := telegram.NewClient(telegram.ClientConfig{
AppID: 6,
AppHash: "app_hash",
})
client.Conn() // establish connection to the Telegram server
client.Login("your_phone_number")
fmt.Println(client.GetMe())
}
This starts an interactive shell that prompts you to enter the authorization code sent to your device. Once you have entered the code, the client will be authorized and you can start using the Telegram API.
While entering the phone number, make sure to enter the phone number in the international format (e.g., +1 123-456-7890).
Enter code: xxxxx
// if 2FA is enabled
Two-steps verification is enabled
Enter password: your_password
After you have successfully authorized your client, a session.dat
file will be created in the current working directory. This file contains the session information and is used to automatically log in the client the next time you run the program.
keep this file safe and do not share it with anyone.
Bot Authorization
In order to use the Telegram Bot API, you need to create a bot and obtain the bot token. This is done by talking to the BotFather (opens in a new tab) on Telegram.
package main
import (
"fmt"
"github.com/amarnathcjd/gogram/telegram"
)
func main() {
client, _ := telegram.NewClient(telegram.ClientConfig{
AppID: 6,
AppHash: "app_hash",
})
client.Conn() // establish connection to the Telegram server
client.LoginBot("bot_token")
fmt.Println(client.GetMe())
}