// loop returns true if need to run again, otherwise false.
loop returns true if need to run again, otherwise false.
// loop returns true if need to run again otherwise false .
func (b *Bot) loop() bool {
	for b.Connect() != nil {
		log.Infof("couldn't reconnect to %s, trying in 10secs...", b.URL)
		select {
		case <-b.ctx.Done():
			return false
		case <-time.After(time.Second * 10):
		}
	}
	chats, err := b.GetChats()
	if err != nil {
		return true
	}
	b.JoinChats(chats...)
	eventsCh := b.StartListen()
	for {
		select {
		case <-b.ctx.Done():
			return false
		case e, ok := <-eventsCh:
			if !ok {
				return true
			}
			log.Tracef("got event: %v", e)
			switch e := e.(type) {
			case *events.ChatJoinEvent:
			case *events.MessageEvent:
				cmd, err := command.ParseCommandForBot(b.Username, e.Message)
				if err != nil {
					continue
				}
				answer, err := cmd.Answer()
				if err != nil {
					continue
				}
				b.PostMessage(e.ChatName, answer)
			case *events.ChatCreatedEvent:
				go func() { b.JoinChat(e.ChatName) }()
			default:
			}
		}
	}
}
func ( b * Bot ) loop ( ) bool { for b . Connect ( ) != nil { log . Infof ( " " , b . URL ) select { case <- b . ctx . Done ( ) : return false case <- time . After ( time . Second * 10 ) : } } chats , err := b . GetChats ( ) if err != nil { return true } b . JoinChats ( chats ... ) eventsCh := b . StartListen ( ) for { select { case <- b . ctx . Done ( ) : return false case e , ok := <- eventsCh : if ! ok { return true } log . Tracef ( " " , e ) switch e := e . ( type ) { case * events . ChatJoinEvent : case * events . MessageEvent : cmd , err := command . ParseCommandForBot ( b . Username , e . Message ) if err != nil { continue } answer , err := cmd . Answer ( ) if err != nil { continue } b . PostMessage ( e . ChatName , answer ) case * events . ChatCreatedEvent : go func ( ) { b . JoinChat ( e . ChatName ) } ( ) default : } } } }
loop returns true if need to run again, otherwise false.
loop returns true if need to run again otherwise false .
// connecting... // channel has been closed, go reconnect