Fakultas Ilmu Komputer UI

Commit 1efc3aeb authored by zakiraihan's avatar zakiraihan
Browse files

Applying send notification function

parent 768cdfc6
......@@ -47,15 +47,18 @@ def hello():
@app.route('/sendNotification', methods=['POST'])
def sendNotif():
from models.UserModel import UserModel
from models.AccountModel import AccountModel
req_data = request.get_json()
user = UserModel.query.filter(UserModel.phoneNumber==req_data['phoneNumber']).first()
user = AccountModel.query.filter(AccountModel.ssoId==req_data['username']).first()
if (user is None):
return {'status': 404, 'message': 'User not found'}
param = urllib.parse.urlencode({'chat_id': user.userId, 'text': req_data['message']})
url = "https://api.telegram.org/bot" + TOKEN + "/sendMessage?" + param
r = requests.get(url = url)
return r.json()
elif (user.chatId == None):
return {'status': 500, 'message': 'User notification is being turned off'}
else:
param = urllib.parse.urlencode({'chat_id': user.chatId, 'text': req_data['message']})
url = "https://api.telegram.org/bot" + TOKEN + "/sendMessage?" + param
r = requests.get(url = url)
return r.json()
@app.route("/auth/", methods=['POST'])
def auth():
......
......@@ -14,7 +14,7 @@ def botNavigation(bot, update):
elif (text == "/activateNotif"):
user_activate_notif(bot, chat_id)
elif (text == None and contact != None) :
save_user_data(bot, chat_id, contact)
save_user_notif_activation_data(bot, chat_id, contact)
elif (text == "/deactivateNotif"):
user_deactive_notif(bot, chat_id)
elif (text == "/login"):
......
......@@ -26,7 +26,7 @@ def user_activate_notif(bot, chat_id):
text="Would you mind sharing your contact with me?",
reply_markup=reply_markup)
else:
bot.sendMessage(chat_id=chat_id, text="You have been registered", reply_markup=ReplyKeyboardRemove())
bot.sendMessage(chat_id=chat_id, text="Your notification is still active", reply_markup=ReplyKeyboardRemove())
def save_user_notif_activation_data(bot, chat_id, contact):
checkAccountExistance = AccountModel.query.filter(AccountModel.phoneNumber==str(contact.phone_number)).first()
......@@ -38,13 +38,13 @@ def save_user_notif_activation_data(bot, chat_id, contact):
bot.sendMessage(chat_id=chat_id, text="Register success!!!", reply_markup=ReplyKeyboardRemove())
def user_deactive_notif(bot, chat_id):
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first()
if (checkUserExistance is None):
bot.sendMessage(chat_id=chat_id, text="You are not registerd yet", reply_markup=ReplyKeyboardRemove())
checkAccountExistance = AccountModel.query.filter(AccountModel.chatId==str(chat_id)).first()
if (checkAccountExistance is None):
bot.sendMessage(chat_id=chat_id, text="You are not activating your notification yet", reply_markup=ReplyKeyboardRemove())
else:
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first()
checkUserExistance.delete()
bot.sendMessage(chat_id=chat_id, text="Successfully logged out from bot", reply_markup=ReplyKeyboardRemove())
checkAccountExistance.chatId = None
checkAccountExistance.update()
bot.sendMessage(chat_id=chat_id, text="Successfully turning off your notification", reply_markup=ReplyKeyboardRemove())
def user_login_sso(bot, chat_id):
base_url = os.environ.get('URL')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment