From 753a488c9de0a188dbc963b7ba967b9deb87bae2 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Thu, 5 Dec 2019 20:13:17 +0200 Subject: [PATCH] Add /set* commands --- telegram/commands.go | 76 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/telegram/commands.go b/telegram/commands.go index dc02ff4..c0fe05f 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -1,26 +1,27 @@ package telegram import ( + "github.com/pkg/errors" "strconv" "strings" "dev.narayana.im/narayana/telegabber/xmpp/gateway" - log "github.com/sirupsen/logrus" + "github.com/zelenin/go-tdlib/client" ) const notEnoughArguments string = "Not enough arguments" const telegramNotInitialized string = "Telegram connection is not initialized yet" var transportCommands = map[string]command{ - "login": command{"phone", "sign in"}, - "logout": command{"", "sign out"}, - "code": command{"", "check one-time code"}, - "password": command{"", "check 2fa password"}, - //"setusername": command{"", "update @username"}, - //"setname": command{"first last", "update name"}, - //"setbio": command{"", "update about"}, - //"setpassword": command{"[old] [new]", "set or remove password"}, + "login": command{"phone", "sign in"}, + "logout": command{"", "sign out"}, + "code": command{"", "check one-time code"}, + "password": command{"", "check 2fa password"}, + "setusername": command{"", "update @username"}, + "setname": command{"first last", "update name"}, + "setbio": command{"", "update about"}, + "setpassword": command{"[old] [new]", "set or remove password"}, //"config": command{"[param] [value]", "view or update configuration options"}, } @@ -140,7 +141,7 @@ func (c *Client) ProcessTransportCommand(cmdline string) string { case "logout": _, err := c.client.LogOut() if err != nil { - log.Errorf("Logout error: %v", err) + return errors.Wrap(err, "Logout error").Error() } for id := range c.cache.chats { @@ -153,6 +154,61 @@ func (c *Client) ProcessTransportCommand(cmdline string) string { } c.Session.Login = "" + // set @username + case "setusername": + var username string + if len(args) > 0 { + username = args[0] + } + + _, err := c.client.SetUsername(&client.SetUsernameRequest{ + Username: username, + }) + if err != nil { + return errors.Wrap(err, "Couldn't set username").Error() + } + // set My Name + case "setname": + var firstname string + var lastname string + if len(args) > 0 { + firstname = args[0] + } + if len(args) > 1 { + lastname = args[1] + } + + _, err := c.client.SetName(&client.SetNameRequest{ + FirstName: firstname, + LastName: lastname, + }) + if err != nil { + return errors.Wrap(err, "Couldn't set name").Error() + } + // set About + case "setbio": + _, err := c.client.SetBio(&client.SetBioRequest{ + Bio: strings.Join(args, " "), + }) + if err != nil { + return errors.Wrap(err, "Couldn't set bio").Error() + } + // set password + case "setpassword": + var oldPassword string + var newPassword string + // 0 or 1 argument is ignored and the password is reset + if len(args) > 1 { + oldPassword = args[0] + newPassword = args[1] + } + _, err := c.client.SetPassword(&client.SetPasswordRequest{ + OldPassword: oldPassword, + NewPassword: newPassword, + }) + if err != nil { + return errors.Wrap(err, "Couldn't set password").Error() + } case "help": return helpString(helpTypeTransport) }