def showShopStyleResults(chat_id,from_user,context): api_json = context['text_query_result'] result_image_urls = [value['image']['sizes']['IPhone']['url'] for value in api_json['products']] text_query_result_index = int(context['text_query_result_index']) i = 0 if text_query_result_index+SHOW_THIS_MANY >= len(result_image_urls): message = "Well, maybe not. Gosh, you're hard to please :/ Try sending me back a pic of something I've showed you already to keep looking." sendSuggestedResponseHowTo(chat_id,from_user,message,context) return api_json urls=[] titles=[] for an_image in result_image_urls[text_query_result_index:]: if i >= SHOW_THIS_MANY: break title = api_json['products'][i]['brandedName'] urls.append(an_image) titles.append(title) if context['platform'] == 'KIK': picture_message = PictureMessage(to=from_user, chat_id=chat_id, pic_url=an_image) picture_message.attribution = CustomAttribution(name=title) kik.send_messages([picture_message]) i +=1 if context['platform'] == 'FB': dispatchMessage(context,'image',chat_id,from_user,urls,suggested_responses=titles) context['text_query_result_index'] = i + text_query_result_index # remember which results we've showed context['text_query_result'] = api_json storeContext(chat_id,from_user,context,action='showShopStyleResults') if context['platform'] == 'KIK': selectAnImageMsg(chat_id,context) return api_json
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 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 abstract_kik_message(to,chat_id,content,msg_type): if msg_type == 'text': return TextMessage(to=to,chat_id=chat_id,body=content) if msg_type == 'image': return PictureMessage(to=to,chat_id=chat_id,pic_url=content) if msg_type == 'link': return LinkMessage(to=to,chat_id=chat_id,url=content)
def test_picture_message_incoming(self): message = PictureMessage.from_json({ 'from': 'aleem', 'participants': ['aleem'], 'mention': None, 'chatId': 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2', 'picUrl': 'http://foo.bar/image', 'attribution': { 'name': 'Webpage', 'iconUrl': 'http://foo.bar/icon' }, 'id': '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0', 'timestamp': 1458336131, 'readReceiptRequested': True, 'metadata': {'some': 'data'}, 'chatType': 'direct' }) self.assertEqual(message.from_user, 'aleem') self.assertEqual(message.participants, ['aleem']) self.assertIsNone(message.mention) self.assertEqual(message.chat_id, 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') self.assertEqual(message.pic_url, 'http://foo.bar/image') self.assertEqual(message.attribution.name, 'Webpage') self.assertEqual(message.attribution.icon_url, 'http://foo.bar/icon') self.assertEqual(message.id, '8e7fc0ad-36aa-43dd-8c5f-e72f5f2ed7e0') self.assertEqual(message.timestamp, 1458336131) self.assertIs(True, message.read_receipt_requested) self.assertEqual(message.chat_type, 'direct') self.assertEqual(message.metadata, {'some': 'data'})
def profile_pic_check_messages(user, message): """Function to check if user has a profile picture and returns appropriate messages. :param user: Kik User Object (used to acquire the URL the profile picture) :param message: Kik message received by the bot :return: Message """ messages_to_send = [] profile_picture = user.profile_pic_url if profile_picture is not None: messages_to_send.append( # Another type of message is the PictureMessage - your bot can send a pic to the user! PictureMessage(to=message.from_user, chat_id=message.chat_id, pic_url=profile_picture)) profile_picture_response = "Here's your profile picture!" else: profile_picture_response = "It does not look like you have a profile picture, you should set one" messages_to_send.append( TextMessage(to=message.from_user, chat_id=message.chat_id, body=profile_picture_response)) return messages_to_send
def bot_send_attachment(self, user_model, file_url, content_type, keyboard=None): if content_type and content_type.startswith('image'): message = PictureMessage(to=user_model.conversation_id, pic_url=file_url) if keyboard: message.keyboards.append(keyboard) self._service.send_messages([message]) return if content_type and content_type.startswith('video'): message = VideoMessage(to=user_model.conversation_id, video_url=file_url) if keyboard: message.keyboards.append(keyboard) self._service.send_messages([message]) return raise Exception('No compatility')
def profile_pic_check_messages(user, message): """Function to check if user has a profile picture and returns appropriate messages. :param user: Kik User Object (used to acquire the URL the profile picture) :param message: Kik message received by the bot :return: Message """ messages_to_send = [] profile_picture = user.profile_pic_url if profile_picture is not None: messages_to_send.append( # Another type of message is the PictureMessage - your bot can send a pic to the user! PictureMessage(to=message.from_user, chat_id=message.chat_id, pic_url=profile_picture)) profile_picture_response = "ini dia foto kamuu!" else: profile_picture_response = "ga mirip loh, yaudh lah yaa" messages_to_send.append( TextMessage(to=message.from_user, chat_id=message.chat_id, body=profile_picture_response)) return messages_to_send
def test_picture_message(self): message = PictureMessage(pic_url='http://foo.bar/image', to='aleem').to_json() self.assertEqual(message, { 'type': 'picture', 'to': 'aleem', 'picUrl': 'http://foo.bar/image' })
def get_picture_message_response(): """ Gets a picture message for consumption by the test suite""" picture_message = PictureMessage( to="daveyjones", chat_id= "0ee6d46753bfa6ac2f089149959363f3f59ae62b10cba89cc426490ce38ea92d", pic_url="http://example.com/me.png") return picture_message
def test_profile_pic_messages(self): """Test the profile pic helper method- should return a picture url""" actual_results = self.testbot.profile_pic_check_messages(self.get_test_user(), TextMessage()) expected_results = [PictureMessage(pic_url="http://example.com/me.png"), TextMessage(body="Here's your profile picture!")] self.assertEquals(actual_results, expected_results)
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 send_pic_message(user,chatId,picLink): kik.send_messages([ PictureMessage( to=user, chat_id=chatId, pic_url=picLink ) ])
def showShopStyleResults(chat_id, from_user, context): api_json = context['text_query_result'] result_image_urls = [ value['image']['sizes']['IPhone']['url'] for value in api_json['products'] ] text_query_result_index = int(context['text_query_result_index']) i = 0 if text_query_result_index + SHOW_THIS_MANY >= len(result_image_urls): message = "Well, maybe not. Gosh, you're hard to please :/ Try sending me back a pic of something I've showed you already to keep looking." sendSuggestedResponseHowTo(chat_id, from_user, message, context) return api_json urls = [] titles = [] for an_image in result_image_urls[text_query_result_index:]: if i >= SHOW_THIS_MANY: break title = api_json['products'][i]['brandedName'] urls.append(an_image) titles.append(title) if context['platform'] == 'KIK': picture_message = PictureMessage(to=from_user, chat_id=chat_id, pic_url=an_image) picture_message.attribution = CustomAttribution(name=title) kik.send_messages([picture_message]) i += 1 if context['platform'] == 'FB': dispatchMessage(context, 'image', chat_id, from_user, urls, suggested_responses=titles) context[ 'text_query_result_index'] = i + text_query_result_index # remember which results we've showed context['text_query_result'] = api_json storeContext(chat_id, from_user, context, action='showShopStyleResults') if context['platform'] == 'KIK': selectAnImageMsg(chat_id, context) return api_json
def test_picture_message_preset_attribution(self): message = PictureMessage(pic_url='http://foo.bar/image', attribution=PresetAttributions.CAMERA, to='aleem').to_json() self.assertEqual( message, { 'type': 'picture', 'to': 'aleem', 'picUrl': 'http://foo.bar/image', 'attribution': 'camera' })
def bot_send_attachment(self, user_model, file_url, file_type, keyboard=None): if file_type and file_type.startswith('image'): return self._service.send_messages( user_model.conversation_id, [PictureMessage(media=file_url)]) if file_type and file_type.startswith('video'): return self._service.send_messages(user_model.conversation_id, [VideoMessage(media=file_url)]) if file_type and file_type.startswith('audio'): raise Exception return self._service.send_messages(user_model.conversation_id, [FileMessage(media=file_url)])
def profile_pic_check_messages(user, message): messages_to_send = [] profile_picture = user.profile_pic_url if profile_picture is not None: messages_to_send.append( # Another type of message is the PictureMessage - your bot can send a pic to the user! PictureMessage( to=message.from_user, chat_id=message.chat_id, pic_url=profile_picture )) profile_picture_response = "Here's your profile picture!" else: profile_picture_response = "It does not look like you have a profile picture, you should set one" messages_to_send.append( TextMessage(to=message.from_user, chat_id=message.chat_id, body=profile_picture_response)) return messages_to_send
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 incoming(): if not kik.verify_signature(request.headers.get('X-Kik-Signature'), request.get_data()): return Response(status=403) messages = messages_from_json(request.json['messages']) for message in messages: if isinstance(message, TextMessage): if message.body == 'Hi': body = 'What\'s the motive?' kik.send_messages([ VideoMessage( to=message.from_user, chat_id=message.chat_id, video_url=getGiphy.getGiphy('Hello'), autoplay=True, loop=True, ), TextMessage( to=message.from_user, chat_id=message.chat_id, body=body, keyboards=[ SuggestedResponseKeyboard( responses=[ TextResponse('Staying in'), TextResponse('Going out'), ] ) ] ) ]) message = TextResponse if isinstance(message, TextMessage): if message.body == 'Going out': body = 'What day though?' kik.send_messages([ TextMessage( to=message.from_user, chat_id=message.chat_id, body=body, keyboards=[ SuggestedResponseKeyboard( responses=[ TextResponse('Sunday'), TextResponse('Monday'), TextResponse('Tuesday'), TextResponse('Wednesday'), TextResponse('Thursday'), TextResponse('Friday'), TextResponse('Saturday'), ] ) ] ) ]) message = TextResponse if isinstance(message, TextMessage): if message.body == 'Friday': body = ('This is what\'s gucci tonight:\nChainsaw: $3 shots\nEthel\'s: $8 for 1.5lbs wings') kik.send_messages([ TextMessage( to=message.from_user, chat_id=message.chat_id, body=body, keyboards=[ SuggestedResponseKeyboard( responses=[ TextResponse('Chainsaw'), TextResponse('Ethel\'s'), ] ) ] ) ]) message = TextResponse if isinstance(message, TextMessage): if message.body == 'Chainsaw': kik.send_messages([ PictureMessage( to=message.from_user, chat_id=message.chat_id, pic_url='http://thecord.ca/wp-content/uploads/2014/09/Chainsaw-Heather-Davidson.jpg', ), TextMessage( to=message.from_user, chat_id=message.chat_id, body='Address: 28 King St N,\n Waterloo, ON N2J 2W6\nPhone:(519) 954-8660' ) ]) return Response(status=200)
def incoming(self): """Handle incoming messages to the bot. All requests are authenticated using the signature in the 'X-Kik-Signature' header, which is built using the bot's api key (set in main() below). :return: Response """ # verify that this is a valid request if not self.kik_api.verify_signature( request.headers.get("X-Kik-Signature"), request.get_data()): return Response(status=403) messages = messages_from_json(request.json["messages"]) response_messages = [] for message in messages: user = self.kik_api.get_user(message.from_user) # Check if its the user's first message. Start Chatting messages are sent only once. if isinstance(message, StartChattingMessage): response_messages.append( TextMessage( to=message.from_user, chat_id=message.chat_id, body="Hey {}, how are you?".format(user.first_name), # keyboards are a great way to provide a menu of options for a user to respond with! keyboards=[ SuggestedResponseKeyboard(responses=[ TextResponse("Good"), TextResponse("Bad") ]) ])) # Check if the user has sent a text message. elif isinstance(message, TextMessage): user = self.kik_api.get_user(message.from_user) message_body = message.body.lower() try: cekpesan = message_body.lower() cekpesan1 = cekpesan[0:6] print(cekpesan1) except: cekpesan1 = message_body print(cekpesan1) if message_body == "kumal": url = 'https://kucingpedia.com/wp-content/uploads/2017/08/Gambar-Harga-Kucing-Persia-Warna-Abu-Abu.jpg' print(str(url)) response_messages.append( PictureMessage(to=message.from_user, chat_id=message.chat_id, pic_url=str(url))) elif cekpesan1 == "gambar": userid = message.from_user pesan = message_body chatid = message.chat_id sql = "INSERT INTO tb_inbox (id_inbox, id_user, id_chat, in_msg, tipee, flag) VALUES (NULL, '%s', '%s', '%s', 'img', '1')" % ( userid, chatid, pesan) curs.execute(sql) conn.commit() print("1 pesan img handle") sql1 = "SELECT id_outbox, id_user, id_chat, out_msg FROM tb_outbox WHERE flag = '1' AND tipee = 'img' ;" cirs.execute(sql1) results = cirs.fetchall() print("Tables : ", cirs.rowcount) for row in results: print(row[0]) print(row[1]) print(row[2]) print(row[3], "\n") url = row[3] print(str(url)) response_messages.append( PictureMessage(to=message.from_user, chat_id=message.chat_id, pic_url=str(url))) sql2 = "UPDATE tb_outbox SET flag='2' WHERE id_outbox='%s';" % ( str(row[0])) curs.execute(sql2) conn.commit() elif cekpesan1 != "gambar": # Insert Pesan ke tabel inbox userid = message.from_user pesan = message_body chatid = message.chat_id sql = "INSERT INTO tb_inbox (id_inbox, id_user, id_chat, in_msg, tipee, flag) VALUES (NULL, '%s', '%s', '%s', 'msg', '1')" % ( userid, chatid, pesan) curs.execute(sql) conn.commit() print("1 pesan msg handle") # Select Pesan dari tabel outbox sql1 = "SELECT id_outbox, id_user, id_chat, out_msg FROM tb_outbox WHERE flag = '1' AND tipee = 'msg';" cirs.execute(sql1) results = cirs.fetchall() print("Tables : ", cirs.rowcount) for row in results: print(row[0]) print(row[1]) print(row[2]) print(row[3], "\n") response_messages.append( TextMessage(to=message.from_user, chat_id=message.chat_id, body=str(row[3]))) sql2 = "UPDATE tb_outbox SET flag='2' WHERE id_outbox='%s';" % ( str(row[0])) curs.execute(sql2) conn.commit() else: response_messages.append( TextMessage( to=message.from_user, chat_id=message.chat_id, body= "Sorry {}, I didn't quite understand that. How are you?" .format(user.first_name), keyboards=[ SuggestedResponseKeyboard(responses=[ TextResponse("Good"), TextResponse("Bad") ]) ])) # If its not a text message, give them another chance to use the suggested responses. else: response_messages.append( TextMessage( to=message.from_user, chat_id=message.chat_id, body= "Sorry, I didn't quite understand that. How are you, {}?" .format(user.first_name), keyboards=[ SuggestedResponseKeyboard(responses=[ TextResponse("Good"), TextResponse("Bad") ]) ])) # We're sending a batch of messages. We can send up to 25 messages at a time (with a limit of # 5 messages per user). self.kik_api.send_messages(response_messages) return Response(status=200)