Skip to content

Commit 51537e9

Browse files
Adding support in livekit-cli for dynamic dispatch rules
PR for dynamic dispatch rules: livekit/protocol#1123 go.mod changes will be made once that PR is approved.
1 parent b01c31b commit 51537e9

File tree

1 file changed

+133
-44
lines changed

1 file changed

+133
-44
lines changed

cmd/lk/sip.go

Lines changed: 133 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -227,57 +227,19 @@ var (
227227
Usage: "Create a SIP Dispatch Rule",
228228
Action: createSIPDispatchRule,
229229
ArgsUsage: RequestDesc[livekit.CreateSIPDispatchRuleRequest](),
230-
Flags: []cli.Flag{
231-
&cli.StringFlag{
232-
Name: "name",
233-
Usage: "Sets a new name for the dispatch rule",
234-
},
235-
&cli.StringSliceFlag{
236-
Name: "trunks",
237-
Usage: "Sets a list of trunks for the dispatch rule",
238-
},
239-
&cli.StringFlag{
240-
Name: "direct",
241-
Usage: "Sets a direct dispatch to a specified room",
242-
},
243-
&cli.StringFlag{
244-
Name: "caller",
245-
Aliases: []string{"individual"},
246-
Usage: "Sets a individual caller dispatch to a new room with a specific prefix",
247-
},
248-
&cli.StringFlag{
249-
Name: "callee",
250-
Usage: "Sets a callee number dispatch to a new room with a specific prefix",
251-
},
252-
&cli.BoolFlag{
253-
Name: "pin",
254-
Usage: "PIN for a dispatch rule",
255-
},
256-
&cli.BoolFlag{
257-
Name: "randomize",
258-
Usage: "Randomize room name, only applies to callee dispatch",
259-
},
260-
},
230+
Flags: sipDispatchRuleBaseFlags,
261231
},
262232
{
263233
Name: "update",
264234
Usage: "Update a SIP Dispatch Rule",
265235
Action: updateSIPDispatchRule,
266236
ArgsUsage: RequestDesc[livekit.UpdateSIPDispatchRuleRequest](),
267-
Flags: []cli.Flag{
237+
Flags: append([]cli.Flag{
268238
&cli.StringFlag{
269239
Name: "id",
270240
Usage: "ID for the rule to update",
271241
},
272-
&cli.StringFlag{
273-
Name: "name",
274-
Usage: "Sets a new name for the rule",
275-
},
276-
&cli.StringSliceFlag{
277-
Name: "trunks",
278-
Usage: "Sets a new list of trunk IDs",
279-
},
280-
},
242+
}, sipDispatchRuleBaseFlags...),
281243
},
282244
{
283245
Name: "delete",
@@ -413,6 +375,47 @@ var (
413375
},
414376
},
415377
}
378+
379+
// Define a shared base flag list for SIP Dispatch Rule create/update
380+
sipDispatchRuleBaseFlags = []cli.Flag{
381+
&cli.StringFlag{
382+
Name: "name",
383+
Usage: "Sets a name for the dispatch rule",
384+
},
385+
&cli.StringSliceFlag{
386+
Name: "trunks",
387+
Usage: "Sets a list of trunks for the dispatch rule",
388+
},
389+
&cli.StringFlag{
390+
Name: "direct",
391+
Usage: "Sets a direct dispatch to a specified room",
392+
},
393+
&cli.StringFlag{
394+
Name: "caller",
395+
Aliases: []string{"individual"},
396+
Usage: "Sets an individual caller dispatch to a new room with a specific prefix",
397+
},
398+
&cli.StringFlag{
399+
Name: "callee",
400+
Usage: "Sets a callee number dispatch to a new room with a specific prefix",
401+
},
402+
&cli.BoolFlag{
403+
Name: "pin",
404+
Usage: "PIN for a dispatch rule",
405+
},
406+
&cli.BoolFlag{
407+
Name: "randomize",
408+
Usage: "Randomize room name, only applies to callee dispatch",
409+
},
410+
&cli.StringFlag{
411+
Name: "dispatch-url",
412+
Usage: "Sets a dynamic dispatch rule with webhook URL (uses POST method)",
413+
},
414+
&cli.StringFlag{
415+
Name: "method",
416+
Usage: "Sets the HTTP method for dynamic dispatch rule (default: POST)",
417+
},
418+
}
416419
)
417420

