2

I am beginner in python,But here is issue that when we send sms language unicode(Hindi, Arabic...etc ), but sms not sent .What can i do please help me. If you have any idea then give me suggestion that how cans send multi language sms send using modem or dongle.

def recept(message, recipient):
   time.sleep(0.5)
   phone.write('AT\r\n')
   time.sleep(0.5)
   phone.write('AT+CMGF=1\r\n')
   time.sleep(0.5)
   phone.write('AT+CMGW="'+recipient+'"\r\n')
   out = ''
   time.sleep(1)
   while phone.inWaiting() > 0:
      out += phone.read(1)
   if out != '':
      print ">>" + out
   phone.write(message)
   phone.write('\x1a')
   out = ''
   time.sleep(1)
   while phone.inWaiting() > 0:
      out += phone.read(1)
   if out != '':
      print ">>" + out
   number = get_num(out)
   phone.write('AT+CMSS='+number+'\r\n')
   out = ''
   time.sleep(1)
   while phone.inWaiting() > 0:
      out += phone.read(1)
   if out != '':
      print ">>" + out

def sendSMS(message):
  try:
   phone.open()
   phone.flushInput()
   phone.flushOutput()
   for row in mobileno:
    time.sleep(0.5)
    mobile = row
    recept(message, mobile)
   time.sleep(1)
   phone.write('AT+CMGD=1,4\r\n')
   phone.close()
  finally:
   phone.close()

# type your message here
message = u'लड़की के चक्कर में मत पड़ना भाई'
sendSMS(message)

+++++++++++++++++ ERROR ++++++++++++++++++++++
लड़की के चक्कर में मत पड़ना भाई
>>AT
OK
AT+CMGF=1
OK
AT+CMGW="Phone number with country code"
> 
Traceback (most recent call last):
  File "right.py", line 76, in <module>
    sendSMS(message)
  File "right.py", line 65, in sendSMS
    recept(u'लड़की के चक्कर में मत पड़ना भाई', mobile)
  File "right.py", line 40, in recept
    phone.write(u'लड़की के चक्कर में मत पड़ना भाई')
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 491, in write
    d = to_bytes(data)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 76, in to_bytes
    b.append(item)  # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer or string of size 1 is required

Thanks in advance

Kaushik
  • 6,150
  • 5
  • 39
  • 54
yogesh kumar
  • 327
  • 1
  • 4
  • 15

1 Answers1

0

Have a look at this question about sending Unicode messages. It's about C#, but the SMS part applies. It references a tutorial on sending SMS, whose “Sending an Unicode SMS message” section you might want to look at.

AFAICT, your code has these two issues

First, you don't specify an encoding, which implies “plain text”, something around 7-bit or 8-bit GSM 03.38. You need to configure an encoding that supports Arabic and Hindi, for example HEX:

phone.write('AT+CSCS="HEX"\r\n');

Then you likely need to specify the Data Coding Scheme (the last part, the 8, is the relevant part for the DCS):

phone.write("AT+CSMP=1,167,0,8\r\n")

Second, the code crashes because the message you pass to phone.write() is of type unicode. The serial module expects a list of numbers (codepoints) or bytestrings. If you chose HEX encoding above, then you can convert your text like this:

"".join([hex(ord(c)).replace("0x", "0") for c in u"लड़की के चक्कर में मत पड़ना भाई"])

(Maybe there is a better way, but binascii.hexlify did not produce the correct format.)

In your case, this gives:

'0932095c0915094002009150947020091a0915094d09150930020092e09470902020092e0924020092a095c0928093e020092d093e0908'

Try sending this, instead of u"लड़की के चक्कर में मत पड़ना भाई".

Of course, all of this assumes that your modem supports Unicode messages in the first place. See the above mentioned tutorial on how to check that.

Also note, that the OP of the other question had issues with "\r" as line ending. If you run into problems, try using "\n" instead.

If you're more interested in getting the messages sent, and less in the building-it aspect of it, you might want to consider services such as Twilio or Nexmo, which offer comparably easy-to-use APIsfor sending SMS.

Community
  • 1
  • 1
mknecht
  • 1,205
  • 11
  • 20
  • Hi mknecht same issue occure when run py file def recept(message, recipient): time.sleep(0.5) phone.write('AT\r\n') time.sleep(0.5) phone.write('AT+CSCS="HEX"\r\n'); time.sleep(0.5) phone.write('AT+CMGF=1\r\n') time.sleep(0.5) phone.write("AT+CSMP=1,167,0,8\r\n") time.sleep(0.5) phone.write('AT+CMGW="'+recipient+'"\r\n') out = '' time.sleep(1) while phone.inWaiting() > 0: out += phone.read(1) if out != '': print ">>" + out phone.write(message) phone.write('\x1a') out = '' time.sleep(1) Error :same above question – yogesh kumar Sep 23 '15 at 18:05
  • Then I assume that your `message` is still a Unicode string, and you did not convert it to a bytestring, see my "Second" point. – mknecht Sep 24 '15 at 00:10
  • @yogeshkumar if you already used the hex string above, then it means you need to remove the `u` in front of the `u"0932095c09150940020091509470200…"` string. It marks this string as Unicode. – mknecht Sep 24 '15 at 01:48
  • I have been check multiple time but same issue oocur – yogesh kumar Sep 24 '15 at 17:26