Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit add9e36

Browse files
ddworkenmmou
andauthored
Add ping command per issue 47 (#50)
Co-authored-by: M Mou <[email protected]>
1 parent 4cef409 commit add9e36

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/keybaseca/bot/bot.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,15 @@ func StartBot(conf config.Config) error {
8585
continue
8686
}
8787

88-
if shared.IsAckRequest(messageBody) {
89-
log.Debug("Responding to AckMessage")
88+
if shared.IsPingRequest(messageBody, kbc.GetUsername()) {
89+
// Respond to messages of the form `ping @botName` with `pong @senderName`
90+
log.Debug("Responding to ping with pong")
91+
_, err = kbc.SendMessageByConvID(msg.Message.ConvID, fmt.Sprintf(shared.GeneratePingResponse(msg.Message.Sender.Username)))
92+
if err != nil {
93+
LogError(conf, kbc, msg, err)
94+
continue
95+
}
96+
} else if shared.IsAckRequest(messageBody) {
9097
// Ack any AckRequests so that kssh can determine whether it has fully connected
9198
_, err = kbc.SendMessageByConvID(msg.Message.ConvID, shared.GenerateAckResponse(messageBody))
9299
if err != nil {

src/shared/chat_types.go

+20
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,23 @@ func IsAckRequest(msg string) bool {
8080
func IsAckResponse(msg string) bool {
8181
return strings.HasPrefix(msg, "Ack--")
8282
}
83+
84+
// Generate a ping request message
85+
func GeneratePingRequest(botname string) string {
86+
return fmt.Sprintf("ping @%s", botname)
87+
}
88+
89+
// Returns whether the given message is a ping request
90+
func IsPingRequest(msg, botUsername string) bool {
91+
return strings.TrimSpace(msg) == fmt.Sprintf("ping @%s", botUsername)
92+
}
93+
94+
// Generate a ping response message
95+
func GeneratePingResponse(botname string) string {
96+
return fmt.Sprintf("pong @%s", botname)
97+
}
98+
99+
// Returns whether the given message is a ping response
100+
func IsPingResponse(msg, localUsername string) bool {
101+
return strings.TrimSpace(msg) == fmt.Sprintf("pong @%s", localUsername)
102+
}

0 commit comments

Comments
 (0)