User Tools

Site Tools


simple-bpq-telnet-application

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
simple-bpq-telnet-application [2024/11/28 17:18] – created kc2ihxsimple-bpq-telnet-application [2026/01/01 22:42] (current) – [BPQ configuration] kc2ihx
Line 1: Line 1:
 ====== Creating a simple BPQ Application ====== ====== Creating a simple BPQ Application ======
 This tutorial shows you how to set up a simple python script [[https://www.cantab.net/users/john.wiseman/Documents/LinBPQ%20Applications%20Interface.html|connected to linbpq as a telnet application]]. This tutorial shows you how to set up a simple python script [[https://www.cantab.net/users/john.wiseman/Documents/LinBPQ%20Applications%20Interface.html|connected to linbpq as a telnet application]].
 +
 +In order to do this we'll do the following:
 +  - Create a location for the script, data, and a python ''venv''
 +  - Write the script
 +  - Add the application to ''BPQ32.cfg''
 +  - Setup a ''systemd'' service to keep it running.
 +
 +===== Setup =====
 +To organize this project, I created a python virtual environment called ''bpq-apps'' in my ''.local'' directory, and installed the ''telnetlib3'' module that it depends on.  Within the ''bpq-apps'' virtual environment directory, I also created a folder called scripts and a folder called data.  I'm going to install various small BPQ applications here.
 +
 +I'm using the python virtual environment framework to help manage the various dependencies I might want to use for these scripts.
 +
 +<code>
 +$ cd ~/.local
 +$ python -m venv bpq-apps
 +$ cd ./bpq-apps
 +$ mkdir scripts data
 +$ source ./bin/activate
 +(bpq-apps)$ pip install telnetlib3
 +</code>
  
 ===== Writing the program ===== ===== Writing the program =====
 I chose to write this in Python to be a //little// more clear than the Perl application provided in the link above. I chose to write this in Python to be a //little// more clear than the Perl application provided in the link above.
  
-This program, called ''ROLL'' uses a file called ''messages.json'' to provide it a welcome message and the blocks of text.   +The BPQ Application, called ''ROLL'' uses a file called ''messages.json'' to provide it a welcome message and the blocks of text.  The script itself was placed under the ''scripts'' directory created above, saved as ''rick.py''
- +
-saved both of these to ''~/.local/bpq-apps/roll''+
  
 ==== Program Code ==== ==== Program Code ====
Line 13: Line 31:
  
 <code> <code>
-import asyncio, telnetlib3, json+import asyncio, telnetlib3, json, sys
 from optparse import OptionParser from optparse import OptionParser
  
Line 22: Line 40:
  
 messages = json.load(open(options.filename,'r')) messages = json.load(open(options.filename,'r'))
 +
 +def print_log(*a):
 +    print(*a, file=sys.stderr)
 +
  
 async def shell(reader, writer): async def shell(reader, writer):
     incall = await reader.readline()     incall = await reader.readline()
-    print("%s connected" % incall)+    print_log("%s connected" % incall)
     writer.write("\r\nWelcome %s" % incall)     writer.write("\r\nWelcome %s" % incall)
     writer.write("\r\n%s" % messages["welcome"])     writer.write("\r\n%s" % messages["welcome"])
     textblocks = messages["text"].copy()     textblocks = messages["text"].copy()
-    inp = await reader.readline() +    leave = False #inp[0].upper() in ["Q", "B"] 
-    leave = inp[0].upper() in ["Q", "B"]+    rolled = False 
 +    inp = await reader.read(1)
     while inp and not leave and len(textblocks) > 0:     while inp and not leave and len(textblocks) > 0:
-        print("writing next block"+        print("writing next block"
-        writer.write(textblocks.pop())+        writer.write(textblocks.pop(0))
         await writer.drain()         await writer.drain()
         inp = await reader.readline()         inp = await reader.readline()
-        leave = inp[0].upper() in ["Q", "B"]+        if not rolled:  
 +            # notify that rolling has happened 
 +            print_log("%s rickrolled" % incall) 
 +            rolled = True 
 +        leave = inp[0].upper() in ["Q", "B"and rolled
     writer.close()     writer.close()
  
Line 67: Line 94:
 In my ''bpq32.cfg'' file, I added the port number to the telnet port, where the fourth entry, ''10001'' is the port for my new application: In my ''bpq32.cfg'' file, I added the port number to the telnet port, where the fourth entry, ''10001'' is the port for my new application:
 <code> <code>
-CMDPORT=8005 63001 4565 10001+CMDPORT=8005 63001 10001
 </code> </code>
-and in the application list, I added a new entry, where ''3'' is the 0th-indexed 4th entry above.+and in the application list, I added a new entry, where ''HOST 2'' is due to being the 0th-indexed 3rd entry above.
 <code> <code>
-APPLICATION 4,ROLL,C 1 HOST S+APPLICATION 3,ROLL,C 1 HOST S
 </code> </code>
  
 ===== Configuring this as a service ===== ===== Configuring this as a service =====
-//todo write block describing adding a service to run this script//+Next we'll configure ''systemd'' service to make sure this script is always running and ready to accept connections.
  
 +Put this file in ''/etc/systemd/system/rick.service'':
  
 +<code>
 +# service for RRoAX25 server
 +# replace USER with the username under which you created the bpq-apps venv
  
 +[Unit]
 +Description="RRoAX25 service"
 +After=network.target
 +# this might be backwards, but I do need linbpq for this to work.
 +Requires=linbpq.service
  
 +[Service]
 +Type=simple
 +User=evan
 +Group=evan
 +WorkingDirectory=/home/USER/.local/bpq-apps/
 +ExecStart=/home/USER/.local/bpq-apps/bin/python /home/USER/.local/bpq-apps/scripts/rick.py -p 10001 -f /home/USER/.local/bpq-apps/data/messages.json
 +Restart=always
 +RestartSec=3
 +
 +[Install]
 +WantedBy=default.target
 +</code>
 +
 +Activate it by running:
 +<code>
 +sudo systemctl enable rick.service
 +sudo systemctl start rick.service
 +</code>
 +
 +Then check the status to confirm it is working:
 +
 +<code>
 +systemctl status rick.service
 +</code>
 +
 +If you need to edit the service file, reload it with:
 +<code>
 +systemctl daemon-reload
 +</code>
 +
 +Finally, to see who has used it:
 +<code>
 +journalctl -u rick.service -f
 +</code>
simple-bpq-telnet-application.1732814305.txt.gz · Last modified: by kc2ihx