UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login
    1. Home
    2. FeZ
    3. Posts
    F
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 3
    • Groups 0

    Posts

    Recent Best Controversial
    • RE: Facebook Messenger PUSH notifications

      Thank you, I'll try that also.

      posted in Off topic
      F
      FeZ
    • Facebook Messenger PUSH notifications

      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

      1. Start your pushclient on the UB device.
      2. Write/copy your token (take care, it's yours, don't share with others!)

      INSTALL fbchat

      1. Login as administrator (aka. root) to your home server
      2. install fbchat:
      # pip install fbchat 
      

      INSTALL AND SETUP fbnotifications.py

      1. Login as a safe normal user to your home server.
      2. 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()
      
      1. Change <your facebook login email>, <your facebook login password>, and <your ubuntu one app token> strings to your own data respectively.
      2. Set DEBUG global variable according to your needs to False or True.
      3. Save the file.
      4. Set permissions so that only you (and administrator) can read your personal data:
      $ chmod 600 fbnotifications.py
      
      
      1. 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

      1. Run the program on home server as a system service (recommended).
      • on most modern Linux use systemd
      • on BSD use rc scripts
      1. 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.
      posted in Off topic
      F
      FeZ
    • Switching wlan hotspot to custom IP address range

      These are the steps I did to use 192.168.7.0/24 instead of the default 10.42.0.0/16 address range.

      IMPORTANT NOTES:

      • The IP range is hardcoded in network-manager-1.2.x branch, which is used in UBports 16.04 (xenial). Therefore one have to rebuild the package! A good tool is to perform the rebuild on an x86 or x86_64 PC under Ubuntu is sbuild. Setup an environment for xenial-armhf, login there, and perform a normal debuild for the package.

      • The latest version of network-manager in Xenial is network-manager-1.2.6. This is compatible with the original 1.2.2.

      SETTING THE CUSTOM IP RANGE:

      1. Open the file src/devices/nm-device.c
        Somewhere at the line 4834 (nm version 1.2.6) find the line like this:
      guint32 start = (guint32) ntohl (0x0a2a0001); /* 10.42.0.1 */
      

      The hex value is just calculated from the gateway's dotted decimal IP address of the desired network.
      See this:

      0a = 10
      2a = 42
      00 = 0
      01 = 1
      

      Calculate your desired IP. Use 1, as the gateway, or read the code further how to adjust the netmask if you want another gw address. Mine is now 192.168.7.1:

      192 = c0
      168 = 80
      7 = 07
      1 = 01
      

      My modified line is like this now:

      guint32 start = (guint32) ntohl (0xc0a80701); /* 192.168.7.1 */
      

      Now locate debian/rules file, and remove everything under override_dh_auto_test: line. Tests break in a chrooted environment. So it should look like this:

      override_dh_auto_test:
      
      
      

      You can also modify the configure part to avoid building any tests. Change the line:

      --with-tests \
      

      into:

      --without-tests \
      

      Simple enough, ready!

      Now rebuild your package. You will get several deb files after a successful build, but you need only network-manager-1.2.x.deb. Deploy it on the phone. Reboot, and enjoy the new hotspot network!

      posted in Support
      F
      FeZ