diff --git a/chat.go b/chat.go index 5fcb11b..303a591 100644 --- a/chat.go +++ b/chat.go @@ -750,40 +750,56 @@ func (c Chat) Mark(messageID int) (ChatAPI, error) { return r, nil } -// ClearCommands clears bot advertisements -func (k *Keybase) ClearCommands() (ChatAPI, error) { - m := ChatAPI{} - m.Method = "clearcommands" +// AdvertiseCommands sends bot command advertisements +func (k *Keybase) AdvertiseCommands(options AdvertiseCommandsOptions) error { + type res struct { + Error *Error `json:"error,omitempty"` + } + + var r res + + arg := newAdvertiseCommandsArg(options) + + jsonBytes, _ := json.Marshal(arg) + + cmdOut, err := k.Exec("chat", "api", "-m", string(jsonBytes)) + if err != nil { + return err + } - r, err := chatAPIOut(k, m) + err = json.Unmarshal(cmdOut, &r) if err != nil { - return r, err + return err } - return r, nil + + if r.Error != nil { + return fmt.Errorf("%v", r.Error.Message) + } + + return nil } -// AdvertiseCommands sets up bot command advertisements -// This method allows you to set up multiple different types of advertisements at once. -// Use this method if you have commands whose visibility differs from each other. -func (k *Keybase) AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error) { - m := ChatAPI{ - Params: ¶ms{}, +// ClearCommands clears bot advertisements +func (k *Keybase) ClearCommands() error { + type res struct { + Error *Error `json:"error,omitempty"` } - m.Method = "advertisecommands" - m.Params.Options.BotAdvertisements = advertisements - r, err := chatAPIOut(k, m) + var r res + + cmdOut, err := k.Exec("chat", "api", "-m", `{"method": "clearcommands"}`) if err != nil { - return r, err + return err + } + + err = json.Unmarshal(cmdOut, &r) + if err != nil { + return err + } + + if r.Error != nil { + return fmt.Errorf("%v", r.Error.Message) } - return r, nil -} -// AdvertiseCommand sets up bot command advertisements -// This method allows you to set up one type of advertisement. -// Use this method if you have commands whose visibility should all be the same. -func (k *Keybase) AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error) { - return k.AdvertiseCommands([]BotAdvertisement{ - advertisement, - }) + return nil } diff --git a/docs_test.go b/docs_test.go index e92ece6..fc08dd7 100644 --- a/docs_test.go +++ b/docs_test.go @@ -1,20 +1,35 @@ package keybase -func ExampleKeybase_AdvertiseCommand() { +import "samhofi.us/x/keybase/types/chat1" + +func ExampleKeybase_AdvertiseCommands() { var k = NewKeybase() // Clear out any previously advertised commands k.ClearCommands() // Create BotAdvertisement - c := BotAdvertisement{ - Type: "public", - BotCommands: []BotCommand{ - NewBotCommand("help", "Get help using this bot", "!help "), - NewBotCommand("hello", "Say hello", "!hello"), + ads := AdvertiseCommandsOptions{ + Alias: "RSS Bot", + Advertisements: []chat1.AdvertiseCommandAPIParam{ + { + Typ: "public", + Commands: []chat1.UserBotCommandInput{ + { + Name: "rss addfeed", + Description: "Add RSS feed", + Usage: "", + }, + { + Name: "rss delfeed", + Description: "Remove RSS feed", + Usage: "", + }, + }, + }, }, } // Send advertisement - k.AdvertiseCommand(c) + k.AdvertiseCommands(ads) } diff --git a/types.go b/types.go index 1dafd9b..8aee86d 100644 --- a/types.go +++ b/types.go @@ -125,6 +125,30 @@ func newReadMessageArg(options ReadMessageOptions) readMessageArg { } } +// AdvertiseCommandsOptions holds a set of options to be passed to AdvertiseCommands +type AdvertiseCommandsOptions struct { + Alias string + Advertisements []chat1.AdvertiseCommandAPIParam +} + +type advertiseCommandsParams struct { + Options AdvertiseCommandsOptions +} + +type advertiseCommandsArg struct { + Method string + Params advertiseCommandsParams +} + +func newAdvertiseCommandsArg(options AdvertiseCommandsOptions) advertiseCommandsArg { + return advertiseCommandsArg{ + Method: "advertisecommands", + Params: advertiseCommandsParams{ + Options: options, + }, + } +} + // KVOptions holds a set of options to be passed to the KV methods type KVOptions struct { Team *string `json:"team"` @@ -955,8 +979,6 @@ type wallet interface { } type keybase interface { - AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error) - AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error) ChatList(opts ...chat1.ChatChannel) (ChatAPI, error) ClearCommands() (ChatAPI, error) CreateTeam(name string) (TeamAPI, error)