Skip to content

Instantly share code, notes, and snippets.

@markphelps
Last active November 3, 2016 23:45
Show Gist options
  • Save markphelps/5981a3515c357730934d5d97044daf9b to your computer and use it in GitHub Desktop.
Save markphelps/5981a3515c357730934d5d97044daf9b to your computer and use it in GitHub Desktop.
// 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