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

I'm using UnityWebRequest with C# to send messages through twilio, but keep getting the error "A 'to' phone number is required." #682

Open
jwesierski opened this issue Jun 18, 2023 · 3 comments
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library

Comments

@jwesierski
Copy link

Issue Summary

I'm using UnityWebRequest with C# to send messages through Twilio, but keep getting the error "A 'to' phone number is required." I've tested my Twilio number and personal number using Twilios website API so I know the problem isn't with either of them. I assume I should be able to use any phone number, but to be safe I even verified my number as a 'Caller ID' within Twilio. I've encountered errors related to my account SID and Auth, and resolved them so I know they aren't the problem. I recently created my Twilio account and don't have strong experience with it so let me know if I need to make some configurations in the console or if something in my code is causing the recipient phone number to be misread or not sent correctly.

Code Snippet

I call the method in my scene controller file with

StartCoroutine(twilioSMS.SendMessage("+1xxxxxxxxxx", "This is a test message"));

Then in my Twilio file, which I've attached to the scene in Unity:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;

public IEnumerator SendMessage(string recipientPhoneNumber, string message)
{
    // Create the JSON payload for the request
    Debug.Log("The recipient phone number is: " + recipientPhoneNumber);
    string jsonPayload = JsonConvert.SerializeObject(new
    {
        from = twilioPhoneNumber, 
        to = recipientPhoneNumber, 
        body = message
    });

    // Create the UnityWebRequest object
    UnityWebRequest request = UnityWebRequest.Post(twilioMessagesApiUrlFormatted, 
    jsonPayload);

    // Set the authorization header with your Account SID and Auth Token
    string auth = accountSid + ":" + authToken;
    string base64Auth = 
    System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(auth));
    string authorizationHeader = "Basic " + base64Auth;
    request.SetRequestHeader("Authorization", authorizationHeader);
    request.SetRequestHeader("Content-Type", "application/json");

    // Send the HTTP request
    request.uploadHandler = new 
    UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonPayload));
    request.downloadHandler = new DownloadHandlerBuffer();
    yield return request.SendWebRequest();
   
    
}

Using the above method and any phone number I try as recipientPhoneNumber in the '+1xxxxxxxxxx' format returns the error:

Exception/Log

Twilio API Request Payload: {"from":"+1xxxxxxxxxx","to":"+1xxxxxxxxxx","body":"This is a test message"}
Twilio API Response Text: {"code": 21604, "message": "A 'To' phone number is required.", "more_info": "https://www.twilio.com/docs/errors/21604", "status": 400}
Twilio API request error: HTTP/1.1 400 Bad Request
Twilio API Request Method: POST

Technical details:

  • twilio-csharp version: Latest - not using a package
  • csharp version: - latest - using through Unity & VSCode which is also the most recent version
@sbansla
Copy link
Contributor

sbansla commented Aug 9, 2023

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

@sbansla sbansla added the status: work in progress Twilio or the community is in the process of implementing label Aug 9, 2023
@sbansla
Copy link
Contributor

sbansla commented Aug 9, 2023

Hi,
From above code I see that you are not using twilio-csharp for sending request.
I see that you are getting 'Bad Request'.
To check on how we create request.
And example of message api:
https://github.com/twilio/twilio-csharp/blob/main/src/Twilio/Rest/Api/V2010/Account/MessageResource.cs#L119C15-L119C15

How we send request:
https://github.com/twilio/twilio-csharp/blob/main/src/Twilio/Http/SystemNetHttpClient.cs

@sbansla sbansla added status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library and removed status: work in progress Twilio or the community is in the process of implementing labels Aug 9, 2023
@addaki-code
Copy link

This repo and issues board is for the Twilio .NET SDK, which you don't appear to be using.

However, @jwesierski - since you are making your own requests directly against the API, your "To" and other params will need to have an upper case first letter instead of lowercase. See the cURL example here: https://www.twilio.com/docs/sms/api/message-resource?code-sample=code-send-an-sms-message&code-language=curl&code-sdk-version=json

I cannot see the config for your json converter, but I'd suggest this would be a good place to start debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

3 participants