def test_famous_command(self): """ Mocked bot famous command """ captain = chatbot.Bot(MockedTests.FAMOUS) expected = "Anne Bonny was a famous pirate." with mock.patch("random.choice", self.mocked_random_choice): actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_mood_command(self): """ Mocked bot mood command """ captain = chatbot.Bot(MockedTests.MOOD) expected = "I be feelin' like crackin' Jenny's tea cup." with mock.patch("random.choice", self.mocked_random_choice): actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_about_command(self): """ Mocked bot about command """ captain = chatbot.Bot(UnmockedTests.ABOUT) expected = "This be a pirate themed chat app with many capabilities, \ to be sure! Explore away!" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_invalid(self): """ Mocked bot invalid input """ captain = chatbot.Bot(UnmockedTests.INVALID_COMMAND) expected = "Sorry, I dern't recognize that command. Please enter '!!help' \ to see a list o' commands." actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_pirate_command(self): """ Mocked bot pirate command """ captain = chatbot.Bot(UnmockedTests.PIRATE) expected = 'A guy who drives a ship and yells "yo dude gimme your money and stuff" \ and gets whatever he wants. Usually has a stash or rum for some reason.' actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_help_command(self): """ Mocked bot help command """ captain = chatbot.Bot(UnmockedTests.HELP) expected = "Here be th' known commands ye can use: !!about - !!help - !!pirate \ - !!mood - !!famous - !!funtranslate <message> - !!insult - !!gif <message>" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_funtranslate_success_command(self, mock_requests_get): """ Mocked bot funtranslate success command """ captain = chatbot.Bot(MockedTests.FUNTRANSLATE) expected = "Ahoy how be ye doin' today good matey" mock_requests_get.return_value = mock.Mock( **{ "status_code": 200, "json.return_value": { "contents": { "translated": "Ahoy how be ye doin' today good matey" } }, }) actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_funtranslate_failed_command(self, mock_requests_get): """ Mocked bot funtranslate failure command """ captain = chatbot.Bot(MockedTests.FUNTRANSLATE) expected = "Too Many Requests: Rate limit of 5 requests per hour exceeded." mock_requests_get.return_value = mock.Mock( **{ "status_code": 429, "json.return_value": { "contents": { "translated": "" } }, }) actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_insult_failed_command(self, mock_requests_get): """ Mocked bot insult failure command """ captain = chatbot.Bot(MockedTests.INSULT) expected = "Too Many Requests: Rate limit of 5 requests per day exceeded." mock_requests_get.return_value = mock.Mock( **{ "status_code": 429, "json.return_value": { "contents": { "taunts": [""] } } }) with mock.patch("random.choice", self.mocked_random_choice): actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_gif_failed1_command(self, mock_requests_get): """ Mocked bot gif failure command """ captain = chatbot.Bot(MockedTests.GIF) expected = "Could not find a related gif." mock_requests_get.return_value = mock.Mock( **{ "status_code": 200, "json.return_value": { "data": [], "pagination": { "total_count": 0 } }, }) actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_gif_failed2_command(self, mock_requests_get): """ Mocked bot gif failure command """ captain = chatbot.Bot(MockedTests.GIF) expected = "Too Many Requests: Rate limit of 42 searches per hour or 1000 searches \ per day exceeded." mock_requests_get.return_value = mock.Mock( **{ "status_code": 429, "json.return_value": { "data": [], "pagination": { "total_count": 0 } }, }) actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_insult_success_command(self, mock_requests_get): """ Mocked bot insult sucess command """ captain = chatbot.Bot(MockedTests.INSULT) expected = ( "Happy now? Ye have me FULL attention! ye el-smelling plundering matey!" ) mock_requests_get.return_value = mock.Mock( **{ "status_code": 200, "json.return_value": { "contents": { "taunts": [ "Happy now? Ye have me FULL attention! ye el-smelling plundering matey!" ] } }, }) with mock.patch("random.choice", self.mocked_random_choice): actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_gif_success_command(self, mock_requests_get): """ Mocked bot gif success command """ captain = chatbot.Bot(MockedTests.GIF) expected = "https://gph.is/g/4g0e07A" mock_requests_get.return_value = mock.Mock( **{ "status_code": 200, "json.return_value": { "data": [{ "images": { "downsized": { "url": "https://gph.is/g/4g0e07A" } } }], "pagination": { "total_count": 1 }, }, }) actual = captain.bot_responses() self.assertEqual(expected, actual)
def on_new_message(data): """ When a new message comes in, store data into database """ print("Received message: ", data) db.session.add( models.ChatHistory(data["userName"], data["picUrl"], data["message"]) ) db.session.commit() # If message starts with '!!' call the bot if data["message"][0:2] == "!!": captain = chatbot.Bot(data["message"]) data["message"] = captain.bot_responses() data["userName"] = "******" data["picUrl"] = "https://avatarfiles.alphacoders.com/792/79207.jpg" print("Received message from bot: ", data) db.session.add( models.ChatHistory(data["userName"], data["picUrl"], data["message"]) ) db.session.commit() emit_all_messages(MESSAGES_RECEIVED_CHANNEL)
def test_invalid_about(self): """ Mocked bot invalid about command """ captain = chatbot.Bot(UnmockedTests.INVALID_ABOUT) expected = "Don't enter anything after '!!about'" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_invalid_funtranslate(self): """ Mocked bot invalid funtranslate command """ captain = chatbot.Bot(UnmockedTests.INVALID_FUNTRANSLATE) expected = "Enter a word or phrase to translate after '!!funtranslate'" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_invalid_help(self): """ Mocked bot invalid help command """ captain = chatbot.Bot(UnmockedTests.INVALID_HELP) expected = "Don't enter anything after '!!help'" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_invalid_pirate(self): """ Mocked bot invalid pirate command """ captain = chatbot.Bot(UnmockedTests.INVALID_PIRATE) expected = "Don't enter anything after '!!pirate'" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_invalid_mood(self): """ Mocked bot invalid mood command """ captain = chatbot.Bot(UnmockedTests.INVALID_MOOD) expected = "Don't enter anything after '!!mood'" actual = captain.bot_responses() self.assertEqual(expected, actual)
def test_invalid_famous(self): """ Mocked bot invalid famous command """ captain = chatbot.Bot(UnmockedTests.INVALID_FAMOUS) expected = "Don't enter anything after '!!famous'" actual = captain.bot_responses() self.assertEqual(expected, actual)
sender = request.cookies["user_id"] message = request.args.get('message') input = "Incoming from %s: %s" % (sender, message) print(input.encode("utf-8")) response = bot.respond_to(sender, message) output = "Outgoing to %s: %s" % (sender, response) print(output.encode("utf-8")) return jsonify({"message": response}) @app.route("/", methods=['GET']) def index(): response = send_from_directory('web', "index.html") response.set_cookie("user_id", str(randint(1, 10000000))) return response @app.route("/web/<filename>", methods=['GET']) def static_files(filename=None): return send_from_directory('web', filename) if __name__ == '__main__': bot = chatbot.Bot() PORT = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=PORT)
return request.args.get('hub.challenge', '') else: return 'Error, wrong validation token' @app.route('/', methods=['POST']) def webhook(): payload = request.get_data() for sender, message in messenger.messaging_events(payload): print "Incoming from %s: %s" % (sender, message) response = bot.respond_to(message) print "Outgoing to %s: %s" % (sender, response) messenger.send_message_with_id(FACEBOOK_TOKEN, sender, response) return "ok" if __name__ == '__main__': # Suppress nltk warnings about not enough data warnings.filterwarnings( 'ignore', '.*returning an arbitrary sample.*', ) if os.path.exists("corpus.txt"): bot = chatbot.Bot(open("corpus.txt").read()) app.run(port=3000, debug=True)
def test_invalid_gif(self): """ Mocked bot invalid gif command """ captain = chatbot.Bot(UnmockedTests.INVALID_GIF) expected = "Enter a word or phrase after '!!gif'" actual = captain.bot_responses() self.assertEqual(expected, actual)