require 'sinatra'
require "sinatra/reloader" if development?
require 'twilio-ruby'
require 'httparty'
require 'json'
require 'did_you_mean'
enable :sessions
configure :development do
require 'dotenv'
Dotenv.load
end
get "/" do
redirect to ("/about")
end
get "/about" do
"Hi, I am Cheng"
end
get "/sms/incoming" do
session[:counter] ||= 0
count = session[:counter]
sender = params[:From] || ""
body = params[:Body] || ""
# session["last_intent"] ||= nil
# session["users_name"] ||= nil
message = determine_response body
# Build a twilio response object
twiml = Twilio::TwiML::MessagingResponse.new do |r|
r.message do |m|
# add the text of the response
m.body( message )
end
end
# increment the session counter
session[:counter] += 1
# send a response to twilio
content_type 'text/xml'
twiml.to_s
end
get "/test/sms" do
client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]
#\n means add the words into the new line (\n\n means adding two lines)
message = "Hello, I am Cheng\nIt works!"
# this will send a message from any end point
client.api.account.messages.create(
from: ENV["TWILIO_FROM"],
to: ENV["TEST_NUMBER"],
body: message
)
"Test Message Sent"
end
GREETINGS = ["Hello", "Hi", "Hey", "What's up", "Good to see you", "Hey there"]
def determine_response body
q = body.to_s.downcase.strip
if q == "hi" or q == "hello" or q == "hey"
# session["last_intent"] = "hello"
send_sms_message_to get_greeting + "👋, I am Cheng"
sleep(3)
send_sms_message_to "An architecture and computation designer 👨💻"
sleep(3)
message = "Do you want to know more? or get in touch?"
elsif q.include? "more"
# session["last_intent"] = "more"
send_sms_message_to "Well, I am a graduate student from CMU 👨🎓"
sleep(3)
send_sms_message_to "My program is master of architecture"
sleep(3)
message = "Are you a student? or working already?"
elsif q.include? "work" or q.include? "student"
# session["last_intent"] = "ask_if_designer"
send_sms_message_to "That's cool! It's nice to meet you! 😊"
sleep(3)
message = "Are you a designer? or you just feel interested?"
#elsif session["last_intent"] == "ask_if_designer"
#if q.include? "design"
#elsif q.include? "interested"
#else
#end
elsif q.include? "design"
send_sms_message_to "Wow! I thought so...😊"
sleep(3)
send_sms_message_to "I am willing to talk to designers"
sleep(3)
send_sms_message_to "I am passionate about architecture design as well as UX&UI design"
sleep(3)
message = "Do you design the same or different?"
elsif q.include? "interest"
send_sms_message_to "Interesting...I really appreciate your interests in design though"
sleep(3)
send_sms_message_to "I guess you might be interested in my projects ✍️"
sleep(3)
message = "Do you want to see my projects? or use email to get in touch? 📧"
elsif q.include? "same"
send_sms_message_to "Cheers! I am glad to hear that..."
sleep(3)
send_sms_message_to "Actually, I think you might be interested in my projects ✍️"
sleep(3)
message = "Do you want to see my projects? or use email to get in touch? 📧"
elsif q.include? "different"
send_sms_message_to "Nice! Design is a big family! We are all part of it"
sleep(3)
send_sms_message_to "Actually, I think you might be interested in my projects ✍️"
sleep(3)
message = "Do you want to see my projects? or use email to get in touch? 📧"
elsif q.include? "project"
send_sms_message_to "Nice! I studied B.Arch in Zhejiang Sci-Tech University and M.Arch in Carnegie Mellon University"
sleep(3)
message = "Do you want to see my projects of bachelor degree? or master degree?"
elsif q.include? "master"
send_sms_message_to "Well, there exists one M.Arch project 1️⃣"
sleep(3)
send_sms_message_to "5.Environment, form and feedback"
sleep(3)
message = "You can type the number to see the details or you can type bachelor to see the others"
elsif q.include? "bachelor"
send_sms_message_to "Well, there exist four B.Arch projects 4️⃣"
sleep(3)
send_sms_message_to "1.Inspiration Palace\n" + "2.Truemanism\n" + "3.On the Wing\n" + "4.Between the Wall"
sleep(3)
message = "You can type the number to see the details or you can type master to see the others"
elsif q.include? "5"
send_sms_message_to "Here is the link:\n" + "https://www.behance.net/gallery/91462971/Environment-Form-Feedback"
sleep(3)
message = "Do you want to see other bachelor's projects? or contact with me? ✉️"
elsif q.include? "1"
send_sms_message_to "Here is the link:\n" + "https://www.behance.net/gallery/91463111/Inspiration-Palace"
sleep(3)
message = "Do you want to see other master's projects? or contact with me? ✉️"
elsif q.include? "2"
send_sms_message_to "Here is the link:\n" + "https://www.behance.net/gallery/91463147/Truemanism"
sleep(3)
message = "Do you want to see other master's projects? or contact with me? ✉️"
elsif q.include? "3"
send_sms_message_to "Here is the link:\n" + "https://www.behance.net/gallery/91463363/On-the-Wing"
sleep(3)
message = "Do you want to see other master's projects? or contact with me? ✉️"
elsif q.include? "4"
send_sms_message_to "Here is the link:\n" + "https://www.behance.net/gallery/91463221/Between-the-Wall"
sleep(3)
message = "Do you want to see other master's projects? or contact with me? ✉️"
elsif q.include? "contact" or q.include? "email" or q.include? "touch"
send_sms_message_to "That's awesome! 🙌"
sleep(3)
message = "Do you want to ask me a question? or talk about the cooperation? 🤝"
elsif q.include? "cooperation" or q.include? "question"
send_sms_message_to "Great! I am looking forward to it! 😊"
sleep(3)
send_sms_message_to "Here is my email: czhou2@andrew.cmu.edu"
sleep(3)
message = "I will respond to you asap!"
else
# Sending unexpected answer to the Slack Channel
send_to_slack q
send_sms_message_to "Sorry 😢, " + error_response
sleep(3)
message = "Could you type your choice again? or say hello to start the conversation"
end
message
end
def send_sms_message_to message_body
client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]
# this will send a message from any end point
client.api.account.messages.create(
from: ENV["TWILIO_FROM"],
to: ENV["TEST_NUMBER"],
body: message_body
)
end
def error_response
error_prompt = ["I didn't catch that", "Hmmm I don't know that", "I am confused with that"]
return error_prompt.sample
end
def get_greeting
return GREETINGS.sample
end
def send_to_slack message
# how to use HTTParty
# response = HTTParty.get('http://myurl.com/path/to/resouce')
# puts response.body, response.code, response.message, response.headers.inspect
# response = HTTParty.post('http://myurl.com/path/to/resouce')
slack_webhook = ENV['SLACK_WEBHOOK']
formatted_message = "*Recently Received:*\n"
formatted_message += "#{message} "
HTTParty.post slack_webhook, body: {text: formatted_message.to_s, username: "ChengBot" }.to_json, headers: {'content-type' => 'application/json'}
end
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .