Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions acceleration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,18 @@ Only Twilio and Telnyx also `attach`, which is the inbound direction: pointing a
trunk so calling it reaches an agent. For the other five that is a per-vendor application
rather than a property of the number, and it is declared missing rather than half-done.

A Twilio number attached before the router answered its calls itself still has its old voice URL.
`router phone move-twilio` points those numbers at `/v1/phone/hooks/twilio`, as an attach does now;
attaching such a number again does the same for that one number. Run it from the image the router
runs (the same tag), with the router's own config: it opens the database as the router does, which
migrates it unless a migrate DSN is set, so a newer image would migrate the shared database. Run
it first with no flag, which only lists each number and what it would do; then with `--apply
--one <e164>`, and call that number; then with `--apply` for the rest (`--app <id>` keeps one Stream
app's). A number already on the webhook is left alone, and so is one whose trunk credentials were
not kept (`no stored bridge`): it needs a detach and an attach. It refuses before it asks Twilio
anything when the webhook token, an https `ROUTER_PUBLIC_URL`, the Twilio account or the keyring
is missing.

A customer who already has numbers and a SIP trunk at their own carrier can call out from
them without buying anything here. `POST /v1/phone/trunks` stores the trunk (host, port,
transport, username, codecs, and whether it accepts an INVITE without SDP); the password is
Expand Down
3 changes: 3 additions & 0 deletions acceleration/cmd/router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ const usage = `usage: router [--config path] [command]
stream-apps look after the Stream apps customers registered in app mode
conversations look after the conversations kept in Stream Chat
prices read and check the price list (router prices for its commands)
phone move-twilio point Twilio numbers attached earlier at the router's call webhook (--apply, --one, --app)
opt-outs normalize rewrite opt-outs stored before E.164 was enforced into E.164
usage replay write the usage rows a log says were lost, once each (--from path)
usage reship send this shard's shipped or refused rows to the usage service again (--since, --rejected)
Expand Down Expand Up @@ -293,6 +294,8 @@ func dispatchCommand(command string, args []string, settings config.Config, logg
return runOptOuts(args, settings, logger)
case "usage":
return runUsage(args, settings, os.Stdout)
case "phone":
return runPhone(args, settings, logger)
case "help", "-h", "--help":
fmt.Print(usage)
return nil
Expand Down
117 changes: 117 additions & 0 deletions acceleration/cmd/router/phonemove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"strconv"

"github.com/GetStream/Vision-Agents/acceleration/internal/config"
"github.com/GetStream/Vision-Agents/acceleration/internal/phone"
"github.com/GetStream/Vision-Agents/acceleration/internal/phone/vendors"
"github.com/GetStream/Vision-Agents/acceleration/internal/store"
"github.com/GetStream/Vision-Agents/acceleration/internal/streamapp"
)

const phoneUsage = `usage: router phone <command> [flags]

move-twilio point the Twilio numbers attached before the router answered their calls itself at
its call webhook (ROUTER_PUBLIC_URL + /v1/phone/hooks/twilio), as attaching one does
now. Says what it would do, a line a number, unless --apply. A number already there
is left alone, and so is one whose trunk credentials were not kept, which needs a
re-attach. Refuses before asking Twilio anything without TWILIO_WEBHOOK_AUTH_TOKEN,
an https ROUTER_PUBLIC_URL, the Twilio account or the keyring.
--apply point the numbers at the webhook
--one only this number, in E.164: move one first and call it
--app only the numbers made in this Stream app
`

// runPhone looks after the numbers the shared database holds.
func runPhone(args []string, settings config.Config, logger *slog.Logger) error {
if len(args) == 0 {
return errors.New(phoneUsage)
}
ctx := context.Background()
switch args[0] {
case "move-twilio":
service, pgStore, err := numberMover(ctx, settings, logger)
if err != nil {
return err
}
defer pgStore.Close()
return moveTwilioNumbers(ctx, args[1:], service, os.Stdout)
}
return fmt.Errorf("unknown phone command %q\n\n%s", args[0], phoneUsage)
}

// numberMover is the phone service with what moving a number needs: the database, the keyring
// the webhook opens trunk passwords with, the vendors and the public URL.
// The store is the caller's to close.
func numberMover(ctx context.Context, settings config.Config, logger *slog.Logger) (
*phone.Service, *store.Store, error,
) {
vendorConfig, err := phone.LoadConfig(settings.PhoneConfig)
if err != nil {
return nil, nil, err
}
secrets, err := newSecretSealer(ctx, settings)
if err != nil {
return nil, nil, err
}
pgStore, err := openStore(ctx, settings)
if err != nil {
return nil, nil, err
}
service, err := phone.NewService(phone.ServiceOptions{
Registry: vendors.Registry(vendorConfig), Store: pgStore, Sealer: secrets,
PublicURL: settings.PublicURL, Logger: logger,
})
if err != nil {
pgStore.Close()
return nil, nil, err
}
return service, pgStore, nil
}

// moveTwilioNumbers runs router phone move-twilio on service, writing a line a number to out.
// A line names the number and its app only.
func moveTwilioNumbers(ctx context.Context, args []string, service *phone.Service, out io.Writer) error {
flags := flag.NewFlagSet("phone move-twilio", flag.ContinueOnError)
apply := flags.Bool("apply", false, "point the numbers at the webhook")
one := flags.String("one", "", "only this number, in E.164")
app := flags.String("app", "", "only the numbers made in this Stream app")
if err := flags.Parse(args); err != nil {
return err
}
move := phone.CallHookMove{Vendor: "twilio", E164: *one, Apply: *apply}
if *one != "" && !store.IsE164(*one) {
return fmt.Errorf("--one is a number in E.164, such as +15125551234, not %q", *one)
}
if *app != "" {
pin, ok := streamapp.ParseAppID(*app)
if !ok {
return fmt.Errorf("--app is a Stream app id, not %q", *app)
}
move.App = &pin
}

counted := map[phone.Move]int{}
err := service.MoveToCallHook(ctx, move, func(number phone.MovedNumber) {
counted[number.Move]++
app := "deployment"
if number.App != 0 {
app = strconv.FormatInt(number.App, 10)
}
fmt.Fprintf(out, "%s\tapp %s\t%s\n", number.E164, app, number.Move)
})
fmt.Fprintf(out, "%d moved, %d to move, %d already moved, %d with no stored bridge\n",
counted[phone.Moved], counted[phone.WouldMove], counted[phone.AlreadyMoved], counted[phone.NoStoredBridge])
if !*apply {
fmt.Fprintln(out, "nothing was written at Twilio: --apply points the numbers at the webhook")
}
return err
}
Loading
Loading