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

Even more work on the basic bot code

This commit is contained in:
Steffo 2017-12-11 12:47:46 +01:00
parent 14aa9697f7
commit d3f420cf6e
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
2 changed files with 37 additions and 3 deletions

34
core.py
View file

@ -5,6 +5,7 @@ import telegram
import threading import threading
import time import time
import strings import strings
import worker
# 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:
@ -75,10 +76,39 @@ while True:
bot.send_message(update.message.chat.id, strings.error_nonprivate_chat) bot.send_message(update.message.chat.id, strings.error_nonprivate_chat)
# Skip the update # Skip the update
continue continue
# TODO: add stuff here # If the message is a start command...
if update.message.text == "/start":
# Check if a worker already exists for that chat
old_worker = chat_workers.get(update.message.chat.id)
# If it exists, gracefully stop the worker
if old_worker:
old_worker.stop()
# Initialize a new worker for the chat
new_worker = worker.ChatWorker(bot, update.message.chat)
# Start the worker
new_worker.start()
# Store the worker in the dictionary
chat_workers[update.message.chat.id] = new_worker
# Skip the update
continue
# Otherwise, forward the update to the corresponding worker
receiving_worker = chat_workers.get(update.message.chat.id)
# Ensure a worker exists for the chat
if receiving_worker is None:
# Suggest that the user restarts the chat with /start
bot.send_message(update.message.chat.id, strings.error_no_worker_for_chat)
# Forward the update to the worker
receiving_worker.pipe.send(update)
# 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 # Forward the update to the corresponding worker
receiving_worker = chat_workers.get(update.message.chat.id)
# Ensure a worker exists for the chat
if receiving_worker is None:
# Suggest that the user restarts the chat with /start
bot.send_message(update.message.chat.id, strings.error_no_worker_for_chat)
# Forward the update to the worker
receiving_worker.pipe.send(update)
# Mark the update as read by increasing the update_offset # Mark the update as read by increasing the update_offset
next_update = update.update_id + 1 next_update = update.update_id + 1
# Temporarily prevent rate limits (remove this later) # Temporarily prevent rate limits (remove this later)

View file

@ -3,3 +3,7 @@
# Error: message received not in a private chat # Error: message received not in a private chat
error_nonprivate_chat = "⚠️ Questo bot funziona solo in chat private." error_nonprivate_chat = "⚠️ Questo bot funziona solo in chat private."
# Error: a message was sent in a chat, but no worker exists for that chat. Suggest the creation of a new worker with /start
error_no_worker_for_chat = "⚠️ La conversazione con il bot si è interrotta.\n" \
"Per riavviarla, manda il comando /start al bot."