user1576199
user1576199

Reputation: 3207

Sending sms using bulksms provider

I am using bulk sms service, https://www.bulksms.com,

I am not able to send arabic message due to encoding.

message = بريستيج
vals = {
      'username': gateway.login,
      'password': gateway.password,
      'message': message,
      'msisdn': mobile,
}
urllib.urlencode(vals)
req = urllib2.Request(url, params)
f = urllib2.urlopen(req)

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)

I fixed this issue with: message.encode('utf8')

but when message send it replace char with ??? ?????? char

Thanks for help

Upvotes: 0

Views: 732

Answers (1)

PaoloC
PaoloC

Reputation: 529

Presuming you're using BulkSMS.com's older EAPI, for a Unicode SMS you would have to add dca=16bit to the URL, and encode your message body in hex - see How do I send Unicode (16-bit) SMSs? in the FAQ.

It would be far easier to use the newer JSON API, which will do roughly what you expected in the first place, with no extra effort:

# coding=utf-8
import json
import urllib
import urllib2
import base64

data = {
      'to': '+1234567890',
      'body': 'بريستيج'
}

encodedData = json.dumps(data, encoding="utf-8", ensure_ascii=False)
request = urllib2.Request('https://api.bulksms.com/v1/messages?auto-unicode=true')
request.add_header('Content-Type', 'application/json;charset=utf-8')
base64string = base64.b64encode('%s:%s' % ('your_username', 'your_password'))
request.add_header("Authorization", "Basic %s" % base64string)
f = urllib2.urlopen(request, encodedData)
response = f.read()
f.close()
print response

Upvotes: 2

Related Questions