Hello! I have this little code that can search dropbox file from slack,
I have been trying to migrate to API V2, but can't get it work.
Don't know what the problem is.
# encoding: utf-8
import time
import os
import json
from flask import Flask, request, render_template, url_for, json, jsonify
import csv
import dropbox
import requests
import sys
from slackclient import SlackClient
reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask(__name__)
SLACK_WEBHOOK_SECRET = os.environ.get('SLACK_WEBHOOK_SECRET')
@app.route('/', methods=['POST'])
def index():
if request.method == 'POST' and request.form.get('token') == SLACK_WEBHOOK_SECRET:
username = request.form.get('user_name', 'null')
text = request.form.get('text', 'null')
channel = request.form.get('channel_name', 'null')
print channel
print username
print text
dbx = dropbox.Dropbox('dropboxtoken')
metadata, f = dbx.files_download('/inventory.csv')
out = open('inventory.csv', 'w')
out.write(f.content)
out.close()
with open('inventory.csv', 'rU') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for i, row in enumerate(readCSV):
for j, column in enumerate(row):
if text.replace("item:", "") in column:
print ' '.join(row)
payload = {"channel": "#"+channel, "username": "ROBOT-1", "text": "您查詢的是::point_down:\n"+' '.join(row), "icon_emoji": ":robot_face:"}
r = requests.post('webhookURL', data=json.dumps(payload))
print (r.url)
if __name__ == '__main__':
app.run(host='0.0.0.0')