CyCoreSystems / ari

Golang Asterisk REST Interface (ARI) library
Apache License 2.0
189 stars 73 forks source link

Cannot set channel variables after create #171

Open gordongrech opened 8 months ago

gordongrech commented 8 months ago

Hello,

I am originating a call to a channel. I need to set some SIP headers and to achieve this I am using channel create not originate, followed by the setting of the variables and dial. I am occasionally getting "Non-2XX response: 409 Conflict" errors when setting the variables.

The issue seems to go away if I wait for a bit after creating the channel (enters stasis?), however I would like to avoid blindly blocking execution. Could you kindly let me know if this is the correct approach or if there is there a better way to achieve this please?

func createChannel(client ari.Client, channel *ari.ChannelHandle, uri, callID, endpointID, callerNumber string) error {
    ch, err := client.Channel().Create(channel.Key(), ari.ChannelCreateRequest{
        Endpoint: uri,
        App:      "test",
    })
    if err != nil {
        return err
    }
    time.Sleep(time.Millisecond * 10)

    headers := map[string]string{
        "PJSIP_HEADER(add,X-Call-ID)":     callID,
        "PJSIP_HEADER(add,X-Endpoint-ID)": endpointID,
        "CONNECTEDLINE(num)":              callerNumber,
    }
    for key, value := range headers {
        if err = ch.SetVariable(key, value); err != nil {
            return err
        }
    }
    return channel.Dial(channel.ID(), 10)
}
ronlockard commented 5 months ago

I have had good luck using StageOriginate() and Exec() instead after running into a similar issue before. You can pass the channel variables in as part of the StageOriginate() call rather than set them individually after the create. It doesn't dial until you call Exec() on the channel.