def getUserSlackMembership(request, company_id): try: #print 'in user room' user_id = request.user.id #company_id = request.user.company slck_auth_needed = False slck_user_auth_needed = False # company = Company.objects(company_id=company_id).first() # company_id = company.id print 'company id is ' + str(company_id) existingIntegration= CompanyIntegration.objects(company_id=company_id).first() if existingIntegration is None or 'slck' not in existingIntegration['integrations']: return JsonResponse({'slck_auth_needed' : True}) #token = existingIntegration['integrations']['slck'] userOauthRecord = UserOauth.objects(user_id= ObjectId(user_id)).first() if userOauthRecord is None: return JsonResponse({'slck_user_auth_needed' : True}) if 'slck_access_token' in userOauthRecord and userOauthRecord['slck_access_token'] is not None and userOauthRecord['slck_access_token'] != "": token = userOauthRecord['slck_access_token'] else: return JsonResponse({'slck_user_auth_needed' : True}) slck = Slack(None, None, None, None, token) channels = json.loads(slck.api_call("channels.list")) groups = json.loads(slck.api_call("groups.list")) ims = json.loads(slck.api_call("im.list")) users = json.loads(slck.api_call("users.list")) if not users['ok']: raise ValueError('Error while retrieving users from Slack') rtm = slck.rtm_connect() return JsonResponse({'slack_channels': channels, 'slack_groups': groups, 'slack_ims': ims, 'users': users['members'], 'rtm': rtm, 'slck_auth_needed': False, 'slck_user_auth_needed': slck_user_auth_needed }) except Exception as e: return Response('Error: ' + str(e))
def user_create_slack_message(request): #this is only for messages with attachments (e.g. Snapshots). Simple messages are sent via websocket from the client try: post_data = json.loads(request.body) channel_id = post_data['channel_id'] message = post_data['message'] #company_id = post_data['company_id'] user_id = request.user.id company_id = request.user.company if 'snapshot_id' in post_data: snapshot_id = post_data['snapshot_id'] else: snapshot_id = None print 'snap id ' + str(snapshot_id) existingIntegration= CompanyIntegration.objects(company_id=company_id).first() if existingIntegration is None or 'slck' not in existingIntegration['integrations']: return JsonResponse(None) #token = existingIntegration['integrations']['slck'] userOauthRecord = UserOauth.objects(user_id= ObjectId(user_id)).first() if userOauthRecord is None: return JsonResponse(None) if 'slck_access_token' in userOauthRecord and userOauthRecord['slck_access_token'] is not None: token = userOauthRecord['slck_access_token'] else: return JsonResponse(None) slck = Slack(None, None, None, None, token) attachments = [] if snapshot_id is not None: snapshot = Snapshot.objects(id=ObjectId(snapshot_id)).first() if snapshot is None: raise ValueError('Snapshot not found!') attachment = { "fallback": "New Claritix" + snapshot['chart_name'] + " chart snapshot - " + settings.BASE_URL + "/snapshots/" + snapshot_id, "pretext": "New Claritix chart snapshot", "title": "Claritix snapshot - " + snapshot['chart_name'] + " as of " + strftime('%Y-%m-%d %H:%M:%S', snapshot['updated_date'].timetuple()), "title_link": settings.BASE_URL + "/snapshots/" + snapshot_id, "text": "Click above link to view the snapshot within Claritix", "color": "#0491c3", "author_name": "Claritix", "author_link": "http://claritix.io", "author_icon": settings.BASE_URL + "/static/images/logo-icon-16x16.png" } attachments.append(attachment) print 'attachme ' + str(attachments) params = {'channel': channel_id, 'text': message, 'as_user': True, "attachments": json.dumps(attachments)} result = json.loads(slck.api_call("chat.postMessage", **params)) return JsonResponse({"result": result}, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
def getUserSlackMessages(request, company_id): try: #print 'in user room' user_id = request.user.id #company_id = request.user.company slack_id = request.GET.get("id") slack_type = request.GET.get("type") # company = Company.objects(company_id=company_id).first() # company_id = company.id existingIntegration= CompanyIntegration.objects(company_id=company_id).first() if existingIntegration is None or 'slck' not in existingIntegration['integrations']: return JsonResponse(None) #token = existingIntegration['integrations']['slck'] userOauthRecord = UserOauth.objects(user_id= ObjectId(user_id)).first() if userOauthRecord is None: return JsonResponse(None) if 'slck_access_token' in userOauthRecord and userOauthRecord['slck_access_token'] is not None: token = userOauthRecord['slck_access_token'] else: return JsonResponse(None) slck = Slack(None, None, None, None, token) messages = None params = {'channel': slack_id, 'inclusive': 1, 'count': 1000} if slack_type == 'group': messages = json.loads(slck.api_call("groups.history", **params)) elif slack_type == 'channel': messages = json.loads(slck.api_call("channels.history", **params)) elif slack_type == 'im': messages = json.loads(slck.api_call("im.history", **params)) if not messages['ok']: raise ValueError('Error while retrieving messages from Slack') messages_list = messages['messages'] rtm = slck.rtm_connect() users = json.loads(slck.api_call("users.list")) if not users['ok']: raise ValueError('Error while retrieving users from Slack') # for message in messages_list: #join the user info to each message # #print 'msg is ' + str(message['user']) # for user in users['members']: # if 'user' in message and message['user'] == user['id']: # message['user_real_name'] = user['profile']['real_name'] # message['user_name'] = user['name'] # message['user_image_url'] = user['profile']['image_72'] # break return JsonResponse({'slack_messages': messages, 'users': users['members'], 'rtm': rtm}, safe=False) except Exception as e: print 'error occurred: ' + str(e) return Response('Error: ' + str(e))