-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
180 lines (149 loc) · 4.61 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require 'rubygems'
require 'sinatra'
require 'twilio-ruby'
require 'json/ext'
require 'mongoid'
# Create new app of type Sinatra
class MyApp < Sinatra::Base
# CORS
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => 'Content-Type'
end
set :protection, false
# Load mongoHQ credentials from the mongoid.yml file
Mongoid.load!("mongoid.yml")
# Create MongoDB collection
class Message
include Mongoid::Document
field :from
field :to
field :body
field :time, type: Time, default: ->{ Time.now }
end
class Contact
include Mongoid::Document
field :sender
field :receiver
field :nickname
end
# Twilio Ruby Gem Credentials
account_sid = "AC5e0e37adaf556c3ef136e9ba71536c74"
auth_token = "f5ca2a5df3edfbf3c1f81014d11de97b"
$client = Twilio::REST::Client.new account_sid, auth_token
$twilioNumber = "+17606421123" # Anthony's Twilio number
def create_contact(params)
# do not allow duplicates here
contact = Contact.new(
sender: params[:From][2..11],
receiver: params[:Body].split(" ")[1],
nickname: params[:Body].split(" ")[2].downcase
)
contact.upsert
end
def send_to_contact(params)
fromNumber = params[:From][2..11]
nickname = params[:Body].split(" ")[0].downcase
toNumber = Contact.where(sender: fromNumber, nickname: nickname[1..nickname.length-1]).first[:receiver]
body = params[:Body]
body.slice! /^\S+\s+/
send(toNumber, body)
save_message(toNumber, fromNumber, body)
end
def send_to_number(params)
fromNumber = params[:From][2..11]
body = params[:Body]
toNumber = extract_phone_number(params[:Body])
###### SEVERE ISSUE HERE ######
# This is only slicing up to the first space, it needs to slice up to the first
body.slice! /^\S+\s+/
send(toNumber, body)
save_message(toNumber, fromNumber, body)
end
def respond(params)
fromNumber = params[:From][2..11]
body = params[:Body]
#Find latest message sent to you
sender = Message.where(to: fromNumber).asc(:time).last
puts "fromNumber #{fromNumber}"
puts "sender: #{sender}"
if !sender.nil?
send(sender[:from], body)
save_message(sender[:from], fromNumber, body)
else
# This person doesn't know what they're doing
puts "========= BEGIN - RESPONSE FROM SOMEONE WHO DOESN'T KNOW WHAT'S GOING ON ==========="
puts fromNumber
puts body
puts "========= END - RESPONSE FROM SOMEONE WHO DOESN'T KNOW WHAT'S GOING ON ==========="
# return error to sender
end
end
def send(toNumber, body)
# Check to see if the body is just a URL, then send them the image if it is
if(body =~ URI::regexp)
# We're working with a URL
send_mms(toNumber, body)
else
# Great, we have a properly formed messaged. Send it
send_sms(toNumber, body)
end
end
def send_mms(toNumber, body)
$client.account.messages.create(
:from => $twilioNumber,
:to => toNumber,
:MediaUrl => body
)
end
def send_sms(toNumber, body)
$client.account.messages.create(
:from => $twilioNumber,
:to => toNumber,
:body => body
)
end
def save_message(toNumber, fromNumber, body)
Message.create(
to: toNumber,
from: fromNumber,
body: body
)
end
# create a duplicate of the body and send it into this method
def extract_phone_number(input)
firstCharIndex = input =~ /[a-zA-Z#]/
number = input[0..firstCharIndex-1]
return clean_phone_number(number)
end
def clean_phone_number(input)
if input.gsub(/\D/, "").match(/^1?(\d{3})(\d{3})(\d{4})/)
[$1, $2, $3].join()
end
end
# GET route at root. Messages sent here will be forwarded to specified
# destination number.
get '/' do
keyword = params[:Body].split(" ")[0]
# Make method calls based on the first keyword
if keyword.downcase == "setcontact"
create_contact(params)
elsif keyword[0] == '#'
send_to_contact(params)
elsif extract_phone_number(params[:Body]).length == 10
send_to_number(params)
else
respond(params)
puts "Keywork that broke shit: #{keyword}"
puts "Params that broke shit: #{params}"
end
end
# GET return all the messages
get '/messages/?' do
content_type :json
messages = Message.all
messages.to_json
end
end