Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add conversations example #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

thinkingserious
Copy link
Contributor

@thinkingserious thinkingserious commented Jul 14, 2021

Checklist

  • I acknowledge that all my contributions will be made under the project's license

When running the terraform apply you will receive the following error; however, the error is not correct. Meaning, the demo works as expected.

Error: Status: 400 - ApiError 50441: Failed to add a proxy address to a participant (null) More info: https://www.twilio.com/docs/errors/50441

@thinkingserious
Copy link
Contributor Author

thinkingserious commented Jul 15, 2021

I used the following code to try and reproduce the error message we see in terraform, but the following produces no error:

package main

import (
	"encoding/json"
	"fmt"
	"os"

	twilio "github.com/twilio/twilio-go"
	conversations "github.com/twilio/twilio-go/rest/conversations/v1"
)

func main() {
	accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
	authToken := os.Getenv("TWILIO_AUTH_TOKEN")
	client := twilio.NewRestClient(accountSid, authToken)
	conversation_params := &conversations.CreateConversationParams{}
	conversation_params.SetFriendlyName("Conversation 00")
	resp0, err := client.ConversationsV1.CreateConversation(conversation_params)
	if err != nil {
		fmt.Println(err.Error())
		err = nil
	} else {
		response, _ := json.Marshal(*resp0)
		fmt.Println("Response: " + string(response))
	}
	participant_00_params := &conversations.CreateConversationParticipantParams{}
	participant_00_params.SetMessagingBindingAddress(<User 00 Personal Mobile Number>)
	participant_00_params.SetMessagingBindingProxyAddress(<Your purchased Twilio Phone Number>)
	resp1, err := client.ConversationsV1.CreateConversationParticipant(*resp0.Sid, participant_00_params)
	if err != nil {
		fmt.Println(err.Error())
		err = nil
	} else {
		response, _ := json.Marshal(*resp1)
		fmt.Println("Response: " + string(response))
	}
	participant_01_params := &conversations.CreateConversationParticipantParams{}
	participant_01_params.SetMessagingBindingAddress(<User 01 Personal Mobile Number>)
	participant_01_params.SetMessagingBindingProxyAddress(<Your purchased Twilio Phone Number>)
	resp2, err := client.ConversationsV1.CreateConversationParticipant(*resp0.Sid, participant_01_params)
	if err != nil {
		fmt.Println(err.Error())
		err = nil
	} else {
		response, _ := json.Marshal(*resp2)
		fmt.Println("Response: " + string(response))
	}
}

Copy link
Contributor

@JenniferMah JenniferMah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm able to produce the same error using your example code. Did the conversations team say what a 50441 error means?

@thinkingserious
Copy link
Contributor Author

No, I was asked to try and reproduce with cURL. I did that and also with twilio-go with no error. But in all those cases, including with terraform, the system ultimately gets setup correctly. 😕

@thinkingserious
Copy link
Contributor Author

Circling back with the conversations team for additional guidance.

Copy link
Contributor

@shwetha-manvinkurke shwetha-manvinkurke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the same error but I also see that the channel and the participants in fact do get created. Probably a bug in our tf provider.

@RJPearson94
Copy link

RJPearson94 commented Jul 19, 2021

hi 👋

I was looking through the provider and the team has done a really good job on the provider.
I noticed this PR and I had a quick look at the issue you described and I think I may have identified the root cause of the issue.

After you add/ create a participant to a conversation you immediately call the updateConversationsParticipants function as seen here. The update participant API call is returning the error Status: 400 - ApiError 50441: Failed to add a proxy address to a participant (null) More info: https://www.twilio.com/docs/errors/50441 and not the original create participant call hence you can see the participant is correctly configured in the Twilio console.

I'm not 100% clear on why the update participant call is returning this error but if you don't call the updateConversationsParticipants function and replace line 328 with the following code

  err = MarshalSchema(d, r)
  if err != nil {
    return diag.FromErr(err)
  }

  return nil

then no error is thrown and the provider continues executing successfully. Although, if you modify the messaging_binding_proxy_address argument to use another Twilio phone number, then you do see the error you specified, so I will continue digging to see what causes this error to occur.

This is also reproducible using the Go SDK with the following code. The code below has been slightly adapted from the example you shared. Now it only configures 1 participant and uses the updated NewRestClient function in the main branch of the Go SDK repo

package main

import (
  "encoding/json"
  "fmt"

  twilio "github.com/twilio/twilio-go"
  conversations "github.com/twilio/twilio-go/rest/conversations/v1"
)

