Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ZAKI RAIHAN
1606878505_ossd_telegram_notif_bot
Commits
551347b7
Commit
551347b7
authored
Sep 26, 2019
by
zakiraihan
Browse files
create simple push notification to user
parent
67893ab1
Changes
2
Hide whitespace changes
Inline
Side-by-side
app.py
View file @
551347b7
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
navigation/navigation.py
View file @
551347b7
...
...
@@ -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"
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment