Com.twilio.sdk.TwilioRestException: number "To" + ???????????? invalid phone number

Am Using Twilio to send sms, and when some use the Arabic phone number in my forms, it cannot send sms and shows the following exception.

I understand that I used English as

+962772211755 works

But in arabic

+962772211755 dose not working

And shows an exception

com.twilio.sdk.TwilioRestException: The 'To' number +???????????? is not a valid phone number.
    at com.twilio.sdk.TwilioRestException.parseResponse(TwilioRestException.java:74)
    at com.twilio.sdk.TwilioClient.safeRequest(TwilioClient.java:497)
    at com.twilio.sdk.resource.list.MessageList.create(MessageList.java:70)
+4
source share
1 answer

Here is my working example, and for converting the number to English I had to parse it with a long one, I don’t know why it works and is converted to English,

/**
     * this method simplest way to send SMS with no threading
     *
     * @param userSms
     * @param toNumber e.g +12246193820
     * @param body
     */
    public static void sendSMS(UserSms userSms, String toNumber, String body) {
        TwilioRestClient client = new TwilioRestClient(userSms.getAccountSid(), userSms.getAuthToken());

        //to solve if arabic numbers was submitted and avoid exceptiono
        // com.twilio.sdk.TwilioRestException: The 'To' number +???????????? is not a valid phone number.
        long phoneNumber = Long.parseLong(toNumber.replace("+", ""));// remove plus just in case cause strange parse is working ! with +s
        // Build a filter for the MessageList
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Body", body));
        params.add(new BasicNameValuePair("To", toNumber));// in TRIAL VERSION works only with verified number with trial account
        params.add(new BasicNameValuePair("From", userSms.getFromNumber()));// for test use the one created from twilio

        MessageFactory messageFactory = client.getAccount().getMessageFactory();
        Message message;
        try {
            message = messageFactory.create(params);
            // Get an object from its sid. If you do not have a sid,
            // check out the list resource examples on this page
            message = client.getAccount().getMessage(message.getSid());
            //store to log
            SmsService.saveSmsLog(message, userSms, body);

        } catch (TwilioRestException ex) {
            Logger.getLogger(SmslUtil.class.getName()).log(Level.SEVERE, "send sms exception", ex);
        }

    }

java ; D

+3

Source: https://habr.com/ru/post/1688918/


All Articles