- return serveAgent(ctx, listener, func(notify func(Event)) (*loop, error) {
- return newLoop(agent, memory, client, time.Now, notify)
- })
-}
-
-// serveAgent runs the loop that start makes and serves the clients of
-// listener until ctx ends or the loop fails. It closes listener.
-func serveAgent(ctx context.Context, listener net.Listener, start func(notify func(Event)) (*loop, error)) error {
- ctx, cancel := context.WithCancel(ctx)
- defer cancel()
-
- hub := &hub{
- clients: make(map[*hubClient]bool),
- }
-
- l, err := start(hub.Broadcast)
- if err != nil {
- return errors.Join(err, listener.Close())
- }
-
- var workers sync.WaitGroup
- workers.Add(1)
- go func() {
- defer workers.Done()
-
- hub.Accept(ctx, listener, l)
- }()
-
- err = l.Run(ctx)
- cancel()
- err = errors.Join(err, listener.Close())
- hub.CloseAll()
- workers.Wait()
-
- if errors.Is(err, context.Canceled) {
- return nil
+ var telegram *TelegramBot
+ if hasBot {
+ telegram = newTelegramBot(newTelegramAPI(telegramAPIURL, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, telegramOffsetFileName), log.New(out, "telegram: ", log.LstdFlags))
+ _, err = fmt.Fprintln(out, "onechat: the Telegram bot waits for messages of its owner")
+ if err != nil {
+ return errors.Join(err, listener.Close())
+ }