1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-22 14:04:18 +00:00

More work on the basic bot code

pass, ..., and NotImplementedError() are unfinished parts of code
This commit is contained in:
Steffo 2017-12-08 16:44:11 +01:00
parent 94d4ac4ff9
commit 56d06ea1d2
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D
3 changed files with 49 additions and 7 deletions

23
core.py
View file

@ -4,6 +4,7 @@ import configparser
import telegram import telegram
import threading import threading
import time import time
import strings
# Check if a configuration file exists, create one if it doesn't and get the template version number. # 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: with open("config/template_config.ini") as template_file:
@ -52,25 +53,33 @@ except telegram.error.Unauthorized:
"Fix it, then restart this script.") "Fix it, then restart this script.")
sys.exit(1) sys.exit(1)
# Create a dictionary containing the chat instances threads and the pipes from the main thread to the chat instance thread # Create a dictionary linking the chat ids to the ChatWorker objects
# {"1234": (<Thread>, <Pipe>)} # {"1234": <ChatWorker>}
chat_threads = {} chat_workers = {}
# Current update offset; if None it will get the last 100 unparsed messages # Current update offset; if None it will get the last 100 unparsed messages
update_offset = None next_update = None
# Main loop of the program # Main loop of the program
while True: while True:
# Get a new batch of 100 updates and mark the last 100 parsed as read # 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 # Parse all the updates
for update in updates: for update in updates:
# If the update is a message... # If the update is a message...
if update.message is not None: 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 the update is a inline keyboard press...
if update.inline_query is not None: 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) # Temporarily prevent rate limits (remove this later)
time.sleep(5) time.sleep(5)

5
strings.py Normal file
View file

@ -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."

28
worker.py Normal file
View file

@ -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()