def messageList(network_id): if 'user_id' in session: form = FormList(request.form) currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.network_id == network_id) #import new tweets home_timeline = oauth_req('https://api.twitter.com/1.1/direct_messages/events/list.json', currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret) #print(home_timeline) data = home_timeline.decode('utf-8', 'replace') obj = json.loads(data) #print(json_str(obj)) for event in obj['events']: selectMessage = NetworkModel.Message.select().where(NetworkModel.Message.external_uuid == event['id'], NetworkModel.Message.network_id == currentNet.network_id) if selectMessage.count() == 0 and currentNet.friend_id.external_uuid != event['message_create']['sender_id']: senderFriend = NetworkModel.Friend.select().where(NetworkModel.Friend.external_uuid == event['message_create']['sender_id'], NetworkModel.Friend.network_id == network_id) if senderFriend.count() == 0: newFriend = NetworkModel.Friend() newFriend.external_uuid = event['message_create']['sender_id'] newFriend.name = 'Desconocido' newFriend.username = '******' newFriend.network_id = network_id newFriend.is_visible = 0 newFriend.save() senderFriend = NetworkModel.Friend.select().where(NetworkModel.Friend.external_uuid == event['message_create']['sender_id'], NetworkModel.Friend.network_id == network_id) newMessage = NetworkModel.Message() newMessage.text = str(event['message_create']['message_data']['text'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newMessage.external_uuid = event['id'] newMessage.network_id = network_id newMessage.friend_sender_id = senderFriend[0].friend_id #newMessage.created_timestamp = datetime.utcfromtimestamp(int(event['created_timestamp'])) newMessage.save() messageList = NetworkModel.Message.select().where(NetworkModel.Message.network_id == currentNet.network_id).order_by(NetworkModel.Message.message_id.desc()) show_message_css = 'hide' show_message_text = '' show_message_type = 'success' if 'result' in request.args: show_message_css = '' if request.args['result'] == 'ok': show_message_text = 'La información se guardo correctamente' else: show_message_type = 'danger' show_message_text = 'Hubo un problema al realizar la acción' return render_template('twitter/inbox.html', form=form, twitter=currentNet, networks=messageList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type) else: return redirect ('/logout')
def networkEdit(id=None): if 'user_id' in session: form = FormEdit(request.form) currentNet = NetworkModel.Network() if id != None: currentNet = NetworkModel.Network.get(NetworkModel.Network.network_id == id) if request.method == 'GET': if currentNet.network_id == None: currentNet = None return render_template('networks/edit.html', form=form, network=currentNet) elif request.method == 'POST': currentNet.owner_id = session['user_id'] currentNet.name = request.form['name'] currentNet.network_type = request.form['network_type'] if request.form.get('is_active', None) != None: currentNet.is_active = 1 else: currentNet.is_active = 0 currentNet.save() last_network_id = NetworkModel.Network.select().order_by(NetworkModel.Network.network_id.desc()).get().network_id if id == None: return redirect ('/' + currentNet.network_type.lower() + '/new/' + str(last_network_id)) else: return redirect ('/networks?result=ok') else: return redirect ('/logout')
def twitterTweet(twitter_id): if 'user_id' in session: currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.twitter_id == twitter_id) form = FormEdit(request.form) if request.method == 'GET': return render_template('twitter/tweet.html', form=form, network=currentNet) else: currentTweet = NetworkModel.Tweet() currentTweet.user_id = session['user_id'] currentTweet.twitter_id = twitter_id currentTweet.text = request.form['tweet'] # URLENCODEAR EL TWEET params = urllib.parse.urlencode({ 'status': currentTweet.text }) home_timeline = oauth_req('https://api.twitter.com/1.1/statuses/update.json?' + params, currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret, http_method='POST') data = home_timeline.decode('utf-8', 'replace') obj = json.loads(data) currentTweet.tweet_uuid = str(obj['id']) currentTweet.save() return redirect ('/twitter/tweets/' + str(twitter_id) + '?result=ok') else: return redirect ('/logout')
def slackFriends(slack_id): if 'user_id' in session: form = FormList(request.form) currentNet = NetworkModel.Slack.get( NetworkModel.Slack.slack_id == slack_id) url = 'https://slack.com/api/users.list' headers = { 'cache-control': "no-cache", 'authorization': "Bearer " + currentNet.oauth_access_token } response = requests.request("GET", url, headers=headers) resp = json.loads(response.text) #print(json_str(resp)) for friend in resp['members']: selectFriend = NetworkModel.Friend.select().where( NetworkModel.Friend.external_uuid == friend['id'], NetworkModel.Friend.network_id == currentNet.network_id) if selectFriend.count() == 0: newFriend = NetworkModel.Friend() newFriend.external_uuid = friend['id'] newFriend.name = str(friend['profile']['real_name'].encode( 'ascii', 'ignore').decode('utf-8', 'replace')) newFriend.username = str(friend['name'].encode( 'ascii', 'ignore').decode('utf-8', 'replace')) newFriend.image_url = friend['profile']['image_48'] newFriend.network_id = currentNet.network_id newFriend.save() friendList = NetworkModel.Friend.select().where( NetworkModel.Friend.network_id == currentNet.network_id, NetworkModel.Friend.is_visible == 1) show_message_css = 'hide' show_message_text = '' show_message_type = 'success' if 'result' in request.args: show_message_css = '' if request.args['result'] == 'ok': show_message_text = 'El mensaje se envió correctamente' else: show_message_type = 'danger' show_message_text = 'Hubo un problema al realizar la acción' return render_template('slack/friends.html', form=form, twitter=currentNet, networks=friendList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type) else: return redirect('/logout')
def tweetList(twitter_id): if 'user_id' in session: form = FormList(request.form) currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.twitter_id == twitter_id) #import new tweets home_timeline = oauth_req('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' + currentNet.username, currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret) data = home_timeline.decode('utf-8', 'replace') obj = json.loads(data) #print(obj.encode('ascii', 'ignore')) for tweet in obj: selectTweet = NetworkModel.Tweet.select().where(NetworkModel.Tweet.tweet_uuid == tweet['id_str']) if selectTweet.count() == 0: newTweet = NetworkModel.Tweet() newTweet.user_id = session['user_id'] newTweet.twitter_id = twitter_id newTweet.text = str(tweet['text'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newTweet.tweet_uuid = tweet['id_str'] newTweet.date = parser.parse(tweet['created_at']) newTweet.save() networkList = NetworkModel.Tweet.select().where(NetworkModel.Tweet.twitter_id == twitter_id).order_by(NetworkModel.Tweet.date.desc()) if session['is_admin'] == 1: networkList = NetworkModel.Tweet.select().where(NetworkModel.Tweet.twitter_id == twitter_id).order_by(NetworkModel.Tweet.date.desc()) show_message_css = 'hide' show_message_text = '' show_message_type = 'success' if 'result' in request.args: show_message_css = '' if request.args['result'] == 'ok': show_message_text = 'La información se guardo correctamente' else: show_message_type = 'danger' show_message_text = 'Hubo un problema al realizar la acción' return render_template('twitter/tweets.html', form=form, twitter=currentNet, networks=networkList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type) else: return redirect ('/logout')
def twitterFriends(twitter_id): if 'user_id' in session: form = FormList(request.form) currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.twitter_id == twitter_id) #import new tweets home_timeline = oauth_req('https://api.twitter.com/1.1/friends/list.json?count=50', currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret) data = home_timeline.decode('utf-8', 'replace') obj = json.loads(data) for friend in obj['users']: #print(str(friend['name'].encode('ascii', 'ignore').decode('utf-8', 'replace'))) selectFriend= NetworkModel.Friend.select().where(NetworkModel.Friend.external_uuid == friend['id_str'], NetworkModel.Friend.network_id == currentNet.network_id) if selectFriend.count() == 0: newFriend = NetworkModel.Friend() newFriend.external_uuid = friend['id_str'] newFriend.name = str(friend['name'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newFriend.username = str(friend['screen_name'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newFriend.image_url = str(friend['profile_image_url'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newFriend.network_id = currentNet.network_id newFriend.save() friendList = NetworkModel.Friend.select().where(NetworkModel.Friend.network_id == currentNet.network_id, NetworkModel.Friend.is_visible == 1) show_message_css = 'hide' show_message_text = '' show_message_type = 'success' if 'result' in request.args: show_message_css = '' if request.args['result'] == 'ok': show_message_text = 'El mensaje se envió correctamente' else: show_message_type = 'danger' show_message_text = 'Hubo un problema al realizar la acción' return render_template('twitter/friends.html', form=form, twitter=currentNet, networks=friendList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type) else: return redirect ('/logout')
def telegramEdit(telegram_id=None, network_id=None): if 'user_id' in session: form = FormEdit(request.form) currentNet = NetworkModel.Telegram() if telegram_id != None: currentNet = NetworkModel.Telegram.get( NetworkModel.Telegram.telegram_id == telegram_id) if request.method == 'GET': if currentNet.telegram_id == None: currentNet = None return render_template('telegram/edit.html', form=form, network=currentNet) elif request.method == 'POST': url = "https://api.telegram.org/bot" headers = {'Content-Type': 'application/json'} response = requests.request("GET", url + request.form['bot_token'] + '/getMe', headers=headers) data = json.loads(response.text) if data['ok'] == False: flash('Bot Token inválido', 'error') currentNet.username = request.form['username'] currentNet.bot_token = request.form['bot_token'] return render_template('telegram/edit.html', form=form, network=currentNet) usernameTemp = request.form['username'] if data['result']['username'] != usernameTemp: flash( 'El username no coincide. El token pertenece a: ' + data['result']['username'], 'error') currentNet.username = request.form['username'] currentNet.bot_token = request.form['bot_token'] return render_template('telegram/edit.html', form=form, network=currentNet) #creates default friend if telegram_id == None: newFriend = NetworkModel.Friend() newFriend.external_uuid = data['result']['id'] newFriend.name = data['result']['first_name'] newFriend.username = data['result']['username'] newFriend.network_id = network_id newFriend.is_visible = 0 newFriend.save() owner_friend_id = NetworkModel.Friend.select().order_by( NetworkModel.Friend.friend_id.desc()).get().friend_id currentNet.webhook_activated = 0 currentNet.friend_id = owner_friend_id currentNet.username = data['result']['username'] currentNet.first_name = data['result']['first_name'] currentNet.external_uuid = data['result']['id'] currentNet.bot_token = request.form['bot_token'] currentNet.user_id = session['user_id'] currentNet.network_id = network_id currentNet.save() if telegram_id == None: last_telegram_id = NetworkModel.Telegram.select().order_by( NetworkModel.Telegram.telegram_id.desc()).get().telegram_id currentNetwork = NetworkModel.Network.get( NetworkModel.Network.network_id == network_id) currentNetwork.telegram_id = last_telegram_id currentNetwork.save() return redirect('/telegram?result=ok') else: return redirect('/logout')
def telegramMessageList(network_id): if 'user_id' in session: form = FormList(request.form) currentNet = NetworkModel.Telegram.get( NetworkModel.Telegram.network_id == network_id) if currentNet.webhook_activated == 0: #import new messages url = "https://api.telegram.org/bot" headers = {'Content-Type': 'application/json'} response = requests.request("GET", url + currentNet.bot_token + '/getUpdates', headers=headers) data = json.loads(response.text) if data['ok'] == True: for event in data['result']: selectMessage = NetworkModel.Message.select().where( NetworkModel.Message.external_uuid == event['message'] ['message_id'], NetworkModel.Message.network_id == currentNet.network_id) if 'text' in event['message'].keys( ) != None and selectMessage.count( ) == 0 and currentNet.friend_id.external_uuid != event[ 'message']['from']['id']: senderFriend = NetworkModel.Friend.select().where( NetworkModel.Friend.external_uuid == event['message']['from']['id'], NetworkModel.Friend.network_id == network_id) if senderFriend.count() == 0: newFriend = NetworkModel.Friend() newFriend.external_uuid = event['message']['from'][ 'id'] newFriend.name = event['message']['from'][ 'first_name'] newFriend.username = event['message']['from'][ 'first_name'] newFriend.network_id = network_id newFriend.save() senderFriend = NetworkModel.Friend.select().where( NetworkModel.Friend.external_uuid == event['message']['from']['id'], NetworkModel.Friend.network_id == network_id) newMessage = NetworkModel.Message() newMessage.text = event['message']['text'] newMessage.external_uuid = event['message'][ 'message_id'] newMessage.network_id = network_id newMessage.friend_sender_id = senderFriend[0].friend_id newMessage.created_timestamp = datetime.utcfromtimestamp( int(event['message']['date'])) newMessage.save() messageList = NetworkModel.Message.select().where( NetworkModel.Message.network_id == currentNet.network_id).order_by( NetworkModel.Message.message_id.desc()) show_message_css = 'hide' show_message_text = '' show_message_type = 'success' if 'result' in request.args: show_message_css = '' if request.args['result'] == 'ok': show_message_text = 'La información se guardo correctamente' else: show_message_type = 'danger' show_message_text = 'Hubo un problema al realizar la acción' return render_template('telegram/inbox.html', form=form, telegram=currentNet, networks=messageList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type) else: return redirect('/logout')
def slackEdit(slack_id=None, network_id=None): if 'user_id' in session: form = FormEdit(request.form) currentNet = NetworkModel.Slack() if slack_id != None: currentNet = NetworkModel.Slack.get( NetworkModel.Slack.slack_id == slack_id) if request.method == 'GET': if currentNet.slack_id == None: currentNet = None return render_template('slack/edit.html', form=form, network=currentNet) elif request.method == 'POST': url = "https://slack.com/api/auth.test" headers = { 'cache-control': "no-cache", 'authorization': "Bearer " + request.form['oauth_access_token'] } response = requests.request("GET", url, headers=headers) resp = json.loads(response.text) if resp['ok'] == False: flash('Token inválido', 'error') return render_template('slack/edit.html', form=form, network=currentNet) urlTemp = 'https://' + request.form['workspace'] + '.slack.com/' if resp['url'] != urlTemp: flash('Workspace inválido', 'error') return render_template('slack/edit.html', form=form, network=currentNet) if slack_id == None: url_user = "******" + resp[ 'user_id'] headers_user = { 'cache-control': "no-cache", 'authorization': "Bearer " + request.form['oauth_access_token'] } response_user = requests.request("GET", url_user, headers=headers_user) resp_user = json.loads(response_user.text) newFriend = NetworkModel.Friend() newFriend.external_uuid = resp_user['user']['id'] newFriend.name = str( resp_user['user']['profile']['real_name'].encode( 'ascii', 'ignore').decode('utf-8', 'replace')) newFriend.username = str(resp_user['user']['name'].encode( 'ascii', 'ignore').decode('utf-8', 'replace')) newFriend.image_url = resp_user['user']['profile']['image_48'] newFriend.network_id = network_id newFriend.is_visible = 0 newFriend.save() owner_friend_id = NetworkModel.Friend.select().order_by( NetworkModel.Friend.friend_id.desc()).get().friend_id currentNet.friend_id = owner_friend_id currentNet.user_id = session['user_id'] currentNet.workspace = request.form['workspace'] currentNet.oauth_access_token = request.form['oauth_access_token'] currentNet.network_id = network_id currentNet.save() if slack_id == None: last_slack_id = NetworkModel.Slack.select().order_by( NetworkModel.Slack.slack_id.desc()).get().slack_id currentNetwork = NetworkModel.Network.get( NetworkModel.Network.network_id == network_id) currentNetwork.slack_id = last_slack_id currentNetwork.save() return redirect('/slack?result=ok') else: return redirect('/logout')
def slackMessageList(network_id, friend_id): if 'user_id' in session: form = FormList(request.form) currentNet = NetworkModel.Slack.get( NetworkModel.Slack.network_id == network_id) currentFriend = NetworkModel.Friend.get( NetworkModel.Friend.network_id == network_id and NetworkModel.Friend.friend_id == friend_id) conversation_id = conversation_open(currentFriend.external_uuid, currentNet.oauth_access_token) url = "https://slack.com/api/im.history?channel=" + conversation_id headers = { 'cache-control': "no-cache", 'authorization': "Bearer " + currentNet.oauth_access_token } response = requests.request("GET", url, headers=headers) resp = json.loads(response.text) #print(json_str(resp)) for event in resp['messages']: selectMessage = NetworkModel.Message.select().where( NetworkModel.Message.external_uuid == event['user'] + '-' + event['ts'], NetworkModel.Message.network_id == currentNet.network_id) if selectMessage.count() == 0: senderFriend = NetworkModel.Friend.select().where( NetworkModel.Friend.external_uuid == event['user'], NetworkModel.Friend.network_id == network_id) if senderFriend.count() == 0: newFriend = NetworkModel.Friend() newFriend.external_uuid = event['user'] newFriend.name = 'Desconocido' newFriend.username = '******' newFriend.network_id = network_id newFriend.is_visible = 0 newFriend.save() senderFriend = NetworkModel.Friend.select().where( NetworkModel.Friend.external_uuid == event['user'], NetworkModel.Friend.network_id == network_id) newMessage = NetworkModel.Message() newMessage.text = event['text'] newMessage.external_uuid = event['user'] + '-' + event['ts'] newMessage.network_id = network_id newMessage.friend_sender_id = senderFriend[0].friend_id #newMessage.created_timestamp = datetime.utcfromtimestamp(int(event['created_timestamp'])) newMessage.save() messageList = NetworkModel.Message.select().where( NetworkModel.Message.network_id == currentNet.network_id).order_by( NetworkModel.Message.message_id.desc()) show_message_css = 'hide' show_message_text = '' show_message_type = 'success' if 'result' in request.args: show_message_css = '' if request.args['result'] == 'ok': show_message_text = 'La información se guardo correctamente' else: show_message_type = 'danger' show_message_text = 'Hubo un problema al realizar la acción' if resp['ok'] == False: show_message_css = '' show_message_type = 'danger' show_message_text = 'Error de API: ' + resp['error'] return render_template('slack/inbox.html', form=form, slack=currentNet, networks=messageList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type) else: return redirect('/logout')
def twitterEdit(id=None, network_id=None): if 'user_id' in session: form = FormEdit(request.form) currentNet = NetworkModel.Twitter() if id != None and id != 0: currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.twitter_id == id) if request.method == 'GET': if currentNet.twitter_id == None: currentNet = None return render_template('twitter/edit.html', form=form, network=currentNet) elif request.method == 'POST': currentNet.user_id = session['user_id'] currentNet.username = request.form['username'] currentNet.consumer_key = request.form['consumer_key'] currentNet.consumer_secret = request.form['consumer_secret'] currentNet.access_token = request.form['access_token'] currentNet.access_token_secret = request.form['access_token_secret'] currentNet.network_id = network_id home_timeline = oauth_req('https://api.twitter.com/1.1/account/settings.json', currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret) data = home_timeline.decode('utf-8', 'replace') obj = json.loads(data) if 'errors' in obj: flash(obj['errors'][0]['message'], 'error') return render_template('twitter/edit.html', form=form, network=currentNet) if currentNet.username != obj['screen_name']: flash('Nombre de usuario diferente', 'error') return render_template('twitter/edit.html', form=form, network=currentNet) if id == None: user_profile = oauth_req('https://api.twitter.com/1.1/users/show.json?screen_name='+currentNet.username, currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret) data_profile = user_profile.decode('utf-8', 'replace') obj_profile = json.loads(data_profile) #print(json_str(obj_profile)) newFriend = NetworkModel.Friend() newFriend.external_uuid = obj_profile['id_str'] newFriend.name = str(obj_profile['name'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newFriend.username = str(obj_profile['screen_name'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newFriend.image_url = str(obj_profile['profile_image_url'].encode('ascii', 'ignore').decode('utf-8', 'replace')) newFriend.network_id = network_id newFriend.is_visible = 0 newFriend.save() owner_friend_id = NetworkModel.Friend.select().order_by(NetworkModel.Friend.friend_id.desc()).get().friend_id currentNet.friend_id = owner_friend_id currentNet.save() if id == None: last_twitter_id = NetworkModel.Twitter.select().order_by(NetworkModel.Twitter.twitter_id.desc()).get().twitter_id currentNetwork = NetworkModel.Network.get(NetworkModel.Network.network_id == network_id) currentNetwork.twitter_id = last_twitter_id currentNetwork.save() return redirect ('/twitter?result=ok') #return render_template('twitter/edit.html', form=form, network=currentNet) else: return redirect ('/logout')