Python calls the Feixin API to send text messages

Source: Internet
Author: User
Tags sendmsg

The test is available in November September 18, 2013. The following code is used: [Replace the account and password with your own password before use]

#-*-Coding: UTF-8-*-# file: SendFetion. py # by Lee 2013-9-18 "using HTTPS to call the Apsara interface: https://quanapi.sinaapp.com/fetion.php? U = login mobile phone number of Apsara stack & p = login password of Apsara stack & to = mobile phone number of the received Apsara stack & m = response result in Json format, if result = 0, the message is successfully sent {"result": 0, "message ": "\ u53d1 \ u9001 \ u6210 \ u529f"} blank "" import sys import httplib import urllib import re import time import json class Fetion: "" model to call fetionapi. attribute: url, fromTel, pwd, toTel, msg function: Trans, format_url, SendMsg "" url =" Https://quanapi.sinaapp.com/fetion.php? U = "def _ init _ (self, toTel, msg, fromTel = '151xxxxxxxx', # default my phone pwd = 'pxxxxxxxx'): self. fromTel = fromTel self. pwd = pwd self. toTel = toTel self. msg = self. trans (msg) def Trans (self, msg): # change space to '% 20', otherwise error raised return re. sub ("", "% 20", str (msg) def format_url (self): url_address = self. url + self. fromTel \ + "& p =" + self. pwd \ + "& to =" + self. toTel \ + "& m =" + self. msg return url_address def SendMsg (self): # call the api by http get method return urllib. urlopen (self. format_url () def msg2log (msg): logfile = open ('myfetion. log', 'A') now = time. strftime ('% Y % m % d % H: % M: % s') logfile. write ('\ n' + now +' \ n' + msg + '\ n') logfile. close () def main (): # format mutual message print "\ n" + "" * 10 + "*" * 60 print "" * 10 + "Personal Fetion" print "* 10 +" Enter the number and message what you want to send. "print" "* 10 +" blank number means yourself, "print" "* 10 +" and a blank message line to exit. "print" "* 10 +" * "* 60 # get the destination phone number toTel = raw_input (" Input the target telphone number: ") if toTel = "": toTel = "151 xxxxxxxx" # none input for a target most used # get the message and send by Fetion class while True: msg = raw_input ("Message :") if msg = "": break # none input to quit else: print "Sending .... "msg2log (msg) ff = Fetion (toTel, msg) answer = ff. sendMsg () data = answer. read () jdata = json. loads (data) if jdata ['result'] = 0: print 'done. ^ _ ^ \ n' else: print 'Fail. -_ = \ n' if _ name _ = '_ main _': main ()

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.