Your task is to plan and implement a test structure to ensure that WhatsappBot
behaves correctly when handling
a few default conversations, as well as achieving a high degree of code coverage. To do that, you will need to
- Create a private fork of the repo
- Grant access to [email protected] and [email protected] so that they can review the progress.
- Note that making multiple commits instead of a single one with everything is preferred.
- Plan the tests
- By looking at the code, write down sample conversations that you consider should be implemented as tests in order to achieve a high degree of coverage of
WhatsappBot
. main.py
has five examples of how a conversation might start, as well as an example of a full conversation (only the user input)- Adding additional conversations to increase code coverage is preferred. Don't just stick with the provided conversations.
- By looking at the code, write down sample conversations that you consider should be implemented as tests in order to achieve a high degree of coverage of
- Implement the tests
- Using pytest or unittest, implement one or more of your full-conversation tests
- You will need to mock calls to
Classifier
andBooklineAPI
methods - methods for these classes are not implemented, so we need to mock them in order to perform the tests. You can read more about this here: https://docs.python.org/3/library/unittest.mock.html
- (Optional) Suggest improvements to the code of
WhatsappBot
- In case you see something that you consider could/should be improved, feel free to point it out!
This repository has a single folder src
with four files:
- api.py
- bot.py
- classifier.py
- main.py
Defines class BooklineAPI
with unimplemented public method insert_customer_email
.
Defines error InsertEmailError
.
Defines class Classifier
with unimplemented public method get_intent
.
Defines class WhatsappBot
with public method message
.
WhatsappBot
uses both Classifier
and BooklineAPI
.
WhatsappBot
also has several private methods used by message
.
Provides examples of how to use WhatsappBot
to manage conversations.
When Bookline receives a new Whatsapp message, it creates a WhatsappBot
instance to manage this new conversation.
A new WhatsappBot
starts with conversation_status = "start"
.
A conversation is handled by WhatsappBot
as a sequence of interactions.
Interactions are independent from each other, and the only variable that is stored from one interaction to the next
is the conversation_status
.
An interaction is passed to WhatsappBot
via a call to its message
method - by providing two strings: the user query
and the conversation motive. All interactions from the same conversation share the same motive.
According to the conversation motive, WhatsappBot.message
handles the user query with a proper conversation flow.
There are currently three conversation motives, each of them with its correspondent flow:
- newsletter - the conversation starts when a user replies to an automatic message from Bookline asking if they wish to receive updates via a newsletter subscription. If the user confirms their interest, the bot asks for an email address.
- ask_for_email - the conversation starts when a user replies to an automatic message from Bookline asking for an email address
- ask_for_card - this conversation flow is not implemented yet, so any conversation started with this motive triggers a hangup reply from the bot
WhatsappBot
provides a response to query in the form of a JSON
with an answer
(consisting of an id and a string) and a next action to perform.
This output will be then handled by another service.
The bot continues to process interactions until it decides to send out a hangup response.
At that time the WhatsappBot
is destroyed and the conversation ends.