PREREQUISITES:
- a FaceBook account;
- a Messenger client installed on your device from appstore (Pesbuk recommended);
- a "home" server capable running python3 code. You can run on your own UPBorts device instead, but I have not tested;
- "pushclient" mini app installed on your device from AppStore;
- basic programming skills;
- understanding, and taking your own responsibility of security considerations.
GETTING YOUR APP TOKEN
- Start your pushclient on the UB device.
- Write/copy your token (take care, it's yours, don't share with others!)
INSTALL fbchat
- Login as administrator (aka. root) to your home server
- install fbchat:
# pip install fbchat
INSTALL AND SETUP fbnotifications.py
- Login as a safe normal user to your home server.
- Open your favourite code editor, and create a file 'fbnotifications.py'. Copy this code into that file:
#!/usr/bin/env python
import signal
import sys
import requests
import json
from datetime import datetime, timedelta
from fbchat import Client
from fbchat.models import *
DEBUG = True
LOG_FILENAME = "debug.log"
USER = "<your facebook login email>"
PASSWORD = "<your facebook login password>"
PUSH_URL = "https://push.ubports.com/notify"
HEADERS = {"Content-Type" : "application/json"}
card = {
"icon" : "message",
"body" : "",
"summary" : "Messenger",
"popup" : True,
"persist" : True
}
notification = {
"card" : card,
"vibrate" : True,
"sound" : True
}
data = {
"notification" : notification,
}
params = {
"appid" : "pushclient.christianpauly_pushclient",
"token" : "<your ubuntu one app token>",
"expire_on" : "",
"data" : data
}
client = None
logfile = None
def log(log_msg):
if DEBUG:
logfile.write(f'{str(log_msg)}\n')
logfile.flush()
def signal_handler(sig, frame):
log('Exiting with signal {sig}')
if client and client.isLoggedIn():
client.logout()
if logfile:
logfile.close()
sys.exit(0)
class CustomClient(Client):
def onMessage(self, mid, author_id, message_object, thread_id, thread_type, ts, metadata, msg, **kwargs):
if not DEBUG and author_id == self.uid:
return
user = self.fetchUserInfo(author_id)[author_id]
log(f'Message from:\n{str(user)}\n')
name = 'noname'
if user.nickname:
name = user.nickname
elif user.name:
name = user.name
dt = datetime.now() + timedelta(days=1)
dt_str = dt.strftime("%Y-%m-%dT%H:%M:00.000Z")
if message_object:
if message_object.text:
body = f'{name}: {message_object.text[:23]}'
else:
body = f'{name} sent media content.'
params["expire_on"] = dt_str
params["data"]["notification"]["card"]["body"] = body
json_data = json.dumps(params)
log(f'Data sent:\n {json_data}\n')
r = requests.post(url=PUSH_URL, headers=HEADERS, data=json_data)
log(r.json())
##################### MAIN #####################
if __name__ == "__main__":
if DEBUG:
print(f'Loggin in: {LOG_FILENAME}')
logfile = open(LOG_FILENAME, "a")
client = CustomClient(USER, PASSWORD)
log(f'User logged in: {client.isLoggedIn()}')
log(client.getSession())
signal.signal(signal.SIGINT, signal_handler)
client.listen()
- Change <your facebook login email>, <your facebook login password>, and <your ubuntu one app token> strings to your own data respectively.
- Set DEBUG global variable according to your needs to False or True.
- Save the file.
- Set permissions so that only you (and administrator) can read your personal data:
$ chmod 600 fbnotifications.py
- Run the program:
$ python fbnotifications.py
TESTING
If you want to run a self-test, then:
- set DEBUG = True, and see the generated logfile in another terminal;
- open FB Messenger web client in a desktop browser;
- search for yourself in the search box above the contact list;
- send a message to yourself;
Now you have to wait until the push notification arrives.
If you have issues, write me on telegram UBPorts Supergroup.
POSSIBLE OPTIONS
- Run the program on home server as a system service (recommended).
- on most modern Linux use systemd
- on BSD use rc scripts
- Run the program in a screen process
This is a very simple solution to run it quick and "service" like.
Note, this will not start automatically after a system restart.
DEFICIENCIES
- Tapping the notification does not open the Messenger app automatically. You have to open it explicitly.