Session Stores

Session Stores

Sessions are a way to store user authentication information securely

File Session

File session is the default session type used by Gogram. It stores the session information in a file named session.dat 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.

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",
        SessionFile: "session.dat",
    })
    client.Start()
}

String Session

String session is an alternative session type that stores the session information in a string. This is useful when you want to store the session information in a database or other storage medium.

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",
        SessionString: "your_session_string",
    })
    client.Start()
 
    // export the session string
    fmt.Println(client.ExportSession())
}

Custom Session

You can also create a custom session type by using the methods of client.ExportRawSession & client.ImportRawSession

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()
 
    // export the raw session
    rawSession := client.ExportRawSession()
 
    // save the raw session to a file or database
    // the contains authKey, authHash, ipAddress, APIKey
 
    data, _ := json.Marshal(rawSession)
    fmt.Println(string(data))
 
    // import the raw session
    client.ImportRawSession(rawSession.Key, rawSession.Hash, rawSession.Hostname, rawSession.AppID)
}