def start(method, number): print('[{}]: {}\n'.format(datetime.now(), 'get/queue/' + method + '/' + str(number))) body = rest.send('*****@*****.**', 'get/queue/' + method + '/' + str(number), {}) if body: queue = decode_queue(body) for q in queue: print('{}[{}]{}: {}'.format(q['method'], q['op'], q['uid'], q['msg'])) if q['method'] == 'sms': smsc.send(q['op'], q['uid'], q['msg']) else: apns.send(q['uid'], q['match_unread'], q['msg_unread'])
def decode_queue(data): queue = [] data = json.loads(data) # print(json.dumps(data, indent=4)) if data and 'status' in data and data[ 'status'] == 'OK' and 'response' in data and data['response']: for user_id, notify_data in data['response'].items(): if 'info' in notify_data and 'user_data' in notify_data[ 'info'] and notify_data['info']['user_data']: if 'apple_push_token' in notify_data['info'][ 'user_data'] and notify_data['info']['user_data'][ 'apple_push_token']: notify_method = 'apns' notify_uid = notify_data['info']['user_data'][ 'apple_push_token'] elif 'mobile_number' in notify_data['info'][ 'user_data'] and notify_data['info']['user_data'][ 'mobile_number'] and notify_data['info'][ 'user_data']['operator']: notify_method = 'sms' notify_uid = notify_data['info']['user_data'][ 'mobile_number'] notify_op = notify_data['info']['user_data']['operator'] else: return queue rest.key_value = rest.kv.KeyValue(notify_uid, rest.send) rest.key_value.add_manifest( json.dumps(notify_data['info']['manifest'])) if notify_method == 'sms': if 'matches' in notify_data: interests_data = notify_data['matches'] else: interests_data = notify_data['messages'] for interest_id, interests in interests_data.items(): if len(interests) == 1: if 'matches' in notify_data: txt = 'a new match' else: txt = 'a new message' else: if 'matches' in notify_data: txt = '{} new matches'.format(len(interests)) else: txt = '{} new messages'.format(len(interests)) lo_id = rest.key_value.lo_id(interest_id) txt = "Your ad 'AD{}' has {}:\n".format(lo_id, txt) for interest in interests: mob = interest['mobile_number'] interest_text = interest['text'] if interest_text.__len__() > 38: interest_text = interest_text[:38] + '..' txt += "{}: {}\n".format(mob[2:], interest_text) if not 'matches' in notify_data: rest.send( '*****@*****.**', 'get/messages/conversation/' + interest['match_id'], {}) if len(interests) == 1: txt += "\nPls contact for more details. " #\nUse eZ Cash for Payments, dial #111#. else: txt += "\nPls contact for more details. " #\nUse eZ Cash for Payments, dial #111#. if 'matches' in notify_data: rest.send('*****@*****.**', 'get/interests/matches/' + interest_id, {}) queue.append({ 'method': notify_method, 'op': notify_op, 'uid': notify_uid, 'msg': txt[:-1], }) elif notify_method == 'apns': match_unread = notify_data['info']['manifest'][ 'match_unread'] msg_unread = notify_data['info']['manifest']['msg_unread'] if match_unread + msg_unread > 0: if 'matches' in notify_data: interests_data = notify_data['matches'] else: interests_data = notify_data['messages'] for interest_id, interests in interests_data.items(): if len(interests) == 1: if 'matches' in notify_data: txt = 'a new match' else: txt = 'a new message' else: if 'matches' in notify_data: txt = '{} new matches'.format( len(interests)) else: txt = '{} new messages'.format( len(interests)) txt = 'Your interest AD# has {}:\n'.format(txt) for interest in interests: txt += "AD#: {}\n".format(interest['text']) queue.append({ 'method': notify_method, 'op': 'apple', 'uid': notify_uid, 'match_unread': match_unread, 'msg_unread': msg_unread, 'msg': txt[:-1], }) else: print('ERROR: unknown notify_method') # print(json.dumps(data, indent=4)) else: print('ERROR: user_data not defined') # print(json.dumps(data, indent=4)) return queue
def start(txt, usr, op): rest.key_value = rest.kv.KeyValue(usr, rest.send) sms = '' for command in txt.split('|'): cmd, lid, msg = parse(command) try: url, params = rest.encode(op, usr, cmd, lid, msg) except rest.NoInterestError: sms += "There is no ad 'AD{}'.\nYour ads are:\n".format(lid) cmd, lid, msg = 'I', '', '' url, params = rest.encode(op, usr, cmd, lid, msg) except rest.NoMatchError: if lid == '': sms += 'There is no conversation going on.\nPlease specify a match number.' else: sms += "There is no match 'AD{}'.\nPlease specify the correct match number.".format(lid[0] + "M" + lid[1]) break except rest.UnknownCommandError: auth = rest.checkAuth(usr) if auth: if auth == '401': if op == 'zong': #sms = "Welcome to Dialog MyTrader. You can now post any amount of ads for just Rs1+tax a day. Just subscribe to the service by sending SUB to 289. (Conditions Apply).".format(product(op)) sms = "Welcome to Dialog MyTrader! To subscribe to the service send SUB to 289. ".format(product(op)) elif op == 'warid': sms = "Please subscribe for using {}.\nYou can subscribe by sending SUB to 8225 for weekly, SUB to 8226 for Monthly, SUB to 8227 for Business package.\n".format(product(op)) else : sms = "Please subscribe for using Mmatcher. You can subscribe by sending sub<space>free to this number. Thanks.".format(product(op)) break else: if cmd == 'SHORT': sms += 'Your sent ad needs more details. To sell, send SELL<space> ad to 289. To buy, send BUY<space> ad to 289. ' #sms += 'Your sent ad needs more details such as Buy or Sell, product name, model, quality and price to find best results for your ads.\n E.g. Buy Toyota Axio X 2010 5M.' break elif cmd == 'LONG': sms += 'Your message needs to be less than 160 characters in length. To sell an item, send SELL<space> your item to 289. To buy, send BUY<space>your item to 289.' break #sms += 'Please post Buy/Sell ads with less than 160 characters in length. To sell an item: send SELL<space> your item to 289. To buy, send BUY<space>your item to 289.' #else: sms += "The command was not recognised. To sell, send SELL<space> your item to 289. To buy, send BUY<space>your item to 289. Send H to 289 for help. " #sms += "Your sent ad needs more details. To sell, send SELL<space> ad to 289. To buy, send BUY<space> ad to 289. " #sms += "Your sent ad needs more details such as Buy or Sell, product name, model, quality and price to find best results for your ads. E.g. Buy Toyota Axio X 2010 5M." break except rest.NoLocationError: sms += "Unknown City!\n To set your location correctly, type 'C<space>city name' and send to 289. e.g. 'C Colombo' Or Dial #289# > MyTrader > Update Location." break if url: body = rest.send(usr, url, params) if body: if body == '401': if op == 'zong': sms = "Welcome to Dialog MyTrader! To subscribe to the service send SUB to 289." #sms = "Welcome to Dialog MyTrader. You can now post any amount of ads for just Rs1+tax a day. Just subscribe to the service by sending SUB to 289. (Conditions Apply)." % format(product(op)) #"Welcome to MyTrader! To subscribe to the service send SUB to 289.".format(product(op)) elif op == 'warid': sms = "Please subscribe for using {}.\nYou can subscribe by sending SUB to 8225 for weekly, SUB to 8226 for Monthly, SUB to 8227 for Business package.\n".format(product(op)) else : sms = "Please subscribe for using Mmatcher. You can subscribe by sending sub<space>free to this number. Thanks.".format(product(op)) break else: if cmd == 'SHORT': sms += 'Your sent ad needs more details. To sell, send SELL<space> ad to 289. To buy, send BUY<space> ad to 289. ' break elif cmd == 'LONG': sms += 'Your message needs to be less than 160 characters in length. To sell an item, send SELL<space> your item to 289. To buy, send BUY<space>your item to 289.' break #sms += 'Please post Buy/Sell ads with less than 160 characters in length. To sell an item: send SELL<space> your item to 289. To buy, send BUY<space>your item to 289.' sms += rest.decode(product(op), cmd, lid, body) + '\n' else: sms += 'EMPTY: ' + command + '\n' elif cmd == 'H': sms += help() + '\n' else: sms += 'ERROR: ' + command + '\n' if sms: sms = sms[:-1] return sms
def decode_queue(data): queue = [] data = json.loads(data) # print(json.dumps(data, indent=4)) if data and 'status' in data and data['status'] == 'OK' and 'response' in data and data['response']: for user_id, notify_data in data['response'].items(): if 'info' in notify_data and 'user_data' in notify_data['info'] and notify_data['info']['user_data']: if 'apple_push_token' in notify_data['info']['user_data'] and notify_data['info']['user_data']['apple_push_token']: notify_method = 'apns' notify_uid = notify_data['info']['user_data']['apple_push_token'] elif 'mobile_number' in notify_data['info']['user_data'] and notify_data['info']['user_data']['mobile_number'] and notify_data['info']['user_data']['operator']: notify_method = 'sms' notify_uid = notify_data['info']['user_data']['mobile_number'] notify_op = notify_data['info']['user_data']['operator'] else: return queue rest.key_value = rest.kv.KeyValue(notify_uid, rest.send) rest.key_value.add_manifest(json.dumps(notify_data['info']['manifest'])) if notify_method == 'sms': if 'matches' in notify_data: interests_data = notify_data['matches'] else: interests_data = notify_data['messages'] for interest_id, interests in interests_data.items(): if len(interests) == 1: if 'matches' in notify_data: txt = 'a new match' else: txt = 'a new message' else: if 'matches' in notify_data: txt = '{} new matches'.format(len(interests)) else: txt = '{} new messages'.format(len(interests)) lo_id = rest.key_value.lo_id(interest_id) txt = "Your ad 'AD{}' has {}:\n".format(lo_id, txt) for interest in interests: mob = interest['mobile_number'] interest_text = interest['text'] if interest_text.__len__() > 38: interest_text = interest_text[:38] + '..' txt += "{}: {}\n".format(mob[2:], interest_text) if not 'matches' in notify_data: rest.send('*****@*****.**', 'get/messages/conversation/' + interest['match_id'], {}) if len(interests) == 1: txt += "\nPls contact for more details. " #\nUse eZ Cash for Payments, dial #111#. else: txt += "\nPls contact for more details. " #\nUse eZ Cash for Payments, dial #111#. if 'matches' in notify_data: rest.send('*****@*****.**', 'get/interests/matches/' + interest_id, {}) queue.append({ 'method': notify_method, 'op': notify_op, 'uid': notify_uid, 'msg': txt[:-1], }) elif notify_method == 'apns': match_unread = notify_data['info']['manifest']['match_unread'] msg_unread = notify_data['info']['manifest']['msg_unread'] if match_unread + msg_unread > 0: if 'matches' in notify_data: interests_data = notify_data['matches'] else: interests_data = notify_data['messages'] for interest_id, interests in interests_data.items(): if len(interests) == 1: if 'matches' in notify_data: txt = 'a new match' else: txt = 'a new message' else: if 'matches' in notify_data: txt = '{} new matches'.format(len(interests)) else: txt = '{} new messages'.format(len(interests)) txt = 'Your interest AD# has {}:\n'.format(txt) for interest in interests: txt += "AD#: {}\n".format(interest['text']) queue.append({ 'method': notify_method, 'op': 'apple', 'uid': notify_uid, 'match_unread': match_unread, 'msg_unread': msg_unread, 'msg': txt[:-1], }) else: print('ERROR: unknown notify_method') # print(json.dumps(data, indent=4)) else: print('ERROR: user_data not defined') # print(json.dumps(data, indent=4)) return queue