Skip to content

Commit

Permalink
bots: Add a separate class "EmbeddedHandlerBots" in bot_lib.py.
Browse files Browse the repository at this point in the history
Add another class for bot type as embedded bot, for bots that run directly
on Zulip server. This class uses internal 'internal_send_messages' from actions.py
instead of using the 'send_message' function from Zulip Client class.

We haven't gone ahead with creating an abstract superclass right away.
We just have two versions for now, it would be easier to iterate a little
more on the interfaces, and then only add the superclass when we're ready
to lock down the interface.
  • Loading branch information
abhijeetkaur committed Jun 18, 2017
1 parent 1fc4064 commit c3d5057
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion api/bots_api/bot_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

from zulip import Client

# For dev setups, we can find the API in the repo itself.
if os.path.exists(os.path.join(our_dir, '../../zerver/lib')):
sys.path.insert(0, os.path.join(our_dir, '../../zerver/lib'))

from actions import internal_send_message

def exit_gracefully(signum, frame):
# type: (int, Optional[Any]) -> None
sys.exit(0)
Expand Down Expand Up @@ -66,7 +72,7 @@ def __init__(self, client):
' up the zuliprc file correctly.')
sys.exit(1)

def send_message(self, message):
def external_send_message(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
if self._rate_limit.is_legal():
return self._client.send_message(message)
Expand Down Expand Up @@ -105,6 +111,67 @@ def get_config_info(self, bot_name, section=None):
config.readfp(open(conf_file_path)) # type: ignore
return dict(config.items(section))

class EmbeddedBotHandler(object):
def __init__(self, client):
# type: (Client) -> None
# Only expose a subset of our Client's functionality
user_profile = client.get_profile()
self._rate_limit = RateLimit(20, 5)
self._client = client
try:
self.full_name = user_profile['full_name']
self.email = user_profile['email']
except KeyError:
logging.error('Cannot fetch user profile, make sure you have set'
' up the zuliprc file correctly.')
sys.exit(1)

def embedded_send_message(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
if self._rate_limit.is_legal():
return internal_send_message(realm=message['realm'], sender_email=message['sender'],
recipient_type_name=message['type'], recipients=message['to'],
subject=message['subject'], content=message['content'])
else:
self._rate_limit.show_error_and_exit()

def update_message(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
if self._rate_limit.is_legal():
return self._client.update_message(message)
else:
self._rate_limit.show_error_and_exit()

def send_reply(self, message, response):
# type: (Dict[str, Any], str) -> Dict[str, Any]
if message['type'] == 'private':
return self.send_message(dict(
sender_email=message['sender'],
realm=message['sender'].realm,
type='private',
to=[x['email'] for x in message['display_recipient'] if self.email != x['email']],
subject=message['subject'],
content=response,
))
else:
return self.send_message(dict(
sender_email=message['sender'],
type='stream',
realm=message['sender'].realm,
to=message['display_recipient'],
subject=message['subject'],
content=response,
))

def get_config_info(self, bot_name, section=None):
# type: (str, Optional[str]) -> Dict[str, Any]
conf_file_path = os.path.realpath(os.path.join(
our_dir, '..', 'bots', bot_name, bot_name + '.conf'))
section = section or bot_name
config = configparser.ConfigParser()
config.readfp(open(conf_file_path)) # type: ignore
return dict(config.items(section))


class StateHandler(object):
def __init__(self):
Expand Down

0 comments on commit c3d5057

Please sign in to comment.