Fakultas Ilmu Komputer UI

Commit 551347b7 authored by zakiraihan's avatar zakiraihan
Browse files

create simple push notification to user

parent 67893ab1
import os
import requests
import telegram
import urllib.parse
from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
......@@ -15,6 +17,7 @@ app.config.from_object(os.environ.get('APP_SETTINGS'))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# ================================================================ Bot Handler ================================================================ #
@app.route('/{}'.format(TOKEN), methods=['POST'])
def respond():
from navigation.navigation import botNavigation
......@@ -37,10 +40,24 @@ def set_webhook(noNgrok):
else:
return "webhook setup failed"
# ============================================================================================================================================= #
@app.route('/')
def hello():
return "Hello World!"
@app.route('/sendNotification', methods=['POST'])
def sendNotif():
from models.UserModel import UserModel
req_data = request.get_json()
user = UserModel.query.filter(UserModel.phoneNumber==req_data['phoneNumber']).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()
if __name__ == '__main__':
app.run(threaded=True)
\ No newline at end of file
......@@ -3,10 +3,10 @@ from telebot.mastermind import *
def botNavigation(bot, update):
chat_id = update.message.chat.id
msg_id = update.message.message_id
contact = update.message.contact if update.message.contact != None else None
# Telegram understands UTF-8, so encode text for unicode compatibility
text = update.message.text.encode('utf-8').decode() if update.message.text != None else None
contact = update.message.contact if update.message.contact != None else None
if (text == "ahoyy"):
basic_response(bot, chat_id)
elif (text == "/about"):
......
Supports Markdown
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