This repository has been archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
venmo.py
800 lines (751 loc) · 33.5 KB
/
venmo.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
import config
from flask import Flask, request, send_from_directory
import ConfigParser
import requests
import datetime
import pytz
import sys
import json
from pymongo import MongoClient
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
if 'code' in request.args:
return send_from_directory('', 'code.html')
else:
return send_from_directory('', 'index.html')
@app.route('/js/<path:filename>', methods=['GET'])
def serve_js(filename):
return send_from_directory('js', filename)
@app.route('/css/<path:filename>', methods=['GET'])
def serve_css(filename):
return send_from_directory('css', filename)
@app.route('/', methods=['POST'])
def process():
load_config()
user_id = request.values.get('user_id')
message = request.values.get('text')
response_url = request.values.get('response_url')
token = request.values.get('token')
team_id = request.values.get('team_id')
if team_id not in config.workspaces['workspaces']:
return str('Team not configured to use Venmo')
verification_token = config.workspaces['workspaces'][team_id]['token']
if token != verification_token:
return str('Team verification token mismatch')
split_message = message.split()
if len(split_message) > 0:
if split_message[0].lower() == 'code':
if len(split_message) == 1:
help(response_url)
else:
complete_auth(split_message[1], user_id, team_id, response_url)
return str('')
access_token = get_access_token(user_id, team_id, response_url)
if access_token != None:
if access_token == 'expired':
respond('Access token is expired, Sanders needs to debug this. So go bother him or something.', response_url)
else:
venmo_id = _get_venmo_id(access_token)
if venmo_id != '':
parse_message('venmo ' + message, access_token, user_id, team_id, venmo_id, response_url)
return str('')
@app.route('/webhook', methods=['GET'])
def webhook_get():
venmo_challenge = request.args.get('venmo_challenge')
return str(venmo_challenge)
@app.route('/webhook', methods=['POST'])
def webhook():
load_config()
data = request.get_json()
dbs = connect_to_mongo_dbs()
user = None
message = ''
if data['type'] == 'payment.created':
(db, user) = _get_user_from_dbs(dbs, data['data']['target']['user']['id'])
if user is None:
return str('')
if _webhook_seen(db, user, data['data']['id']):
return str('')
_save_webhook_id(db, user, data['data']['id'])
message += data['data']['actor']['display_name'] + ' '
if data['data']['action'] == 'pay':
message += 'paid you '
elif data['data']['action'] == 'charge':
message += 'charged you '
message += '$' + '{:0,.2f}'.format(data['data']['amount']) + ' '
message += 'for ' + data['data']['note']
if data['data']['action'] == 'charge':
message += ' | ID: ' + data['data']['id']
send_slack_message(message, user)
if data['data']['action'] == 'charge':
accept_command = '/venmo complete accept ' + data['data']['id']
send_slack_message(accept_command, user)
elif data['type'] == 'payment.updated':
if data['data']['target']['type'] != 'user':
return str('')
(db, user) = _get_user_from_dbs(dbs, data['data']['actor']['id'])
if user is None:
return str('')
if _webhook_seen(db, user, data['data']['id']):
return str('')
_save_webhook_id(db, user, data['data']['id'])
message += data['data']['target']['user']['display_name'] + ' '
if data['data']['status'] == 'settled':
message += 'accepted your '
elif data['data']['status'] == 'cancelled':
message += 'rejected your '
message += '$' + '{:0,.2f}'.format(data['data']['amount']) + ' charge '
message += 'for ' + data['data']['note']
send_slack_message(message, user)
return str('')
# returns tuple of (db, user_id) with db that user is located in, returns None if user was not found
def _get_user_from_dbs(dbs, venmo_user_id):
for workspace in config.workspaces['workspaces']:
db_name = config.credentials.get("Mongo", "database") + "_" + workspace
db = dbs[db_name]
users = list(db.users.find())
for user in users:
if user['venmo']['id'] == venmo_user_id:
return (db, user['_id'])
return None
def send_slack_message(message, channel):
bot_token = config.credentials.get('Slack', 'bot-token')
o = {}
o['token'] = bot_token
o['channel'] = channel
o['text'] = message
o['username'] = 'Venmo'
o['icon_url'] = 'https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2015-11-10/14228813844_49fae5f9cad227c8c1b5_72.jpg'
response = requests.post('https://slack.com/api/chat.postMessage', data=o)
def respond(message, response_url):
o = {}
o['text'] = message
response = requests.post(response_url, json=o)
# Connects to mongo and returns a MongoClient
def connect_to_mongo(team_id):
client = connect_to_mongo_dbs()
db = config.credentials.get("Mongo", "database")
db += "_" + team_id
return client[db]
# Connects to Mongo and returns a MongoClient NOT connected to any databases
def connect_to_mongo_dbs():
host = config.credentials.get("Mongo", "connection")
user = config.credentials.get("Mongo", "user")
password = config.credentials.get("Mongo", "password")
connection_url = "mongodb://" + user + ":" + password + "@" + host + "/"
client = MongoClient(connection_url)
return client
def update_database(user_id, db, access_token, expires_date, refresh_token):
return db.users.update_one({'_id': user_id},
{'$set': {
'venmo.access_token': access_token,
'venmo.expires_in': expires_date,
'venmo.refresh_token': refresh_token
},
'$currentDate': {'lastModified': True}
})
def update_database(user_id, db, access_token, expires_date, refresh_token, new_id):
return db.users.update_one({'_id': user_id},
{'$set': {
'venmo': {
'access_token': access_token,
'expires_in': expires_date,
'refresh_token': refresh_token,
'id': new_id
}
},
'$currentDate': {'lastModified': True}
})
def get_access_token(user_id, team_id, response_url):
db = connect_to_mongo(team_id)
venmo_auth = db.users.find_one({'_id': user_id}, {'venmo': 1})
if venmo_auth == None or 'venmo' not in venmo_auth or venmo_auth['venmo'] == {} or venmo_auth['venmo']['access_token'] == '':
user_doc = db.users.find_one({'_id': user_id})
if user_doc == None:
create_user_doc = db.users.insert_one({'_id': user_id})
create_venmo_auth = update_database(user_id, db, '', '', '', '')
request_auth(config.credentials, response_url)
return None
else:
expires_date = venmo_auth['venmo']['expires_in'].replace(tzinfo = pytz.utc)
if expires_date < datetime.datetime.utcnow().replace(tzinfo = pytz.utc):
post_data = {
'client_id': config.credentials.get('Venmo', 'clientId'),
'client_secret': config.credentials.get('Venmo', 'clientSecret'),
'refresh_token': venmo_auth['venmo']['refresh_token']
}
response = requests.post('https://api.venmo.com/v1/oauth/access_token', post_data)
if response.status_code == 400:
update_database(user_id, db, '', '', '', '')
request_auth(config.credentials, response_url)
return None
response_dict = response.json()
access_token = response_dict['access_token']
expires_in = response_dict['expires_in']
expires_date = (datetime.datetime.utcnow().replace(tzinfo = pytz.utc) + datetime.timedelta(seconds=expires_in))
update_database(user_id, db, access_token, expires_date, response_dict['refresh_token'])
return access_token
return venmo_auth['venmo']['access_token']
def request_auth(config, response_url):
auth_url = 'https://api.venmo.com/v1/oauth/authorize?client_id=' + config.get('Venmo', 'clientId') + '&scope=make_payments%20access_payment_history%20access_feed%20access_profile%20access_email%20access_phone%20access_balance%20access_friends&response_type=code'
url_message = ('Authenticate to Venmo with the following URL: ' + auth_url + ' then send back the auth code in this format\n'
'venmo code CODE')
respond(url_message, response_url)
def complete_auth(code, user_id, team_id, response_url):
db = connect_to_mongo(team_id)
post_data = {
'client_id': config.credentials.get('Venmo', 'clientId'),
'client_secret': config.credentials.get('Venmo', 'clientSecret'),
'code': code
}
response = requests.post('https://api.venmo.com/v1/oauth/access_token', post_data)
response_dict = response.json()
access_token = response_dict['access_token']
expires_in = response_dict['expires_in']
expires_date = (datetime.datetime.utcnow().replace(tzinfo = pytz.utc) + datetime.timedelta(seconds=expires_in))
refresh_token = response_dict['refresh_token']
id = response_dict['user']['id']
update_access_token = update_database(user_id, db, access_token, expires_date, refresh_token, id)
respond('Authentication complete!', response_url)
def _save_webhook_id(db, user_id, hook_id):
db.users.update_one({'_id': user_id},
{'$set': {
'lastWebhook': hook_id
},
'$currentDate': {'lastModified': True}
}
)
def _webhook_seen(db, user_id, hook_id):
user = db.users.find_one({'_id': user_id})
if 'lastWebhook' in user:
last_webhook = user['lastWebhook']
if hook_id == last_webhook:
return True
return False
def _get_venmo_id(access_token):
response = requests.get('https://api.venmo.com/v1/me?access_token=' + access_token)
response_dict = response.json()
if 'error' in response_dict:
venmo_error(response_dict['error'], response_url)
return ''
return response_dict['data']['user']['id']
def _get_pagination(initial, access_token):
final_list = []
while True:
final_list += initial['data']
if not initial['pagination'] or initial['pagination']['next'] == None:
break
else:
response = requests.get(initial['pagination']['next'] + '&access_token=' + access_token)
response_dict = response.json()
if 'error' in response_dict:
venmo_error(response_dict['error'], response_url)
return []
initial = response_dict
return final_list
def _find_friend(list, username):
for friend in list:
if friend['username'].lower() == username.lower():
return friend['id']
return None
def get_venmo_balance(access_token, response_url):
response = requests.get('https://api.venmo.com/v1/me?access_token=' + access_token)
response_dict = response.json()
if 'error' in response_dict:
venmo_error(response_dict['error'], response_url)
return
respond(response_dict['data']['balance'], response_url)
def _get_friends_count(venmo_id, access_token, response_url):
payload = {'access_token': access_token}
user_response = requests.get('https://api.venmo.com/v1/users/' + venmo_id, params=payload)
user_response_dict = user_response.json()
if 'error' in user_response_dict:
venmo_error(user_response_dict['error'], response_url)
return
return user_response_dict['data']['friends_count']
def _get_friends(venmo_id, access_token, response_url):
friends_count = _get_friends_count(venmo_id, access_token, response_url)
payload = {
'limit': friends_count,
'access_token': access_token
}
friends_response = requests.get('https://api.venmo.com/v1/users/' + venmo_id + '/friends', params=payload)
friends_response_dict = friends_response.json()
if 'error' in friends_response_dict:
venmo_error(friends_response_dict['error'], response_url)
return []
return friends_response_dict['data']
def _calculate_total(amount_str_array, response_url):
previous_number = None
current_sign = None
current_number = None
while len(amount_str_array) > 1:
for i in range(len(amount_str_array)):
copy = amount_str_array[i]
if copy.startswith('$'):
copy = copy[1:]
if copy == '+' or copy == '-' or copy == '*' or copy == '/':
if current_sign is None:
if previous_number is None:
parse_error('Invalid arithmetic string', response_url)
return None
current_sign = copy
else:
parse_error('Invalid arithmetic string', response_url)
return None
elif previous_number is None:
try:
previous_number = float(copy)
except:
parse_error('Could not parse ' + copy + ' into number', response_url)
return None
elif current_number is None:
try:
current_number = float(copy)
except:
parse_error('Could not parse ' + copy + ' into number', response_url)
return None
try:
result = _mathify(previous_number, current_sign, current_number)
result = str(result)
amount_str_array[i] = result
modifying_i = i
del amount_str_array[modifying_i - 1]
modifying_i -= 1
del amount_str_array[modifying_i - 1]
previous_number = None
current_sign = None
current_number = None
break
except:
parse_error('Invalid arithmetic string', response_url)
return None
try:
if amount_str_array[0].startswith('$'):
amount_str_array[0] = amount_str_array[0][1:]
final = float(amount_str_array[0])
final = round(final, 2)
return final
except:
parse_error('Could not calculate total', response_url)
return None
def _mathify(num1, sign, num2):
if num1 is None or sign is None or num2 is None:
raise ArithmeticError('A argument is None')
if sign == '+':
return num1 + num2
elif sign == '-':
return num1 - num2
elif sign == '*':
return num1 * num2
elif sign == '/':
return num1 / num2
else:
raise ArithmeticError('Unknown sign')
def venmo_payment(audience, which, amount, note, recipients, access_token, venmo_id, user_id, team_id, response_url):
url = 'https://api.venmo.com/v1/payments'
amount_str = str(amount)
if which == 'charge':
amount_str = '-' + amount_str
full = None
final_message = ''
for r in recipients:
post_data = {
'access_token': access_token
}
if r.startswith('phone:'):
id = r[6:]
post_data['phone'] = id
elif r.startswith('email:'):
id = r[6:]
post_data['email'] = id
else:
id = _check_alias(user_id, team_id, r)
if id is None:
id = _check_cache(user_id, team_id, r)
if id is None:
if full is None:
full = _get_friends(venmo_id, access_token, response_url)
id = _find_friend(full, r)
if id is not None:
_add_to_cache(user_id, team_id, r, id)
if id is None:
parse_error('You are not friends with ' + r, response_url)
continue
post_data['user_id'] = id
post_data['note'] = note
post_data['amount'] = amount_str
post_data['audience'] = audience
response = requests.post(url, post_data)
response_dict = response.json()
if 'error' in response_dict:
final_message += response_dict['error']['message'] + '\n'
else:
name = ''
target = response_dict['data']['payment']['target']
if target['type'] == 'user':
name = target['user']['display_name']
elif target['type'] == 'phone':
name = target['phone']
elif target['type'] == 'email':
name = target['email']
if amount_str.startswith('-'):
final_message += 'Successfully charged ' + name + ' $' + '{:0,.2f}'.format(response_dict['data']['payment']['amount']) + ' for ' + response_dict['data']['payment']['note'] + '. Audience is ' + audience + '.\n'
else:
final_message += 'Successfully paid ' + name + ' $' + '{:0,.2f}'.format(response_dict['data']['payment']['amount']) + ' for ' + response_dict['data']['payment']['note'] + '. Audience is ' + audience + '.\n'
respond(final_message, response_url)
def _add_to_cache(user_id, team_id, id, venmo_id):
db = connect_to_mongo(team_id)
user = db.users.find_one({'_id': user_id})
db.users.update_one({'_id': user_id},
{'$set': {
'cache.' + id: {'id': venmo_id}
},
'$currentDate': {'lastModified': True}
})
return
def _check_cache(user_id, team_id, id):
db = connect_to_mongo(team_id)
user = db.users.find_one({'_id': user_id})
if 'cache' in user:
cache = user['cache']
if id in cache:
return cache[id]['id']
return None
def alias_user(user_id, team_id, id, alias, venmo_id, access_token, response_url):
friends = _get_friends(venmo_id, access_token, response_url)
friend_id = _find_friend(friends, id)
if friend_id == None:
parse_error('You are not friends with ' + id, response_url)
return
db = connect_to_mongo(team_id)
user = db.users.find_one({'_id': user_id})
db.users.update_one({'_id': user_id},
{'$set': {
'alias.' + alias: {'username': id, 'id': friend_id}
},
'$currentDate': {'lastModified': True}
})
respond('Alias set!', response_url)
return
def _get_alias(user_id, team_id, alias):
db = connect_to_mongo(team_id)
user_doc = db.users.find_one({'_id': user_id})
if 'alias' in user_doc:
aliases = user_doc['alias']
if alias in aliases:
return aliases[alias]
return None
def _check_alias(user_id, team_id, alias):
alias_obj = _get_alias(user_id, team_id, alias)
if alias_obj is not None:
return alias_obj['id']
else:
return None
def list_aliases(user_id, team_id, response_url):
db = connect_to_mongo(team_id)
user = db.users.find_one({'_id': user_id})
if 'alias' in user:
alias_list = ''
for alias in user['alias'].keys():
alias_list += alias + ' points to ' + user['alias'][alias]['username'] + '\n'
respond(alias_list, response_url)
return
else:
respond('You have no aliases set', response_url)
return
def delete_alias(user_id, team_id, alias, response_url):
alias_obj = _get_alias(user_id, team_id, alias)
if alias_obj is not None:
db = connect_to_mongo(team_id)
db.users.update_one({'_id': user_id},
{'$unset': {
'alias.' + alias: 1
},
'$currentDate': {'lastModified': True}
}
)
respond('Alias deleted!', response_url)
else:
respond('That alias does not exist', response_url)
def save_last_message(user_id, team_id, message):
db = connect_to_mongo(team_id)
db.users.update_one({'_id': user_id},
{'$set': {
'last': message
},
'$currentDate': {'lastModified': True}
}
)
def get_last_message(user_id, team_id, response_url):
db = connect_to_mongo(team_id)
user = db.users.find_one({'_id': user_id})
if 'last' in user:
respond('/' + user['last'], response_url)
else:
respond('No last message', response_url)
def venmo_pending(which, access_token, venmo_id, response_url):
message = ''
url = 'https://api.venmo.com/v1/payments?access_token=' + access_token + '&status=pending'
response = requests.get(url)
response_dict = response.json()
if 'error' in response_dict:
venmo_error(response_dict['error'], response_url)
return
full = _get_pagination(response_dict, access_token)
for pending in full:
if which == 'to':
if pending['actor']['id'] != venmo_id:
message += pending['actor']['display_name'] + ' requests $' + '{:0,.2f}'.format(pending['amount']) + ' for ' + pending['note'] + ' | ID: ' + pending['id'] + '\n'
elif which == 'from':
if pending['actor']['id'] == venmo_id:
if pending['target']['type'] == 'user':
message += pending['target']['user']['display_name'] + ' owes you $' + '{:0,.2f}'.format(pending['amount']) + ' ' + pending['note'] + ' | ID: ' + pending['id'] + '\n'
if message != '':
respond(message[0:-1], response_url)
else:
respond('No pending Venmos', response_url)
def venmo_complete_all(which, access_token, venmo_id, response_url):
action = ''
if which == 'accept':
action = 'approve'
elif which == 'reject':
action = 'deny'
elif which == 'cancel':
action = 'cancel'
pending_url = 'https://api.venmo.com/v1/payments?access_token=' + access_token + '&status=pending'
pending_response = requests.get(pending_url)
pending_response_dict = pending_response.json()
if 'error' in pending_response_dict:
venmo_error(pending_response_dict['error'], response_url)
return
full = _get_pagination(pending_response_dict, access_token)
pending_ids = []
for pending in full:
if action == 'approve' or action == 'deny':
if pending['actor']['id'] != venmo_id:
pending_ids.append(pending['id'])
elif action == 'cancel':
if pending['actor']['id'] == venmo_id:
pending_ids.append(pending['id'])
if len(pending_ids) == 0:
if action == 'approve':
respond('No Venmos to accept', response_url)
elif action == 'deny':
respond('No Venmos to reject', response_url)
elif action == 'cancel':
respond('No Venmos to cancel', response_url)
return
for number in pending_ids:
complete_url = 'https://api.venmo.com/v1/payments/' + str(number)
put_data = {
'access_token': access_token,
'action': action
}
complete_response = requests.put(complete_url, put_data)
complete_response_dict = complete_response.json()
if 'error' in complete_response_dict:
venmo_error(complete_response_dict['error'], response_url)
continue
if action == 'approve':
respond('All Venmos accepted!', response_url)
elif action == 'deny':
respond('All Venmos rejected!', response_url)
elif action == 'cancel':
respond('All Venmos canceled!', response_url)
def venmo_complete(which, completion_numbers, access_token, venmo_id, response_url):
for str_number in completion_numbers:
number = -1
try:
number = int(str_number)
except:
parse_error('Payment completion number, ' + str_number + ', must be a number', response_url)
continue
url = 'https://api.venmo.com/v1/payments/' + str(number)
action = ''
if which == 'accept':
action = 'approve'
elif which == 'reject':
action = 'deny'
elif which == 'cancel':
action = 'cancel'
check_url = 'https://api.venmo.com/v1/payments/' + str(number) + '?access_token=' + access_token
check_response = requests.get(check_url)
check_response_dict = check_response.json()
if 'error' in check_response_dict:
venmo_error(check_response_dict['error'], response_url)
continue
if check_response_dict['data']['actor']['id'] != venmo_id:
if action == 'cancel':
parse_error(check_response_dict['data']['actor']['display_name'] + ' requested $' + '{:0,.2f}'.format(check_response_dict['data']['amount']) + ' for ' + check_response_dict['data']['note'] + '. You cannot cancel it!', response_url)
continue
else:
if action == 'approve' or action == 'deny':
parse_error('You requested $' + '{:0,.2f}'.format(check_response_dict['data']['amount']) + ' for ' + check_response_dict['data']['note'] + '. You can try venmo complete cancel ' + str(number) + " if you don't want to be paid back.", response_url)
continue
put_data = {
'access_token': access_token,
'action': action
}
response = requests.put(url, put_data)
response_dict = response.json()
if 'error' in response_dict:
venmo_error(response_dict['error'], response_url)
continue
if action == 'approve':
respond('Venmo completed!', response_url)
elif action == 'deny':
respond('Venmo rejected!', response_url)
elif action == 'cancel':
respond('Venmo canceled!', response_url)
def help(response_url):
message = ('Venmo help\n'
'Commands:\n'
'venmo balance\n'
' returns your Venmo balance\n'
'venmo last\n'
' returns your last command\n'
'venmo (audience) pay/charge amount for note to recipients\n'
' example: venmo public charge $10.00 for lunch to testuser phone:5555555555 email:example@example.com\n'
' supports basic arithmetic, does not follow order of operations or support parenthesis\n'
' example: venmo charge 20 + 40 / 3 for brunch to a_user boss phone:5556667777\n'
' this would charge $20 NOT $33.33 to each user in the recipients list\n'
' audience (optional) = public OR friends OR private\n'
' defaults to private if omitted\n'
' pay/charge = pay OR charge\n'
' amount = Venmo amount\n'
' note = Venmo message\n'
' recipients = list of recipients, can specify Venmo username, phone number prefixed with phone: or email prefixed with email:\n'
'venmo alias id alias\n'
' example: venmo alias 4u$3r1d sam\n'
' set an alias for a Venmo username\n'
' id = Venmo username\n'
' alias = the alias for that user, must not contain spaces\n'
'venmo alias list\n'
' list all aliases\n'
'venmo alias delete alias\n'
' example: venmo alias delete sam\n'
' delete an alias\n'
' alias = the alias for that user, must not contain spaces\n'
'venmo pending (to OR from)\n'
' returns pending venmo charges, defaults to to\n'
' also returns ID for payment completion\n'
'venmo complete accept/reject/cancel number(s)/all\n'
' accept OR reject pending incoming Venmos with the given IDs\n'
' cancel pending outgoing Venmos with the given IDs\n'
'venmo code code\n'
' code = Venmo authentication code\n'
'venmo help\n'
' this help message')
respond(message, response_url)
def venmo_error(dict, response_url):
respond(dict['message'], response_url)
def parse_error(error_message, response_url):
respond(error_message, response_url)
def _find_str_in_list(list, str):
for i in range(len(list)):
if list[i].lower() == str.lower():
return i
return -1
def _find_last_str_in_list(list, str):
index = -1
for i in range(len(list)):
if list[i].lower() == str.lower():
index = i
return index
def parse_message(message, access_token, user_id, team_id, venmo_id, response_url):
split_message = message.split()
if len(split_message) == 1:
help(response_url)
elif split_message[1].lower() == 'help':
help(response_url)
elif split_message[1].lower() == 'last':
get_last_message(user_id, team_id, response_url)
elif split_message[1].lower() == 'code':
complete_auth(split_message[2], user_id, team_id, response_url)
else:
save_last_message(user_id, team_id, message)
if split_message[1].lower() == 'balance':
get_venmo_balance(access_token, response_url)
elif split_message[1].lower() == 'pending':
if len(split_message) == 2:
venmo_pending('to', access_token, venmo_id, response_url)
elif len(split_message) == 3:
which = split_message[2].lower()
if which == 'to' or which == 'from':
venmo_pending(which, access_token, venmo_id, response_url)
else:
parse_error('Valid pending commands\npending\npending to\npending from', response_url)
else:
parse_error('Valid pending commands\npending\npending to\npending from', response_url)
elif split_message[1].lower() == 'accept' or split_message[1].lower() == 'reject' or split_message[1].lower() == 'cancel':
if len(split_message) == 3:
error_string = 'You probably meant: /venmo complete ' + split_message[1].lower() + ' ' + split_message[2].lower()
parse_error(error_string, response_url)
elif split_message[1].lower() == 'complete':
if len(split_message) >= 4:
which = split_message[2].lower()
if which == 'accept' or which == 'reject' or which == 'cancel':
if split_message[3].lower() == 'all':
venmo_complete_all(which, access_token, venmo_id, response_url)
else:
completion_numbers = split_message[3:]
venmo_complete(which, completion_numbers, access_token, venmo_id, response_url)
else:
parse_error('Valid complete commands\nvenmo complete accept #\nvenmo complete reject #\nvenmo complete cancel #', response_url)
else:
parse_error('Valid complete commands\nvenmo complete accept #\nvenmo complete reject #\nvenmo complete cancel #', response_url)
elif split_message[1].lower() == 'alias':
if len(split_message) == 4:
if split_message[2].lower() == 'delete':
delete_alias(user_id, team_id, split_message[3].lower(), response_url)
else:
id = split_message[2]
alias = split_message[3].lower()
alias_user(user_id, team_id, id, alias, venmo_id, access_token, response_url)
elif len(split_message) == 3 and split_message[2].lower() == 'list':
list_aliases(user_id, team_id, response_url)
else:
parse_error('Invalid alias command, your alias probably has a space in it', response_url)
elif len(split_message) <= 2:
parse_error('Invalid payment string', response_url)
elif (split_message[1].lower() == 'charge' or split_message[2].lower() == 'charge' or
split_message[1].lower() == 'pay' or split_message[2].lower() == 'pay'):
audience = 'private'
if split_message[2].lower() == 'charge' or split_message[2].lower() == 'pay':
audience = split_message[1].lower()
if audience != 'public' and audience != 'friends' and audience != 'private':
parse_error('Valid payment sharing commands\npublic\nfriend\nprivate', response_url)
return
del split_message[1]
which = split_message[1]
if len(split_message) <= 6:
parse_error('Invalid payment string', response_url)
return
for_index = _find_str_in_list(split_message, 'for')
if for_index == -1:
parse_error('Invalid payment string', response_url)
return
amount_str_array = split_message[2:for_index]
amount = _calculate_total(amount_str_array, response_url)
if amount is None:
return
to_index = _find_last_str_in_list(split_message, 'to')
if to_index < 5:
parse_error('Could not find recipients', response_url)
return
note = ' '.join(split_message[(for_index + 1):to_index])
recipients = split_message[(to_index + 1):]
venmo_payment(audience, which, amount, note, recipients, access_token, venmo_id, user_id, team_id, response_url)
def load_config():
config.credentials = ConfigParser.ConfigParser()
config.credentials.read('credentials.ini')
with open('settings.json', 'r') as f:
config.workspaces = json.loads(f.read())
if __name__ == '__main__':
config.credentials = ConfigParser.ConfigParser()
config.credentials.read('credentials.ini')
with open('settings.json', 'r') as f:
config.workspaces = json.loads(f.read())
app.run(debug=False, use_reloader=False)