我最近写了一个小功能来发送Nexmo消息。除非您需要libpynexmo代码的全部功能,否则这应该为您完成工作。而且,如果您想继续检修libpynexmo,只需复制此代码即可。关键是utf8编码。
如果您想随邮件一起发送其他任何字段,请在此处找到有关 nexmo出站邮件可以包含的内容的完整文档。
Python 3.4测试了Nexmo出站(JSON):
def nexmo_sendsms(api_key, api_secret, sender, receiver, body): """ Sends a message using Nexmo. :param api_key: Nexmo provided api key :param api_secret: Nexmo provided secrety key :param sender: The number used to send the message :param receiver: The number the message is addressed to :param body: The message body :return: Returns the msgid received back from Nexmo after message has been sent. """ msg = { 'api_key': api_key, 'api_secret': api_secret, 'from': sender, 'to': receiver, 'text': body } nexmo_url = 'https://rest.nexmo.com/sms/json' data = urllib.parse.urlenpre(msg) binary_data = data.enpre('utf8') req = urllib.request.Request(nexmo_url, binary_data) response = urllib.request.urlopen(req) result = json.loads(response.readall().depre('utf-8')) return result['messages'][0]['message-id']


