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(): ...@@ -47,15 +47,18 @@ def hello():
@app.route('/sendNotification', methods=['POST']) @app.route('/sendNotification', methods=['POST'])
def sendNotif(): def sendNotif():
from models.UserModel import UserModel from models.AccountModel import AccountModel
req_data = request.get_json() 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): if (user is None):
return {'status': 404, 'message': 'User not found'} return {'status': 404, 'message': 'User not found'}
param = urllib.parse.urlencode({'chat_id': user.userId, 'text': req_data['message']}) elif (user.chatId == None):
url = "https://api.telegram.org/bot" + TOKEN + "/sendMessage?" + param return {'status': 500, 'message': 'User notification is being turned off'}
r = requests.get(url = url) else:
return r.json() 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']) @app.route("/auth/", methods=['POST'])
def auth(): def auth():
......
...@@ -14,7 +14,7 @@ def botNavigation(bot, update): ...@@ -14,7 +14,7 @@ def botNavigation(bot, update):
elif (text == "/activateNotif"): elif (text == "/activateNotif"):
user_activate_notif(bot, chat_id) user_activate_notif(bot, chat_id)
elif (text == None and contact != None) : 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"): elif (text == "/deactivateNotif"):
user_deactive_notif(bot, chat_id) user_deactive_notif(bot, chat_id)
elif (text == "/login"): elif (text == "/login"):
......
...@@ -26,7 +26,7 @@ def user_activate_notif(bot, chat_id): ...@@ -26,7 +26,7 @@ def user_activate_notif(bot, chat_id):
text="Would you mind sharing your contact with me?", text="Would you mind sharing your contact with me?",
reply_markup=reply_markup) reply_markup=reply_markup)
else: 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): def save_user_notif_activation_data(bot, chat_id, contact):
checkAccountExistance = AccountModel.query.filter(AccountModel.phoneNumber==str(contact.phone_number)).first() 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): ...@@ -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()) bot.sendMessage(chat_id=chat_id, text="Register success!!!", reply_markup=ReplyKeyboardRemove())
def user_deactive_notif(bot, chat_id): def user_deactive_notif(bot, chat_id):
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first() checkAccountExistance = AccountModel.query.filter(AccountModel.chatId==str(chat_id)).first()
if (checkUserExistance is None): if (checkAccountExistance is None):
bot.sendMessage(chat_id=chat_id, text="You are not registerd yet", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(chat_id=chat_id, text="You are not activating your notification yet", reply_markup=ReplyKeyboardRemove())
else: else:
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first() checkAccountExistance.chatId = None
checkUserExistance.delete() checkAccountExistance.update()
bot.sendMessage(chat_id=chat_id, text="Successfully logged out from bot", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(chat_id=chat_id, text="Successfully turning off your notification", reply_markup=ReplyKeyboardRemove())
def user_login_sso(bot, chat_id): def user_login_sso(bot, chat_id):
base_url = os.environ.get('URL') 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