Project Setup

Project Setup

We have just installed Gogram, in this page ww will discuss what you need to do in order to setup a new project with this framework.

Api-Key (App ID and App Hash)

The first step requires you to obtain a Telegram API key.

In order to do so, follow Telegram’s instructions at https://core.telegram.org/api/obtaining_api_id (opens in a new tab) and make sure you understand and abide to the rules for third-party clients and libraries explained there.

The API key consists of two parts: the App ID and the App Hash. You will need to provide these two values to Gogram in order to authenticate your client.

Auto-Configuration

Auto-obtaining the API key is supported by Gogram. You can use the following code to automatically obtain the API key:

main.go
package main
 
import (
    "github.com/amarnathcjd/gogram/telegram"
)
 
func main() {
    webcodecallback := func() (string, error) {
        fmt.Println("Please enter the web code:")
        var code string
        fmt.Scanln(&code)
 
        return code, nil
    }
 
    client, _ := telegram.NewClient(telegram.ClientConfig{}) // create an empty client
    appId, appHash, success, err := client.ScrapeAppConfig(&telegram.ScrapeConfig{
        PhoneNum:         "your_phone_number",
        WebCodeCallback:  webcodecallback,
    })
 
    if !success {
        fmt.Println("Failed to scrape app config:", err)
        return
    }
 
    client.SetAppID(appId)
    client.SetAppHash(appHash)
 
    client.Start()
}

Manual Configuration

If you prefer to manually configure the API key, you can use the following code:

main.go
package main
 
import (
    "github.com/amarnathcjd/gogram/telegram"
)
 
func main() {
    client, _ := telegram.NewClient(telegram.ClientConfig{ // Create a new client
        AppID:   6,
        AppHash: "app_hash",
    })
    client.Start()
}