From de29ddb054584e216ade711edcf557dedaa928f1 Mon Sep 17 00:00:00 2001 From: randogoth Date: Wed, 24 May 2023 16:37:56 +0300 Subject: [PATCH] fixed missing folder, more instance options --- README.md | 3 ++- lxmfbot.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index afaad09..8ed594d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ $ pip install -r requirements.txt ## Usage -- Instantiate the class with a name for the bot as parameter +- Instantiate the class with a `name` for the bot as parameter. + - Two additional options the class takes as parameters are `announce` to define the announce interval in seconds (defaults to `360`) and an `announce_immediately` boolean to define whether the bot should announce itself immediately after instantiation or not (defaults to `False`) - Use the `received` decorator to define functions for parsing received messages - Use the `.send(recipient_hash, message)` or `msg.reply(message)` methods to send messages - Launch the bot using the `run` method diff --git a/lxmfbot.py b/lxmfbot.py index 04522d6..7e72c1e 100644 --- a/lxmfbot.py +++ b/lxmfbot.py @@ -13,14 +13,18 @@ class LXMFBot: delivery_callbacks = [] receipts = [] queue = Queue(maxsize = 5) + announce_time = 360 - def __init__(self, name='LXMFBot'): + def __init__(self, name='LXMFBot', announce=360, announce_immediately=False): # initialize identiy self.name = name + self.announce_time = announce dirs = AppDirs("LXMFBot", "randogoth") self.config_path = os.path.join(dirs.user_data_dir, name) idfile = os.path.join(self.config_path, "identity") + if not os.path.isdir(dirs.user_data_dir): + os.mkdir(dirs.user_data_dir) if not os.path.isdir(self.config_path): os.mkdir(self.config_path) if not os.path.isfile(idfile): @@ -29,6 +33,11 @@ class LXMFBot: id.to_file(idfile) self.id = RNS.Identity.from_file(idfile) RNS.log('Loaded identity from file', RNS.LOG_INFO) + if announce_immediately: + af = os.path.join(self.config_path, "announce") + if os.path.isfile(af): + os.remove(af) + RNS.log('Announcing now. Timer reset.', RNS.LOG_INFO) # start RNS and LXMFRouter RNS.Reticulum(loglevel=RNS.LOG_VERBOSE) @@ -50,7 +59,7 @@ class LXMFBot: RNS.log('Recent announcement', RNS.LOG_DEBUG) else: with open(announce_path, "w+") as af: - next_announce = int(time.time()) + 1800 + next_announce = int(time.time()) + self.announce_time af.write(str(next_announce)) self.local.announce() RNS.log('Announcement sent, expr set 1800 seconds', RNS.LOG_INFO)