From 56d06ea1d29d5361d6baf9e785ad475de993b788 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 8 Dec 2017 16:44:11 +0100 Subject: [PATCH] More work on the basic bot code pass, ..., and NotImplementedError() are unfinished parts of code --- core.py | 23 ++++++++++++++++------- strings.py | 5 +++++ worker.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 strings.py create mode 100644 worker.py diff --git a/core.py b/core.py index 2e577bc..8c82500 100644 --- a/core.py +++ b/core.py @@ -4,6 +4,7 @@ import configparser import telegram import threading import time +import strings # Check if a configuration file exists, create one if it doesn't and get the template version number. with open("config/template_config.ini") as template_file: @@ -52,25 +53,33 @@ except telegram.error.Unauthorized: "Fix it, then restart this script.") sys.exit(1) -# Create a dictionary containing the chat instances threads and the pipes from the main thread to the chat instance thread -# {"1234": (, )} -chat_threads = {} +# Create a dictionary linking the chat ids to the ChatWorker objects +# {"1234": } +chat_workers = {} # Current update offset; if None it will get the last 100 unparsed messages -update_offset = None +next_update = None # Main loop of the program while True: # Get a new batch of 100 updates and mark the last 100 parsed as read - updates = bot.get_updates(offset=update_offset) + updates = bot.get_updates(offset=next_update) # Parse all the updates for update in updates: # If the update is a message... if update.message is not None: - ... + # Ensure the message has been sent in a private chat + if update.message.chat.type != "private": + # Notify the chat + bot.send_message(update.message.chat.id, strings.error_nonprivate_chat) + # Skip the update + continue + # TODO: add stuff here # If the update is a inline keyboard press... if update.inline_query is not None: - ... + pass + # Mark the update as read by increasing the update_offset + next_update = update.update_id + 1 # Temporarily prevent rate limits (remove this later) time.sleep(5) \ No newline at end of file diff --git a/strings.py b/strings.py new file mode 100644 index 0000000..bf32842 --- /dev/null +++ b/strings.py @@ -0,0 +1,5 @@ +# Strings / localization file for greed +# Can be edited, but DON'T REMOVE THE REPLACEMENT FIELDS (words surrounded by {curly braces}) + +# Error: message received not in a private chat +error_nonprivate_chat = "⚠️ Questo bot funziona solo in chat private." \ No newline at end of file diff --git a/worker.py b/worker.py new file mode 100644 index 0000000..34c645d --- /dev/null +++ b/worker.py @@ -0,0 +1,28 @@ +import multiprocessing +import telegram + +class ChatWorker: + """A worker for a single conversation. A new one should be created every time the /start command is sent.""" + + def __init__(self, bot: telegram.Bot, chat: telegram.Chat): + # A pipe connecting the main process to the chat process is created + in_pipe, out_pipe = multiprocessing.Pipe(duplex=False) + # The sending pipe is stored in the ChatWorker class, allowing the forwarding of messages to the chat process + self.pipe = in_pipe + # A new process running the conversation handler is created, and the receiving pipe is passed to its arguments to enable the receiving of messages + self.process = multiprocessing.Process(target=conversation_handler, args=(bot, chat, out_pipe)) + + def start(self): + """Start the worker process.""" + self.process.start() + + def stop(self): + # Gracefully stop the worker process + # TODO: send a stop message to the process + raise NotImplementedError() + # Wait for the process to stop + self.process.join() + + +def conversation_handler(bot: telegram.Bot, chat: telegram.Chat, pipe: multiprocessing.Connection): + raise NotImplementedError() \ No newline at end of file