-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.rb
84 lines (69 loc) · 2.22 KB
/
web.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
require 'mail'
require 'pp'
require 'sinatra'
require 'twitter'
require 'openssl'
require_relative 'helpers'
$stdout.sync = $stderr.sync = true
TOM_EMAIL = ENV.fetch("TOM_EMAIL")
MAILING_LISTS = ENV.fetch("MAILING_LISTS").split(',')
MAX_LEN = ENV.fetch("MAX_TWEET_LENGTH", 280)
twitter = Twitter::REST::Client.new do |config|
config.consumer_key = ENV.fetch('TWITTER_CONSUMER_KEY')
config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET')
config.access_token = ENV.fetch('TWITTER_ACCESS_TOKEN')
config.access_token_secret = ENV.fetch('TWITTER_ACCESS_TOKEN_SECRET')
end
def verify_msg(token, timestamp, signature)
signing_key = ENV.fetch('MAILGUN_API_KEY')
digest = OpenSSL::Digest::SHA256.new
data = [timestamp, token].join
signature == OpenSSL::HMAC.hexdigest(digest, signing_key, data)
end
post '/messages' do
token = params.fetch('token')
timestamp = params.fetch('timestamp')
signature = params.fetch('signature')
verified = verify_msg(token, timestamp, signature)
unless verified
halt 401
end
puts "received message:"
pp params
from = params.fetch('from')
headers = Hash[JSON.parse(params.fetch('message-headers'))]
list = headers['List-Id']
if from.include?(TOM_EMAIL) && !list.nil? && MAILING_LISTS.any? { |l| list.include?(l) }
body = params.fetch('body-plain')
last_sentence = find_last_sentence(body)
if last_sentence
len = last_sentence.length
if len <= MAX_LEN
puts "tweeting last sentence (#{len} chars)"
twitter.update(last_sentence)
status 200
else
puts "last sentence too long (#{len} chars; max is #{MAX_LEN}); skipping"
end
else
puts "could not extract last sentence"
end
end
end
post '/debug' do
puts "received message:"
puts params
token = params.fetch('token')
timestamp = params.fetch('timestamp')
signature = params.fetch('signature')
verified = verify_msg(token, timestamp, signature)
puts "message verifies: #{verified}"
puts "from: #{params.fetch('from')}"
headers = Hash[JSON.parse(params.fetch('message-headers'))]
puts headers
list = headers['List-Id']
puts "list is: #{list}"
body = params.fetch('body-plain')
puts "body is: #{body}"
status 200
end