Example #1
0
def MakePostResponseNoCrypto(data, flow_id):
    msg_id = utils.randomStr(16)
    data['flow_id'] = flow_id
    data['message_id'] = msg_id
    re = json.dumps(data)
    m = MessageLog(flow_id, msg_id, re)
    m.add()
    return re
Example #2
0
 def outgoing (self, message):
     # log outgoing message
     try:
         if message.PREPAID:
             log     = MessageLog(sender=self.me, recipient=message.peer, text=message.text, date=datetime.now())
             log.save()
     except AttributeError:
         pass
Example #3
0
 def parse (self, message):
     # Test if it's a prepaid message
     for keyword in self.keywords:
         if message.text.upper().startswith(keyword['keyw'].upper()):
             # message is prepaid
             message.PREPAID = True
             message.PREPAID_ACTION  = keyword['id']
             
             # log incoming message
             log     = MessageLog(sender=message.peer, recipient=self.me, text=message.text, date=datetime.now())
             log.save()
             
             return
     # message is not prepaid
     return False
Example #4
0
def local_response_operate():
    """先统一消息进入日志 然后传入相应的函数进行处理"""
    try:
        data = postdata.ParseData(request.data)
        msg = MessageLog(data['flow_id'], data['message_id'], request.data)
        msg.add()
        if data['type'] == 'command':
            return dealCommand(data)
        elif data['type'] == 'data':
            return dealData(data)
        else:
            return postdata.MakePostResponseNoCrypto(
                {
                    'type': 'data',
                    'data': '解析不了'
                }, flow_id=0)
    except Exception as e:
        return postdata.MakePostResponseNoCrypto(
            {
                'type': 'data',
                'data': str(e)
            }, flow_id=0)
Example #5
0
def twilioSMS(k,msg, count):
	
	# To check for country code
	pn = phonenumbers.parse(k)
	print pn.country_code
	country = pycountry.countries.get(alpha_2=region_code_for_number(pn))
	yo = region_code_for_country_code(pn.country_code)
	
	# To get timezone for the specified country
	url = "http://api.timezonedb.com/v2/list-time-zone?key=" + API_Key + "&format=json&country=" + yo
	json_obj_places = urllib2.urlopen(url)
	places = json.load(json_obj_places)
	print places["zones"][0]["zoneName"]

	local_date = datetime.now(pytz.timezone(places["zones"][0]["zoneName"]))
	print local_date.time()

	hour = local_date.time().hour

	try:
		# To check whether the time is the night time or not
		if hour in range(7, 23):
			client = Client(account_sid, auth_token)
			message = client.messages.create(to=k, from_=my_twilio, body=msg)

			time.sleep( 60 )
			print message.sid
			bappa = client.messages(message.sid).fetch()

			print(bappa.body)
			print(bappa.status)

			# Checking the status of the Message after 1 minute if not delivered or sent, sending again
			if bappa.status not in ["delivered", "sent"] and count<5:
				print bappa.status
				count = count +1
				print count
				logging.basicConfig()
				time.sleep(2)
				obj = MessageLog(number=k, dateField=local_date.date(), timeField=local_date.time(), status="Failed or Undelivered")
				obj.save()
				print "yoyo"
				time.sleep(5)
				twilioSMS(k,msg, count)	
		else:
			#Saving to database in Night Time
			print "Night time"
			time.sleep(2)
			logging.basicConfig()
			obj = MessageLog(number=k, dateField=local_date.date(), timeField=local_date.time(), status="Night Time")
			print obj
			obj.save()
			print "bappa"
	except:
		# Checking if the number is invalid
		print "yo bappa"
		obj = MessageLog(number=k, dateField=local_date.date(), timeField=local_date.time(), status="Not Sent")
		obj.save()
		print "error"