Skip to content

Commit 2618765

Browse files
committed
fixes
1 parent 63d65ba commit 2618765

5 files changed

Lines changed: 45 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ The bot uses a single duplex WebSocket to both publish and subscribe to audio.
2222
## Quick start
2323

2424
```bash
25-
cp config.yaml config.yaml # edit token, api_base, ingress_url
25+
cp config.json config.json # edit token, api_base, ingress_url
2626
export BOT_TOKEN="your-bot-token"
2727
go run .
2828
```
2929

3030
## Configuration
3131

32-
Edit `config.yaml` or use environment variables:
32+
Edit `config.json` or use environment variables:
3333

3434
| Config key | Env var | Default | Description |
3535
|---|---|---|---|

bot.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"net/http"
1313
"net/url"
1414
"strings"
15+
"sync"
16+
"sync/atomic"
1517
"time"
1618
)
1719

@@ -54,10 +56,12 @@ type CallEndedEvent struct {
5456
}
5557

5658
type Bot struct {
57-
cfg Config
58-
client *http.Client
59-
variants []EchoVariant
60-
bgPackets []OpusPacket
59+
cfg Config
60+
client *http.Client
61+
variants []EchoVariant
62+
bgPackets []OpusPacket
63+
activeCalls sync.Map // callID → context.CancelFunc
64+
callCount atomic.Int64 // number of active call handlers
6165
}
6266

6367
func NewBot(cfg Config) *Bot {
@@ -227,14 +231,24 @@ func (b *Bot) handleEvent(ctx context.Context, eventType, data string) {
227231
return
228232
}
229233
slog.Info("incoming call", "callId", ev.CallID, "from", ev.FromUserID)
230-
go b.handleCall(ctx, ev)
234+
235+
callCtx, callCancel := context.WithCancel(ctx)
236+
b.activeCalls.Store(ev.CallID, callCancel)
237+
go func() {
238+
defer callCancel()
239+
defer b.activeCalls.Delete(ev.CallID)
240+
b.handleCall(callCtx, ev)
241+
}()
231242

232243
case "callEnded":
233244
var ev CallEndedEvent
234245
if err := json.Unmarshal([]byte(data), &ev); err != nil {
235246
slog.Error("failed to parse callEnded", "error", err)
236247
return
237248
}
249+
if cancel, ok := b.activeCalls.LoadAndDelete(ev.CallID); ok {
250+
cancel.(context.CancelFunc)()
251+
}
238252
slog.Info("call ended", "callId", ev.CallID)
239253

240254
default:
@@ -243,7 +257,16 @@ func (b *Bot) handleEvent(ctx context.Context, eventType, data string) {
243257
}
244258

245259
func (b *Bot) handleCall(ctx context.Context, ev CallIncomingEvent) {
246-
callLog := slog.With("callId", ev.CallID, "from", ev.FromUserID)
260+
n := b.callCount.Add(1)
261+
defer b.callCount.Add(-1)
262+
263+
defer func() {
264+
if r := recover(); r != nil {
265+
slog.Error("panic in handleCall", "callId", ev.CallID, "panic", r)
266+
}
267+
}()
268+
269+
callLog := slog.With("callId", ev.CallID, "from", ev.FromUserID, "activeCalls", n)
247270

248271
v := b.variants[rand.Intn(len(b.variants))]
249272
callLog.Info("selected variant", "variant", v.Name)

config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"api_base": "https://gateway.argon.zone",
3+
"background_file": "background.opus",
4+
"ingress_url": "ws://rts.argon.gl:12880",
5+
"log_level": "debug",
6+
"token": "",
7+
"variants": [
8+
"girl_echo_00",
9+
"girl_echo_01",
10+
"girl_echo_02",
11+
"girl_echo_03"
12+
]
13+
}

config.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func loadConfig(path string) Config {
158158
}
159159

160160
func main() {
161-
cfg := loadConfig(env("CONFIG_FILE", "config.yaml"))
161+
cfg := loadConfig(env("CONFIG_FILE", "config.json"))
162162

163163
// env vars take priority
164164
if v := os.Getenv("BOT_TOKEN"); v != "" {

0 commit comments

Comments
 (0)