Last active
November 3, 2016 23:45
-
-
Save markphelps/5981a3515c357730934d5d97044daf9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run starts the run loop for the bot to listen/respond to messages | |
func (b *Bot) Run() { | |
go b.client.ManageConnection() | |
for { | |
select { | |
case msg := <-b.client.IncomingEvents: | |
switch ev := msg.Data.(type) { | |
// other types skipped for brevity | |
case *slack.MessageEvent: | |
userID := ev.User | |
// check to see if conversation already in progress | |
convo, ok := conversations[userID] | |
// if not, start a new one | |
if !ok { | |
convo = &conversation{ | |
user: userID, | |
incomming: make(chan *slack.MessageEvent), | |
} | |
conversations[userID] = convo | |
// each chat runs in its own goroutine until conversation ceases | |
go b.chat(convo) | |
} | |
// forward incomming message to that chat's incomming channel | |
convo.incomming <- ev | |
} | |
} | |
} | |
} | |
func (b *Bot) chat(convo *conversation) { | |
for convo.state = hello; convo.state != nil; { | |
convo.state = convo.state(b, convo) | |
} | |
// conversation is over so close the incomming channel | |
close(convo.incomming) | |
// and delete the conversation from the map | |
delete(conversations[convo.userID]) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment