def process(queue) : url = '/zong' get = json.loads(queue) print(get) if 'text' in get and 'sender' in get: num = get['number'] print(num) #op = 'zong' subtxt = get['text'] #subtxt =subtxt.replace("'",'') # meta_data = get['meta_data'] # smpp = ''meta_data.split('?') # print('smpp - split length' + len(smpp)) append_to_log('/mmatcher/sms/log/' + url + '.log', get['sender'] + ' - ' + get['text']) print(subtxt) subtxt = subtxt.lower() if subtxt == 'sub m' : subtxt = subtxt.replace('sub m','sub bus') elif subtxt == 'sub off' : subtxt = subtxt.replace('sub off','unsub') elif subtxt == 'sub free off' : subtxt = subtxt.replace('sub free off','unsub') elif subtxt == 'off sub' : subtxt = subtxt.replace('off sub','unsub') elif subtxt == 'sub' : subtxt = subtxt.replace('sub','sub bus') print(subtxt) body = web.start(subtxt, get['sender'].replace('+',''), url.replace('/','')) logBody = body filename = datetime.now().strftime('/log/' + url + '_sms_usage-%Y%m%U%d.log') append_to_log('/mmatcher/sms' + filename, get['sender'] + ' !|! ' + subtxt + ' !|! ' + logBody + ' !|! '+num) else: body = 'The command was not recognized. To sell, send SELL<space> your item to 289. To buy, send BUY<space>your item to 289. Send H to 289 for help.' smsc.send('zong',get['sender'].replace('+',''), body)
def start(xml): from mmodules import web txt, usr = decode(xml) sms = web.start(txt, usr, 'clickatell') xml = encode(usr, sms) if xml: out = send(xml) return usr, txt, sms
def test_095_add_interest2(self): self.assertEqual(web.start('selling unittestingtestcases for 10', test_user2), 'Your interest I2 is added and currently has 1 match. More matches will arrive on SMS later.')
def test_032_list_interests(self): self.assertEqual(set(web.start('i', test_user).split('\n')), {'I{}:no tst{}'.format(x, x) for x in range(14,0,-1)})
def test_092_list_interests2(self): self.assertEqual(set(web.start('i', test_user2).split('\n')), {'I{}:selling unittestingtestcases for {}(1)'.format(x, x) for x in [9,7,5,3,1]})
def test_114_list_matches(self): rng = range(11, 20) self.assertEqual(set(re.split('[\n:]', web.start('i1', test_user))), {'I1M{}'.format(x) for x in rng} | {'selling unittestingtestcases for {}'.format(x) for x in rng})
def test_122_list_interests2(self): self.assertEqual(web.start('i', test_user2), 'You have no interests. You can add them by sending your interest text.')
def test_104_list_matches(self): self.assertEqual(web.start('i1', test_user), 'No matches for interest 1.')
def test_112_list_interests2(self): self.assertEqual(set(web.start('i', test_user2).split('\n')), {'I{}:selling unittestingtestcases for {}(1)'.format(x, x+10) for x in range(1, 10)})
def test_073_add_interest(self): self.assertEqual(web.start('a10 selling unittestingtestcases for 1', test_user2), 'Your interest I1 is added with 10 days validity. It currently has 1 match. More matches will arrive on SMS later.')
def test_076_list_matches(self): self.assertEqual(web.start('i1', test_user2), 'I1M1:buying unittestingtestcases')
def test_064_no_matches(self): self.assertEqual(web.start('i2', test_user), 'There is no interest I2.\nYour interests are:\nI1:buying unittestingtestcases')
def test_071_register2(self): self.assertEqual(web.start('s', test_user2), "Thank you for subscribing to mmatcher.com.\nFor instructions on how to use mmatcher reply 'H' to this message.")
def test_061_add_interest(self): self.assertEqual(web.start('a1 buying unittestingtestcases', test_user), 'Your interest I1 is added with 1 day validity. It currently has no matches. Matches will arrive on SMS later.')
def test_043_add_interest(self): self.assertEqual(web.start('a0 tst', test_user), 'Your interest I7 is added with 0 days validity. It currently has no matches. Matches will arrive on SMS later.')
def test_041_delete_interest(self): self.assertEqual(web.start('d3|d7', test_user), 'Interest 3 has been deleted.\nInterest 7 has been deleted.')
def test_096_list_interests2(self): self.assertEqual(set(web.start('i', test_user2).split('\n')), {'I{}:selling unittestingtestcases for {}(1)'.format(x,y) for x,y in [(9,9),(7,7),(5,5),(3,3),(2,10),(1,1)]})
def test_078_send_message(self): rest.key_value.mc.delete('u' + test_user2 + 'mLAST') self.assertEqual(web.start('m $100?', test_user2), 'The command was not recognised.')
def test_098_list_matches(self): rng = [10,9,7,5,3,1] self.assertEqual(set(re.sub(r'I1M\d0?:', 'I1M?:', web.start('i1', test_user)).split('\n')), {'I1M?:selling unittestingtestcases for {}'.format(x) for x in rng})
def test_081_send_message(self): self.assertEqual(web.start('m $100?', test_user2), 'The command was not recognised.')
def test_111_add_interests2(self): rng = range(1, 10) self.assertEqual(web.start(''.join('selling unittestingtestcases for {}|'.format(x+10) for x in rng)[:-1], test_user2).split('\n'), ['Your interest I{} is added and currently has 1 match. More matches will arrive on SMS later.'.format(x) for x in rng])
def test_02_list_interests(self): self.assertEqual(web.start('i', test_user), 'I1:no tst1')
def test_113_list_interests(self): self.assertEqual(web.start('i', test_user), 'I1:buying unittestingtestcases(9)')
def test_024_add_interest(self): self.assertEqual(web.start('no tst1', test_user), 'Your interest I1 is added and currently has no matches. Matches will arrive on SMS later.')
def test_121_delete_interests2(self): rng = range(1, 10) self.assertEqual(web.start(''.join(['d{}|'.format(x) for x in rng])[:-1], test_user2).split('\n'), ['Interest {} has been deleted.'.format(x) for x in rng])
def test_084_list_interests(self): self.assertEqual(web.start('i', test_user2), 'I1:selling unittestingtestcases for 1(1)')
def test_082_list_interests(self): self.assertEqual(web.start('m#', test_user2), 'The command was not recognised.')
def test_031_add_interests(self): rng = range(2, 15) self.assertEqual(web.start(''.join('no tst{}|'.format(x) for x in rng)[:-1], test_user).split('\n'), ['Your interest I{} is added and currently has no matches. Matches will arrive on SMS later.'.format(x) for x in rng])
def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) sar_ref_number = 0 if 'DOCUMENT_ROOT' not in environ: environ['DOCUMENT_ROOT'] = '.' if 'CONTENT_LENGTH' in environ and environ['CONTENT_LENGTH'] == '': environ['CONTENT_LENGTH'] = '0' append_to_log(environ['DOCUMENT_ROOT'] + '/log/environ.log', str(environ)) url = environ.get('PATH_INFO', '').lower() if url == '/help': return [web.HTMLhelp().encode('utf-8')] body = '' length = int(environ.get('CONTENT_LENGTH', '0')) if length != 0: utf8 = str(environ['wsgi.input'].read(length), encoding='utf-8') post = parse_qs(utf8) append_to_log(environ['DOCUMENT_ROOT'] + '/log/post_params.log', str(post)) if url == '/clickatell': if 'data' in post: append_to_log(environ['DOCUMENT_ROOT'] + '/log/' + url + '.log', post['data'][0]) sender, txt, body = clickatell.start(post['data'][0]) filename = datetime.now().strftime('/log/' + url + '_sms_usage-%Y%m%U%d.log') append_to_log(environ['DOCUMENT_ROOT'] + filename, sender + ' !|! ' + txt + ' !|! ' + body) else: body = 'Wrong params.' elif url == '/clickatell_callback': if 'data' in post: append_to_log(environ['DOCUMENT_ROOT'] + '/log/' + url + '.log', post['data'][0]) else: body = 'Wrong params.' elif url == '/web': if 'text' in post and 'credentials' in post: append_to_log(environ['DOCUMENT_ROOT'] + '/log/' + url + '.log', post['credentials'][0] + ' - ' + post['text'][0]) body = web.start(post['text'][0], post['credentials'][0], 'web') else: body = 'Wrong params.' else: body = 'Wrong url.' else: if url == '/zong' or url == '/warid': get = parse_qs(environ['QUERY_STRING']) #concat = '' if 'text' in get and 'sender' in get: num = get['number'][0] #op = 'zong' subtxt = get['text'][0] subtxt =subtxt.replace("'",'') meta_data = get['meta_data'][0] smpp = meta_data.split('?') #concat = subtxt #sar_params = [] if (smpp[2] != '') : sar_params = smpp[2].split('&') if (len(sar_params)>= 3) : sar_msg_ref_num = sar_params[0].split('=')[0] sar_msg_ref_num_val = sar_params[0].split('=')[1] sar_total_segments = sar_params[1].split('=')[0] sar_total_segments_val = sar_params[1].split('=')[1] sar_segment_seqnum = sar_params[2].split('=')[0] sar_segment_seqnum_val = sar_params[2].split('=')[1] #key_value = kv.KeyValue(get['sender'][0].replace('+',''), None) #multipart_msg = '' #multipart_msg_ = key_value.retreive_long_msg(sar_msg_ref_num_val) #if sar_total_segments_val <= sar_segment_seqnum_val : # if not multipart_msg_: # key_value.store_long_msg(sar_msg_ref_num_val, subtxt) # else : # key_value.store_long_msg(sar_msg_ref_num_val, multipart_msg_ + ' ' + subtxt) #else : # multipart_msg = key_value.retreive_long_msg(sar_msg_ref_num_val) #if sar_segment_seqnum_val == sar_total_segments_val : # multipart_msg = key_value.retreive_long_msg(sar_msg_ref_num_val) # multipart_msg = multipart_msg + subtxt #smsc.send(url.replace('/',''),'00923333036853', multipart_msg) if sar_segment_seqnum_val == sar_total_segments_val : smsc.send(url.replace('/',''),get['sender'][0], '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.') #smsc.send(url.replace('/',''),get['sender'][0], '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 : append_to_log(environ['DOCUMENT_ROOT'] + '/log/' + url + '.log', get['sender'][0] + ' - ' + get['text'][0]) if url == '/warid' : if num == '8225' : subtxt = get['text'][0].lower().replace('sub','sub sim') elif num == '8226' : subtxt = get['text'][0].lower().replace('sub','sub gol') elif num == '8227' : subtxt = get['text'][0].lower().replace('sub','sub bus') elif num == '8229' : subtxt = get['text'][0].lower().replace('sub free','sub') elif url == '/zong' : print(subtxt) subtxt = subtxt.lower() #if subtxt == 'sub pa' : # subtxt = subtxt.replace('sub pa','sub sim') #elif subtxt == 'sub d' : # subtxt = subtxt.replace('sub d','sub gol') #el if subtxt == 'sub m' : subtxt = subtxt.replace('sub m','sub bus') elif subtxt == 'sub off' : subtxt = subtxt.replace('sub off','unsub') elif subtxt == 'sub free off' : subtxt = subtxt.replace('sub free off','unsub') elif subtxt == 'off sub' : subtxt = subtxt.replace('off sub','unsub') elif subtxt == 'sub' : subtxt = subtxt.replace('sub','sub bus') print(subtxt) body = web.start(subtxt, get['sender'][0].replace('+',''), url.replace('/','')) logBody = body if url == '/warid': smsc.send(url.replace('/',''), get['sender'][0], body) body='' if subtxt == 'sub sim' or subtxt == 'sub gol' or subtxt == 'sub bus' : smsc.send(url.replace('/',''), get['sender'][0], 'Dear User, the Terms of Use for Warid Tijarat are available at http://waridtijarat.waridtel.com Kindly go over the Terms before continuing using this service.') filename = datetime.now().strftime('/log/' + url + '_sms_usage-%Y%m%U%d.log') append_to_log(environ['DOCUMENT_ROOT'] + filename, get['sender'][0] + ' !|! ' + subtxt + ' !|! ' + logBody + ' !|! '+num) else: body = 'The command was not recognized. To sell, send SELL<space> your item to 289. To buy, send BUY<space>your item to 289. Send H to 289 for help.' #body = 'Please resend the ad with more details for us to find the best results for you.' #'Wrong params.4' elif url == '/send_token': get = parse_qs(environ['QUERY_STRING']) if 'sender' in get: append_to_log(environ['DOCUMENT_ROOT'] + '/log/' + url + '.log', get['sender'][0] + ' - ' + get['operator'][0]) body = smsc.send_token(get['operator'][0], get['sender'][0].replace('+','')) else: body = 'Wrong params.' elif url == '/verify_token': get = parse_qs(environ['QUERY_STRING']) if 'sender' in get and 'token' in get: append_to_log(environ['DOCUMENT_ROOT'] + '/log/' + url + '.log', get['sender'][0] + ' - ' + get['token'][0]) body = smsc.verify_token(get['sender'][0].replace('+',''), get['token'][0]) if body == 'Equal.': filename = datetime.now().strftime('/log/web_token-%Y%m%U%d.log') append_to_log(environ['DOCUMENT_ROOT'] + filename, get['sender'][0]) else: body = 'Wrong params.' else: body = 'No params.' return [body.encode('utf-8')]
def test_083_send_message(self): self.assertEqual(web.start('i1m2 how much?', test_user2), 'The command was not recognised.')