418421
func listUpdateFlag(cmd *cli.Command, setName string) *livekit.ListUpdate {
@@ -895,6 +898,23 @@ func createSIPDispatchRule(ctx context.Context, cmd *cli.Command) error {
895898
},
896899
}
897900
}
901+
if val := cmd.String("dispatch-url"); val != "" {
902+
if p.Rule != nil {
903+
return fmt.Errorf("only one dispatch rule type is allowed")
904+
}
905+
method := cmd.String("method")
906+
if method == "" {
907+
method = "POST"
908+
}
909+
p.Rule = &livekit.SIPDispatchRule{
910+
Rule: &livekit.SIPDispatchRule_DispatchRuleDynamic{
911+
DispatchRuleDynamic: &livekit.SIPDispatchRuleDynamic{
912+
Url: val,
913+
Method: method,
914+
},
915+
},
916+
}
917+
}
898918
return nil
899919
}, cli.CreateSIPDispatchRule, printSIPDispatchRuleID)
900920
}
@@ -945,6 +965,71 @@ func updateSIPDispatchRule(ctx context.Context, cmd *cli.Command) error {
945965
if id == "" {
946966
return errors.New("no ID specified")
947967
}
968+
969+
// Check if any dispatch rule flags are set
970+
hasDispatchRuleFlags := cmd.IsSet("direct") || cmd.IsSet("caller") || cmd.IsSet("callee") || cmd.IsSet("dispatch-url")
971+
972+
if hasDispatchRuleFlags {
973+
// Create a new dispatch rule from flags (similar to create function)
974+
rule := &livekit.SIPDispatchRule{}
975+
976+
if val := cmd.String("direct"); val != "" {
977+
rule.Rule = &livekit.SIPDispatchRule_DispatchRuleDirect{
978+
DispatchRuleDirect: &livekit.SIPDispatchRuleDirect{
979+
RoomName: val,
980+
Pin: cmd.String("pin"),
981+
},
982+
}
983+
} else if val := cmd.String("caller"); val != "" {
984+
rule.Rule = &livekit.SIPDispatchRule_DispatchRuleIndividual{
985+
DispatchRuleIndividual: &livekit.SIPDispatchRuleIndividual{
986+
RoomPrefix: val,
987+
Pin: cmd.String("pin"),
988+
},
989+
}
990+
} else if val := cmd.String("callee"); val != "" {
991+
rule.Rule = &livekit.SIPDispatchRule_DispatchRuleCallee{
992+
DispatchRuleCallee: &livekit.SIPDispatchRuleCallee{
993+
RoomPrefix: val,
994+
Randomize: cmd.Bool("randomize"),
995+
Pin: cmd.String("pin"),
996+
},
997+
}
998+
} else if val := cmd.String("dispatch-url"); val != "" {
999+
method := cmd.String("method")
1000+
if method == "" {
1001+
method = "POST"
1002+
}
1003+
rule.Rule = &livekit.SIPDispatchRule_DispatchRuleDynamic{
1004+
DispatchRuleDynamic: &livekit.SIPDispatchRuleDynamic{
1005+
Url: val,
1006+
Method: method,
1007+
},
1008+
}
1009+
}
1010+
1011+
// Use Update action with the rule field populated
1012+
req := &livekit.SIPDispatchRuleUpdate{
1013+
Rule: rule,
1014+
}
1015+
if val := cmd.String("name"); val != "" {
1016+
req.Name = &val
1017+
}
1018+
req.TrunkIds = listUpdateFlag(cmd, "trunks")
1019+
1020+
info, err := cli.UpdateSIPDispatchRule(ctx, &livekit.UpdateSIPDispatchRuleRequest{
1021+
SipDispatchRuleId: id,
1022+
Action: &livekit.UpdateSIPDispatchRuleRequest_Update{
1023+
Update: req,
1024+
},
1025+
})
1026+
if err != nil {
1027+
return err
1028+
}
1029+
printSIPDispatchRuleID(info)
1030+
return err
1031+
}
1032+
9481033
req := &livekit.SIPDispatchRuleUpdate{}
9491034
if val := cmd.String("name"); val != "" {
9501035
req.Name = &val
@@ -969,10 +1054,10 @@ func listSipDispatchRule(ctx context.Context, cmd *cli.Command) error {
9691054
return err
9701055
}
9711056
return listAndPrint(ctx, cmd, cli.ListSIPDispatchRule, &livekit.ListSIPDispatchRuleRequest{}, []string{
972-
"SipDispatchRuleID", "Name", "SipTrunks", "Type", "RoomName", "Pin",
1057+
"SipDispatchRuleID", "Name", "SipTrunks", "Type", "RoomName", "Pin", "DispatchURL",
9731058
"Attributes", "Agents",
9741059
}, func(item *livekit.SIPDispatchRuleInfo) []string {
975-
var room, typ, pin string
1060+
var room, typ, pin, dispatchUrl string
9761061
switch r := item.GetRule().GetRule().(type) {
9771062
case *livekit.SIPDispatchRule_DispatchRuleDirect:
9781063
room = r.DispatchRuleDirect.RoomName
@@ -989,6 +1074,10 @@ func listSipDispatchRule(ctx context.Context, cmd *cli.Command) error {
9891074
}
9901075
pin = r.DispatchRuleCallee.Pin
9911076
typ = "Callee"
1077+
case *livekit.SIPDispatchRule_DispatchRuleDynamic:
1078+
room = ""
1079+
dispatchUrl = r.DispatchRuleDynamic.Url
1080+
typ = "Dynamic"
9921081
}
9931082
trunks := strings.Join(item.TrunkIds, ",")
9941083
if trunks == "" {
@@ -1001,7 +1090,7 @@ func listSipDispatchRule(ctx context.Context, cmd *cli.Command) error {
10011090
}
10021091
}
10031092
return []string{
1004-
item.SipDispatchRuleId, item.Name, trunks, typ, room, pin,
1093+
item.SipDispatchRuleId, item.Name, trunks, typ, room, pin, dispatchUrl,
10051094
fmt.Sprintf("%v", item.Attributes), strings.Join(agents, ","),
10061095
}
10071096
})

0 commit comments

Comments
 (0)