func main() {
  client := twilio.NewRestClient()
  conversation_params := &conversations.CreateConversationParams{}
  conversation_params.SetFriendlyName("Conversation 00")
  resp0, err := client.ConversationsV1.CreateConversation(conversation_params)
  if err != nil {
    fmt.Println(err.Error())
    err = nil
  } else {
    response, _ := json.Marshal(*resp0)
    fmt.Println("Response: " + string(response))
  }
  participant_00_params := &conversations.CreateConversationParticipantParams{}
  participant_00_params.SetMessagingBindingAddress(<User 00 Personal Mobile Number>)
  participant_00_params.SetMessagingBindingProxyAddress(<Your purchased Twilio Phone Number>)
  resp1, err := client.ConversationsV1.CreateConversationParticipant(*resp0.Sid, participant_00_params)
  if err != nil {
    fmt.Println(err.Error())
    err = nil
  } else {
    response, _ := json.Marshal(*resp1)
    fmt.Println("Response: " + string(response))
  }

  update_participant_00_params := &conversations.UpdateConversationParticipantParams{}
  // the message binding address cannot be set on an update
  update_participant_00_params.SetMessagingBindingProxyAddress(<Your purchased Twilio Phone Number>)
  updateResp1, updateErr := client.ConversationsV1.UpdateConversationParticipant(*resp0.Sid, *resp1.Sid, update_participant_00_params)
  if updateErr != nil {
    fmt.Println("Update Error Response: " + updateErr.Error())
    updateErr = nil
  } else {
    response, _ := json.Marshal(*updateResp1)
    fmt.Println("Update Response: " + string(response))
  }

}

Hope this helps

@thinkingserious
Copy link
Contributor Author

Thank you for taking a look and providing awesome feedback @RJPearson94!

With respect to "Although, if you modify the messaging_binding_proxy_address argument to use another Twilio phone number" ... I'm not quite sure what you mean. Are you saying that after you successfully setup a conversation, setting up a second one fails? Can you please walk me through that scenario so I can try to also reproduce? Thank you!!

@RJPearson94
Copy link

RJPearson94 commented Jul 20, 2021

Hi @thinkingserious.

I have included the steps to reproduce this issue below. Apologies, I should have included it in my original message.

When I said, "Although, if you modify the messaging_binding_proxy_address argument to use another Twilio phone number" I meant that once you have provisioned your conversation and 1 or more participants using Terraform. If you make a change to one of your twilio_conversations_conversations_participants_v1 resources that Terraform detects as a difference, between your configuration and what is held in the state i.e. changing the messaging_binding_proxy_address value to be a different Twilio phone number. When the changes are applied and the participant is updated then an error will be thrown.

Steps to Reproduce

These steps assume that there is no pre-existing Terraform state and resources deployed.

  1. Modify the provider by replacing the updateConversationsParticipants function call on line 328 with Figure 1
  2. Build the provider locally
  3. Copy the Terraform code (Figure 2) and substitute in a personal mobile number and a Twilio Phone Number
  4. Run terraform init then terraform apply and approve the changes. You will need to wait for all the changes to apply and the outputs to be displayed
  5. Modify the copied Terraform code by updating the messaging_binding_proxy_address argument to use another Twilio phone number
  6. Run terraform apply and you should see Terraform is showing you there is 1 change to apply (similar to Figure 3)
  7. Accept the changes and an error should be thrown

Note: this is not limited to just modifying the messaging_binding_proxy_address argument, this error occurs by any change which causes Terraform to detect a change on the resource and attempt to update the participant.
I also get a similar error when I try to modify an existing participant via the API too

Figure 1

err = MarshalSchema(d, r)
  if err != nil {
    return diag.FromErr(err)
  }

  return nil

Figure 2

terraform {
  required_providers {
    twilio = {
      source  = "local/twilio/twilio"
      version = ">=0.4.0"
    }
  }
}

provider "twilio" {
}

resource "twilio_conversations_conversations_v1" "create_conversation" {
  friendly_name = "Deep Conversation 00"
}

resource "twilio_conversations_conversations_participants_v1" "add_user_00_to_conversation" {
  conversation_sid                = twilio_conversations_conversations_v1.create_conversation.sid
  messaging_binding_address       = "<User 00 Personal Mobile Number>"
  messaging_binding_proxy_address = "Twilio Phone Number 1"
}

output "create_conversation" {
  value = twilio_conversations_conversations_v1.create_conversation
}

output "add_user_00_to_conversation" {
  value = twilio_conversations_conversations_participants_v1.add_user_00_to_conversation
}

Figure 3

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # twilio_conversations_conversations_participants_v1.add_user_01_to_conversation will be updated in-place
  ~ resource "twilio_conversations_conversations_participants_v1" "add_user_01_to_conversation" {
        id                              = "CHxxxxxxxxxxxxxxxxxxx/MBxxxxxxxxxxxxxxxxxx"
      ~ messaging_binding_address       = "Twilio Phone Number 1" -> "Twilio Phone Number 2"
        # (8 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Changes to Outputs:
  ~ add_user_01_to_conversation = {
      ~ messaging_binding_address           = "Twilio Phone Number 1" -> "Twilio Phone Number 2"
        # (13 unchanged elements hidden)
    }

@thinkingserious
Copy link
Contributor Author

Thank you @RJPearson94, fantastic explanation! Very much appreciated!!

I'm circling back with the conversations API team to help determine the best path forward.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants