Add AdvertiseCommands and ClearCommands
This commit is contained in:
72
chat.go
72
chat.go
@ -750,40 +750,56 @@ func (c Chat) Mark(messageID int) (ChatAPI, error) {
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(cmdOut, &r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Error != nil {
|
||||||
|
return fmt.Errorf("%v", r.Error.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ClearCommands clears bot advertisements
|
// ClearCommands clears bot advertisements
|
||||||
func (k *Keybase) ClearCommands() (ChatAPI, error) {
|
func (k *Keybase) ClearCommands() error {
|
||||||
m := ChatAPI{}
|
type res struct {
|
||||||
m.Method = "clearcommands"
|
Error *Error `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
r, err := chatAPIOut(k, m)
|
var r res
|
||||||
|
|
||||||
|
cmdOut, err := k.Exec("chat", "api", "-m", `{"method": "clearcommands"}`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, err
|
return err
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdvertiseCommands sets up bot command advertisements
|
err = json.Unmarshal(cmdOut, &r)
|
||||||
// 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{},
|
|
||||||
}
|
|
||||||
m.Method = "advertisecommands"
|
|
||||||
m.Params.Options.BotAdvertisements = advertisements
|
|
||||||
|
|
||||||
r, err := chatAPIOut(k, m)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, err
|
return err
|
||||||
}
|
|
||||||
return r, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdvertiseCommand sets up bot command advertisements
|
if r.Error != nil {
|
||||||
// This method allows you to set up one type of advertisement.
|
return fmt.Errorf("%v", r.Error.Message)
|
||||||
// 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{
|
return nil
|
||||||
advertisement,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
29
docs_test.go
29
docs_test.go
@ -1,20 +1,35 @@
|
|||||||
package keybase
|
package keybase
|
||||||
|
|
||||||
func ExampleKeybase_AdvertiseCommand() {
|
import "samhofi.us/x/keybase/types/chat1"
|
||||||
|
|
||||||
|
func ExampleKeybase_AdvertiseCommands() {
|
||||||
var k = NewKeybase()
|
var k = NewKeybase()
|
||||||
|
|
||||||
// Clear out any previously advertised commands
|
// Clear out any previously advertised commands
|
||||||
k.ClearCommands()
|
k.ClearCommands()
|
||||||
|
|
||||||
// Create BotAdvertisement
|
// Create BotAdvertisement
|
||||||
c := BotAdvertisement{
|
ads := AdvertiseCommandsOptions{
|
||||||
Type: "public",
|
Alias: "RSS Bot",
|
||||||
BotCommands: []BotCommand{
|
Advertisements: []chat1.AdvertiseCommandAPIParam{
|
||||||
NewBotCommand("help", "Get help using this bot", "!help <command>"),
|
{
|
||||||
NewBotCommand("hello", "Say hello", "!hello"),
|
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
|
// Send advertisement
|
||||||
k.AdvertiseCommand(c)
|
k.AdvertiseCommands(ads)
|
||||||
}
|
}
|
||||||
|
|||||||
26
types.go
26
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
|
// KVOptions holds a set of options to be passed to the KV methods
|
||||||
type KVOptions struct {
|
type KVOptions struct {
|
||||||
Team *string `json:"team"`
|
Team *string `json:"team"`
|
||||||
@ -955,8 +979,6 @@ type wallet interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type keybase interface {
|
type keybase interface {
|
||||||
AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error)
|
|
||||||
AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error)
|
|
||||||
ChatList(opts ...chat1.ChatChannel) (ChatAPI, error)
|
ChatList(opts ...chat1.ChatChannel) (ChatAPI, error)
|
||||||
ClearCommands() (ChatAPI, error)
|
ClearCommands() (ChatAPI, error)
|
||||||
CreateTeam(name string) (TeamAPI, error)
|
CreateTeam(name string) (TeamAPI, error)
|
||||||
|
|||||||
Reference in New Issue
Block a user