Skip to content

Commit

Permalink
Implement #10; release 0.7.6
Browse files Browse the repository at this point in the history
  • Loading branch information
double-fault committed Apr 15, 2019
1 parent 558c73b commit fede4f7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Some incomplete documentation of Botpy exists here: https://botpy.readthedocs.io

# Changelog

- **v0.7.4**: Fixes privilege level bug.
- **v0.7.5**: Allows the usage of the `send_aggressively` option in CE.
- **v0.7.6**: Add optional event callback; #10.
- **v0.7.5**: Allows the usage of the `send_aggressively` option in CE; fix LICENSE copyright.
- **v0.7.4**: Fixes privilege level bug.
- **v0.7.3**: Finally fixes the bug introduced in 0.6.7. All default commands have been moved to `AllCommands.py`; this also fixes #11.
- **v0.7.2**: Another attempt at 0.7.1.
Expand Down
13 changes: 13 additions & 0 deletions Source/Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, bot_name, commands, room_ids, background_tasks=[], host='stac
self._startup_message = self.name + " starting..."
self._standby_message = "Switching to standby mode."
self._failover_message = "Failover received."
self._on_event_callback = lambda: None

background_tasks.append(BackgroundTask(self._command_manager.cleanup_finished_commands, interval=3))
self._background_task_manager = BackgroundTaskManager(background_tasks)
Expand All @@ -69,6 +70,15 @@ def __init__(self, bot_name, commands, room_ids, background_tasks=[], host='stac
except ValueError as value_error:
logging.error(str(value_error))

def add_event_callback(self, event_callback):
"""
'event_callback' will now be called everytime a new event occurs in a room which the bot
is present in.
"""
if not callable(event_callback):
raise TypeError('Bot.add_event_callback: \'event_callback\' is not callable!')
self._on_event_callback = event_callback

def add_alias(self, alias):
"""
Adds a new name alias for the bot.
Expand Down Expand Up @@ -361,6 +371,9 @@ def _handle_event(self, event, _):
logging.info("(%s) %s (user id: %d) entered the room" % (event.room.name, event.user.name, event.user.id))
event.room.add_user(event.user)

# Call event callback
self._on_event_callback(event)

def _save_users(self):
for room in self._rooms:
filename = self._convert_to_save_filename(room.id)
Expand Down
40 changes: 40 additions & 0 deletions examples/simple_chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 15th April 2019

import os
import getpass
import BotpySE as bp

if "ChatbotEmail" in os.environ:
email = os.environ["ChatbotEmail"]
else:
email = input("Email: ")

if "ChatbotPass" in os.environ:
password = os.environ["ChatbotPass"]
else:
password = getpass.getpass("Password: ")

commands = bp.all_commands # We are using all of Botpy's default commands, and not creating any of our own.

rooms = [1] # This bot will join only one room, that is the Sandbox room (room id 1) on StackOverflow chat.

background_tasks = [] # We will not be having any background tasks in this bot.
# All tasks required to keep the bot alive such as monitoring rooms will be automatically added.

host = "stackoverflow.com" # Our chat room is on StackOverflow chat.

bot = bp.Bot("TestBot", commands, rooms, background_tasks, host, email, password)

# Erase email and password from memory.
email = ""
password = ""

def test(event):
print(event)
print("woot")

bot.add_event_callback(test)

# Start the bot. The bot will run forever till a stop command is run. The reboot command will automatically reboot the bot.
# All background tasks specified and those automatically added will continue running till the bot stops.
bot.start()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup (
name = "BotpySE",
packages = ["BotpySE"],
version = "0.7.5",
version = "0.7.6",
description = "A python framework to create chatbots on the StackExchange network.",
author = "Ashish Ahuja",
author_email = "[email protected]",
Expand Down

0 comments on commit fede4f7

Please sign in to comment.