telegabber/telegram/client.go

104 lines
2.4 KiB
Go
Raw Normal View History

package telegram
import (
"fmt"
"github.com/pkg/errors"
"path/filepath"
"strconv"
"dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence"
"github.com/zelenin/go-tdlib/client"
2019-11-24 17:10:29 +00:00
"gosrc.io/xmpp"
)
var logConstants = map[string]int32{
":fatal": 0,
":error": 1,
":warn": 2,
":info": 3,
":debug": 4,
":verbose": 5,
":all": 1023,
}
func stringToLogConstant(c string) int32 {
level, ok := logConstants[c]
if !ok {
level = 0
}
return level
}
2019-11-05 00:25:15 +00:00
// Client stores the metadata for lazily invoked TDlib instance
type Client struct {
client *client.Client
2019-11-24 17:10:29 +00:00
authorizer *client.ClientAuthorizerType
xmpp *xmpp.Component
jid string
parameters *client.TdlibParameters
2019-11-19 20:25:14 +00:00
Session *persistence.Session
online bool
logVerbosity client.Option
}
// NewClient instantiates a Telegram App
2019-11-24 17:10:29 +00:00
func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
})
2019-11-05 00:25:15 +00:00
apiID, err := strconv.Atoi(conf.Tdlib.Client.APIID)
if err != nil {
2019-11-24 17:10:29 +00:00
return &Client{}, errors.Wrap(err, "Wrong api_id")
}
parameters := client.TdlibParameters{
UseTestDc: false,
DatabaseDirectory: filepath.Join("./sessions/", jid),
FilesDirectory: filepath.Join("./sessions/", jid, "/files/"),
UseFileDatabase: true,
UseChatInfoDatabase: conf.Tdlib.Client.UseChatInfoDatabase,
UseMessageDatabase: true,
UseSecretChats: conf.Tdlib.Client.UseSecretChats,
2019-11-05 00:25:15 +00:00
ApiId: int32(apiID),
ApiHash: conf.Tdlib.Client.APIHash,
SystemLanguageCode: "en",
DeviceModel: conf.Tdlib.Client.DeviceModel,
SystemVersion: "1.0.0",
ApplicationVersion: conf.Tdlib.Client.ApplicationVersion,
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
2019-11-24 17:10:29 +00:00
return &Client{
parameters: &parameters,
2019-11-24 17:10:29 +00:00
xmpp: component,
jid: jid,
2019-11-19 20:25:14 +00:00
Session: session,
logVerbosity: logVerbosity,
}, nil
}
func updateHandler(tdlibClient *client.Client) {
listener := tdlibClient.GetListener()
defer listener.Close()
for update := range listener.Updates {
if update.GetClass() == client.ClassUpdate {
fmt.Printf("%#v", update)
}
}
}
2019-11-24 17:10:29 +00:00
func (c *Client) ProcessOutgoingMessage(chatID int, text string, messageID int) {
// TODO
}