com.twilio.sdk.TwilioRestException: The 'To' number +???????????? is not a valid phone number

Viewed 502

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

I understand if i used english like this

+962772211755 it works

But in arabic

+٩٦٢٧٧٢٢١١٧٥٥ dose not work

And shows 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)
1 Answers

Here is my working example , and to convert the number to english i had to parse it with long, i dont know why it works and converts 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);
        }

    }

Here is java example to proof this ;D Example of parsing arabic and its on my production now

Related