Browse Source

Add AdvertiseCommands and ClearCommands

main
Sam 4 years ago
parent
commit
0316bc6db1
  1. 68
      chat.go
  2. 29
      docs_test.go
  3. 26
      types.go

68
chat.go

@ -750,40 +750,56 @@ func (c Chat) Mark(messageID int) (ChatAPI, error) { @@ -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: &params{},
// 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
}

29
docs_test.go

@ -1,20 +1,35 @@ @@ -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 <command>"),
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: "<url>",
},
{
Name: "rss delfeed",
Description: "Remove RSS feed",
Usage: "<url>",
},
},
},
},
}
// Send advertisement
k.AdvertiseCommand(c)
k.AdvertiseCommands(ads)
}

26
types.go

@ -125,6 +125,30 @@ func newReadMessageArg(options ReadMessageOptions) readMessageArg { @@ -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 { @@ -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)

Loading…
Cancel
Save