49714 Programming for Online Prototypes
· 9 members
A hands on introduction to building online products and services through code
Diet bot is an integrated SMS conversational bot and Alexa skill. It can help users track their meals and log nutrition values for them.
People keep to a diet for different reasons, such as losing weight, gaining weight or just keeping a healthy lifestyle. Furthermore, some people are trying to be more accurate in controlling how much food they have each day, i.e. recording and controlling the calories of their food. However, even with advanced calorie calculators, it's hard for people to stick to the plan and keep the habit of controlling their diet.
The intention of the bot is to basically serve as a kind of personal assistant to help users stick to the plan, nudge them when necessary and most fundamentally provide a friendly interface for people to track their calories.
There is a similar web-based product called myfitnesspal. It asks for users' body stats and fitness goals when they signed up. But due to all the clicks and typing needed to track fitness plans, it's not that easy to use, as compared to a zero UI SMS bot or Alexa skill. That's how my idea came into being, why not enable people to just say their meals to a bot or talk to a bot to record their meals? Besides the convenience of doing that, it would also feel like they are talking to an assistant, which will more likely help them persist through their plans.
The diet bot is not supposed to be a one-time conversation bot, i.e. it has some sort of memory like what the user has eaten in the day. So after testing the initial version with a friend, where the bot would only tell users how to use it in a simple paragraph, I found that they still don't have a clear sense of what it looks like to interact with and make use of the full features of the bot. So I integrated a tutorial feature into SMS to lead first-time users through a typical day of using the bot.
I've been considering if I want to build the product in Alexa skills or SMS bot, until later I realized why not have both and build the most suitable feature into each one. Therefore, I built the tutorial feature in SMS where users would be able to track the messages more easily. Whereas for more frequent users, justing talking with Alexa to do some routine work would be preferable to typing it in the chat. So I built the "log meal" feature in both SMS and Alexa and have them working on the same file.
Below you can find the code to call Nutritionix API and process the response. Basically what the API does is if I send it a sentence saying "I had two eggs and some salad for breakfast", it will return all food detected in that sentence and the corresponding nutrition value. What I did was to extract the calories, protein and fat value of the said food, then log it and return that message to the user.
def get_nutrients body
url = 'https://trackapi.nutritionix.com/v2/natural/nutrients'
puts body
res = HTTParty.post url, body: { query:body, timezone: "US/Eastern"}.to_json, headers: {'content-type' => 'application/json', 'x-app-id' => ENV['NUTRITIONIX_ID'], 'x-app-key' => ENV['NUTRITIONIX_KEY']}
total_fat = 0
total_protein = 0
total_cal = 0
if res['foods'].nil?
msg = "I can't seem to recognize any food in what you said. Can you be more specific please?"
session[:last_msg] = "further_log_meal"
return msg
else
res['foods'].each do |food|
total_fat += food['nf_total_fat']
total_protein += food['nf_protein']
total_cal += food['nf_calories']
end
#save changes to json file
# update_log total_cal, total_protein, total_fat
total_fat = total_fat.to_i
total_protein = total_protein.to_i
total_cal = total_cal.to_i
msg = "You consumed approximately #{total_cal} calories."
return msg, total_cal, total_protein, total_fat
end
end
Click to Expand
def get_summary
#<break time='130ms'/>
motivate_quotes = ['The struggle you are in today is developing the strength you need for tomorrow🙂',
'The road may be bumpy but stay committed to the process🙂',
'If you are tired of starting over, stop giving up🙂',
'It’s not a diet, it’s a lifestyle change🙂',
'Will is a skill🙂',
'Stressed spelled backwards is desserts. coincidence? I think not!🙃',
'Strive for progress, not perfection💪',
'Success is never certain, failure is never final🙂',
'A goal without a plan is just a wish🙂']
obj = JSON.parse(IO.read('food_log.json'))
cal_sum = obj['cal_sum'].to_f
cal_limit = obj['cal_limit'].to_f
dif = (cal_limit - cal_sum)/cal_limit
if dif < -0.25
message = "You've exceeded your daily calorie limit a lot! \n"+ motivate_quotes.sample + "\nDon't worry, I'm here for you"
elsif dif < 0
message = "You've run out of your calorie limit for the day. Don't forget your plans~"
elsif dif < 0.15
message = "You have less than 300 calories left for your daily limit. That's about the amount of a light breakfast.🥪"
elsif dif < 0.25
message = "You have less than 500 calories left for your daily limit. That's about a hamburger, but I'm not suggesting you to eat that!😂"
else
message = "You have more than 500 calories left for your daily limit. That's more than a proper meal. Go ahead and enjoy the food!😀"
end
return message
end
Click to Expand
I would say what I learned most from the project is that you need to have a well iterated and thought-through design of your product before you start to implement it. Constantly changing the design while you have implemented some of them is time-consuming and contrary to what we hope to achieve, i.e. "rapid" prototyping.
Another point is that API is really powerful. I can imagine how wielding the power could scaffold the implementation of products and the possibilities it creates. I'll keep an eye on it when making other products later and I'm sure I can find some surprises.
A hands on introduction to building online products and services through code
Diet bot is an integrated SMS conversational bot and Alexa skill. It can help users track their meals and log nutrition values for them.
May 11th, 2020