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 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 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')