def test_message_inequality(self): message1 = PictureMessage( pic_url='http://foo.bar/image', to='aleem', mention='anotherbot', chat_id= 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', keyboards=[ SuggestedResponseKeyboard(hidden=True, responses=[TextResponse('Foo')]) ], attribution=CustomAttribution(name='Foobar Not The Same'), delay=100) message2 = PictureMessage( pic_url='http://foo.bar/image', to='aleem', mention='anotherbot', chat_id= 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', keyboards=[ SuggestedResponseKeyboard(hidden=True, responses=[TextResponse('Foo')]) ], attribution=CustomAttribution(name='Foobar'), delay=100) self.assertNotEqual(message1, message2)
def test_text_messaging_response_bad(self): """The user's feeling bad today.""" self.testbot.kik_api.get_user = mock.MagicMock( return_value=self.get_test_user()) self.testbot.kik_api.verify_signature = mock.MagicMock( return_value=True) self.testbot.kik_api.send_messages = mock.MagicMock() data = self.get_text_message("Bad") response = self.app.post('/incoming', data=data, content_type='application/json') self.assertEqual(response.status_code, 200) expected_results = [ TextMessage( to="daveyjones", chat_id= "0ee6d46753bfa6ac2f089149959363f3f59ae62b10cba89cc426490ce38ea92d", body="Oh No! :( Wanna see your profile pic?", keyboards=[ SuggestedResponseKeyboard(responses=[ TextResponse("Yep! I Sure Do!"), TextResponse("No Thank You") ]) ]) ] self.testbot.kik_api.send_messages.assert_called_once() self.testbot.kik_api.send_messages.assert_called_once_with( expected_results)
def test_text_message_with_keyboard(self): message = TextMessage(body='Some text', to='aleem', id='8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', keyboards=[ SuggestedResponseKeyboard( hidden=True, responses=[TextResponse('Foo')]) ]).to_json() self.assertEqual( message, { 'type': 'text', 'to': 'aleem', 'body': 'Some text', 'id': '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'text', 'body': 'Foo' }] }] })
def send_text_response(to, chat_id, body, keyboards=None, hidden=False): message = TextMessage(to=to, chat_id=chat_id, body=body) if keyboards: message.keyboards.append( SuggestedResponseKeyboard(hidden=hidden, responses=keyboards)) kik.send_messages([message])
def test_start_messaging(self): """Tests that the web framework properly handles a start-messenging event""" self.testbot.kik_api.get_user = mock.MagicMock( return_value=self.get_test_user()) self.testbot.kik_api.verify_signature = mock.MagicMock( return_value=True) self.testbot.kik_api.send_messages = mock.MagicMock() data = self.get_start_chatting_message() response = self.app.post('/incoming', data=data, content_type='application/json') self.assertEqual(response.status_code, 200) expected_result = TextMessage( to="daveyjones", chat_id= "b3be3bc15dbe59931666c06290abd944aaa769bb2ecaaf859bfb65678880afab", body="Hey Davey, how are you?", keyboards=[ SuggestedResponseKeyboard( responses=[TextResponse('Good'), TextResponse('Bad')]) ]) self.testbot.kik_api.send_messages.assert_called_once() self.testbot.kik_api.send_messages.assert_called_once_with( [expected_result])
def test_response_unexpected(self): """Tests that the bot responds with a message, the picture and another message when texted.""" self.testbot.kik_api.get_user = mock.MagicMock( return_value=self.get_test_user()) self.testbot.kik_api.verify_signature = mock.MagicMock( return_value=True) self.testbot.kik_api.send_messages = mock.MagicMock() data = self.get_text_message("omg r u real?") response = self.app.post('/incoming', data=data, content_type='application/json') self.assertEqual(response.status_code, 200) expected_results = [ TextMessage( to="daveyjones", chat_id= "0ee6d46753bfa6ac2f089149959363f3f59ae62b10cba89cc426490ce38ea92d", body= "Sorry Davey, I didn't quite understand that. How are you?", keyboards=[ SuggestedResponseKeyboard( responses=[TextResponse('Good'), TextResponse('Bad')]) ]) ] self.testbot.kik_api.send_messages.assert_called_once() actual_results = self.testbot.kik_api.send_messages.call_args[0][0] self.assertEqual(actual_results, expected_results)
def test_suggested_keyboard_message(self): message = TextMessage.from_json({ 'to': 'aleem', 'participants': ['aleem'], 'chatId': 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2', 'body': 'Some text', 'id': '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', 'timestamp': 1458336131, 'readReceiptRequested': True, 'keyboards': [{ 'to': 'aleem', 'type': 'suggested', 'hidden': False, 'responses': [{ 'type': 'text', 'body': 'Ok!' }, { 'type': 'text', 'body': 'No way!' }, { 'type': 'friend-picker', 'body': 'Pick a friend!', 'min': 1, 'max': 5, 'preselected': ['foo', 'bar'] }] }] }) self.assertEqual(message.to, 'aleem') self.assertEqual(message.participants, ['aleem']) self.assertEqual( message.chat_id, 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') self.assertEqual(message.body, 'Some text') self.assertEqual(message.id, '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0') self.assertEqual(message.timestamp, 1458336131) self.assertIs(True, message.read_receipt_requested) responses = [ TextResponse('Ok!'), TextResponse('No way!'), FriendPickerResponse('Pick a friend!', 1, 5, ['foo', 'bar']) ] self.assertEqual(message.keyboards, [ SuggestedResponseKeyboard( to='aleem', hidden=False, responses=responses) ])
def send_image_response(to, chat_id, album_art, album, keyboards=None): message = PictureMessage(to=to, chat_id=chat_id, pic_url=album_art) message.attribution = CustomAttribution(name=album) if keyboards: message.keyboards.append( SuggestedResponseKeyboard(hidden=True, responses=keyboards)) kik.send_messages([message])
def test_link_message_complete(self): message = LinkMessage( url='http://foo.bar', to='aleem', mention='anotherbot', chat_id= 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', title='A Title', text='Some Text', pic_url='http://foo.bar/image', no_forward=True, kik_js_data='foobar', keyboards=[ SuggestedResponseKeyboard(hidden=True, responses=[TextResponse('Foo')]) ], attribution=CustomAttribution(name='Foobar'), delay=100).to_json() self.assertEqual( message, { 'type': 'link', 'to': 'aleem', 'url': 'http://foo.bar', 'mention': 'anotherbot', 'chatId': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'title': 'A Title', 'text': 'Some Text', 'picUrl': 'http://foo.bar/image', 'noForward': True, 'kikJsData': 'foobar', 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'text', 'body': 'Foo' }] }], 'attribution': { 'name': 'Foobar' }, 'delay': 100 })
def send_text(user, chat_id, body, keyboards=[]): """Send text.""" message = TextMessage(to=user, chat_id=chat_id, body=body) if keyboards: message.keyboards.append( SuggestedResponseKeyboard( to=user, hidden=False, responses=[TextResponse(keyboard) for keyboard in keyboards], )) kik.send_messages([message])
def test_video_message_complete(self): message = VideoMessage( video_url='http://foo.bar/vid', to='aleem', mention='anotherbot', chat_id= 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', autoplay=True, muted=True, loop=True, no_save=True, keyboards=[ SuggestedResponseKeyboard(hidden=True, responses=[TextResponse('Foo')]) ], attribution=CustomAttribution(name='Foobar'), delay=100).to_json() self.assertEqual( message, { 'type': 'video', 'to': 'aleem', 'mention': 'anotherbot', 'chatId': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'videoUrl': 'http://foo.bar/vid', 'muted': True, 'loop': True, 'autoplay': True, 'noSave': True, 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'text', 'body': 'Foo' }] }], 'attribution': { 'name': 'Foobar' }, 'delay': 100 })
def send_message_with_responses(self, message, responses): text_responses = [] for response in responses: text_responses.append(TextResponse(response)) self.response_messages.append( TextMessage(to=self.message.from_user, chat_id=self.message.chat_id, body=message, keyboards=[ SuggestedResponseKeyboard(responses=text_responses) ]))
def _build_keyboard(message, to, responses): """ Build the keyboard for the provided message with the specified responses. :param message: The message to have the responses added to it. :param to: The recipient of the message. :param responses: An array with Kik keyboard responses. :return: Nothing (message object directly modified to include the keyboard) """ if len(responses) > 0: message.keyboards.append( SuggestedResponseKeyboard(to=to, hidden=False, responses=responses))
def SetKeyboard(user): if user in adminlist: Keyboard = [SuggestedResponseKeyboard( to=user, hidden=True, responses=[TextResponse('Set Probe'), TextResponse('Benchmark Analysis'), TextResponse('Probe Analysis'), TextResponse('Inactive Analysis'), TextResponse('Reset Benchmark')] )] else: Keyboard = [SuggestedResponseKeyboard( to=user, hidden=True, responses=[TextResponse('Set Probe'), TextResponse('Benchmark Analysis'), TextResponse('Probe Analysis'), TextResponse('Inactive Analysis'), TextResponse('Help')] )] return Keyboard
def test_set_configuration(self, post): config = Configuration(webhook='https://example.com/incoming', features={'manuallySendReadReceipts': True}, static_keyboard=SuggestedResponseKeyboard( responses=[TextResponse('foo')])) response = self.api.set_configuration(config) self.assertEqual(post.call_count, 1) self.assertEqual(post.call_args[0][0], 'https://api.kik.com/v1/config') self.assertEqual(post.call_args[1]['timeout'], 60) self.assertEqual(post.call_args[1]['auth'], ('mybotusername', 'mybotapikey')) self.assertEqual(post.call_args[1]['headers'], {'Content-Type': 'application/json'}) self.assertEqual( json.loads(post.call_args[1]['data']), { 'webhook': 'https://example.com/incoming', 'features': { 'manuallySendReadReceipts': True }, 'staticKeyboard': { 'type': 'suggested', 'responses': [{ 'type': 'text', 'body': 'foo' }] } }) self.assertIsInstance(response, Configuration) self.assertEqual(response.webhook, 'https://example.com/incoming') self.assertEqual(response.features, {'manuallySendReadReceipts': True}) self.assertIsInstance(response.static_keyboard, SuggestedResponseKeyboard) self.assertEqual( response.static_keyboard, SuggestedResponseKeyboard(responses=[TextResponse('foo')]))
def SetKeyboard(user): if user in adminlist: Keyboard = [ SuggestedResponseKeyboard(to=user, hidden=True, responses=[ TextResponse('BENCHMARK ANALYSIS'), TextResponse('PROBE ANALYSIS'), TextResponse('INACTIVE ANALYSIS'), TextResponse('RESET BENCHMARK') ]) ] else: Keyboard = [ SuggestedResponseKeyboard(to=user, hidden=True, responses=[ TextResponse('BENCHMARK ANALYSIS'), TextResponse('PROBE ANALYSIS'), TextResponse('INACTIVE ANALYSIS') ]) ] return Keyboard
def seeResultsOnWebsite(chat_id, context): from_user = context['from_user'] img_url = context['user_img_url'] msg = LinkMessage(to=from_user, chat_id=chat_id, url='http://gofindfashion.com?' + img_url, title="Go Find Fashion Seach Engine") msg.keyboards.append( SuggestedResponseKeyboard(responses=[ TextResponse('See more results'), TextResponse('Search with this pic'), TextResponse('New search') ])) kik.send_messages([msg])
def test_get_configuration(self, get): config = self.api.get_configuration() get.assert_called_once_with('https://api.kik.com/v1/config', timeout=60, auth=('mybotusername', 'mybotapikey')) self.assertIsInstance(config, Configuration) self.assertEqual(config.webhook, 'https://example.com/incoming') self.assertEqual(config.features, {'manuallySendReadReceipts': True}) self.assertIsInstance(config.static_keyboard, SuggestedResponseKeyboard) self.assertEqual( config.static_keyboard, SuggestedResponseKeyboard(responses=[TextResponse('foo')]))
def __choose_response(self, message): messages = [] response = self.engine.computeResponse(message.body) message = TextMessage(to=message.from_user, chat_id=message.chat_id, body=response) message.keyboards.append( SuggestedResponseKeyboard(hidden=False, responses=[TextResponse('OK')])) messages.append(message) return messages
def test_text_message_with_all_keyboard_types(self): message = TextMessage( body='Some text', to='aleem', id='8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', keyboards=[ SuggestedResponseKeyboard( hidden=True, responses=[ PictureResponse('http://foo.bar', {'some': 'data'}), FriendPickerResponse('Foo', 7, 16, ['bar', 'dar']), TextResponse('Bar') ]) ]).to_json() self.assertEqual( message, { 'type': 'text', 'to': 'aleem', 'body': 'Some text', 'id': '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'picture', 'picUrl': 'http://foo.bar', 'metadata': { 'some': 'data' } }, { 'type': 'friend-picker', 'body': 'Foo', 'min': 7, 'max': 16, 'preselected': ['bar', 'dar'] }, { 'type': 'text', 'body': 'Bar' }] }] })
def SendResetMessage(messageObject): kik.send_messages([ TextMessage( to=messageObject.from_user, chat_id=messageObject.chat_id, body= "Resetting will clear all previously recorded member data. Do you wish to continue?", keyboards=[ SuggestedResponseKeyboard( to=messageObject.from_user, hidden=True, responses=[TextResponse('YES'), TextResponse('NO')]) ]) ]) return Response(status=200)
def send_wubble_response(to, chat_id, url, keyboards=None): message = WubbleMessage(to=to, chat_id=chat_id, width=130, height=143, url=url_for("main.music_player", id=url, _external=True)) help_body = 'Reply with @mus.iq to guess the song title' help_message = TextMessage(to=to, chat_id=chat_id, body=help_body) if to != 'mus.iq' else None if keyboards: message.keyboards.append( SuggestedResponseKeyboard(hidden=True, responses=keyboards)) kik.send_messages([message])
def selectAnImageMsg(chat_id, context): from_user = context['from_user'] responses = [ TextResponse('Digging the first one'), TextResponse('Like the second'), TextResponse("Let's go with the third"), TextResponse('See more like this'), TextResponse('New search') ] # if context['search_type'] == 'image': # responses.append(TextResponse('See results on the GoFindFashion website')) # website down for now select_an_image_msg = TextMessage(to=from_user, chat_id=chat_id, body=canned_responses.show_outfits()) select_an_image_msg.keyboards.append( SuggestedResponseKeyboard(responses=responses)) kik.send_messages([select_an_image_msg])
def sendKikMessages(chat_id,from_user,msgs,suggested_responses=[],msg_type=None): send_these = [] for msg in msgs: send_these.append(abstract_kik_message( to=from_user, chat_id=chat_id, content=msg, msg_type=msg_type )) if msg_type == 'text' and suggested_responses: text_resonses = [TextResponse(r) for r in suggested_responses] send_these[-1].keyboards.append( SuggestedResponseKeyboard( responses=text_resonses ) ) kik.send_messages(send_these)
def test_to_json(self): config = Configuration(webhook='https://mybot.com/incoming', features={'manuallySendReadReceipts': True}, static_keyboard=SuggestedResponseKeyboard( responses=[TextResponse('foo')])) self.assertEqual( config.to_json(), { 'webhook': 'https://mybot.com/incoming', 'features': { 'manuallySendReadReceipts': True }, 'staticKeyboard': { 'type': 'suggested', 'responses': [{ 'type': 'text', 'body': 'foo' }] } })
def test_text_message_with_keyboard_picture_response(self): message = TextMessage( body='Some text', to='aleem', id='8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', keyboards=[ SuggestedResponseKeyboard( hidden=True, responses=[ PictureResponse('http://foo.bar', {'some': 'data'}), PictureResponse('http://foo.bar', "some data") ]) ]).to_json() self.assertEqual( message, { 'type': 'text', 'to': 'aleem', 'body': 'Some text', 'id': '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'picture', 'picUrl': 'http://foo.bar', 'metadata': { 'some': 'data' } }, { 'type': 'picture', 'picUrl': 'http://foo.bar', 'metadata': "some data" }] }] })
def test_from_json(self): config = Configuration.from_json({ 'webhook': 'https://mybot.com/incoming', 'features': { 'manuallySendReadReceipts': True }, 'staticKeyboard': { 'type': 'suggested', 'responses': [{ 'type': 'text', 'body': 'foo' }] } }) self.assertEqual(config.webhook, 'https://mybot.com/incoming') self.assertEqual(config.features, {'manuallySendReadReceipts': True}) self.assertIsInstance(config.static_keyboard, SuggestedResponseKeyboard) self.assertEqual( config.static_keyboard, SuggestedResponseKeyboard(responses=[TextResponse('foo')]))
def test_picture_message_complete(self): message = PictureMessage( pic_url='http://foo.bar/image', to='aleem', mention='anotherbot', chat_id= 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', keyboards=[ SuggestedResponseKeyboard(hidden=True, responses=[TextResponse('Foo')]) ], attribution=CustomAttribution(name='Foobar'), delay=100).to_json() self.assertEqual( message, { 'type': 'picture', 'to': 'aleem', 'mention': 'anotherbot', 'chatId': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'picUrl': 'http://foo.bar/image', 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'text', 'body': 'Foo' }] }], 'attribution': { 'name': 'Foobar' }, 'delay': 100 })
def test_text_message_with_keyboard_friend_picker_response(self): message = TextMessage(body='Some text', to='aleem', id='8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', keyboards=[ SuggestedResponseKeyboard( hidden=True, responses=[ FriendPickerResponse( 'Foo', 7, 16, ['bar', 'dar']) ]) ]).to_json() self.assertEqual( message, { 'type': 'text', 'to': 'aleem', 'body': 'Some text', 'id': '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', 'keyboards': [{ 'type': 'suggested', 'hidden': True, 'responses': [{ 'type': 'friend-picker', 'body': 'Foo', 'min': 7, 'max': 16, 'preselected': ['bar', 'dar'] }] }] })
MessageHandler = MessageBuilder() app = Flask(__name__) kik = KikApi('BOTID', 'API_KEY') #-------------------------KIK BOT CONFIG------------------------------------------------------ afeatures = { "manuallySendReadReceipts": False, "receiveReadReceipts": True, "receiveDeliveryReceipts": False, "receiveIsTyping": False } staticKeyboard = SuggestedResponseKeyboard(responses=[ TextResponse('BENCHMARK ANALYSIS'), TextResponse('PROBE ANALYSIS'), TextResponse('INACTIVE ANALYSIS'), TextResponse('HELP') ]) kik.set_configuration( Configuration(webhook='WEBHOOK', features=afeatures, static_keyboard=staticKeyboard)) banlist = "" #list of users to have privadged access to reset benchmark adminlist = {} #-----------------Returns Keyboard based on if user in in adminlist or not