bots: Support determining bot runtime identity.

This improves the ability of a bot to specify how to mention it,
which varies at run-time depending upon the identity used to run it;
this is commonly used in many bot help commands.
This commit is contained in:
neiljp (Neil Pilgrim) 2017-08-01 14:09:36 -07:00 committed by showell
parent 4c8d86c1d9
commit 56c59d915a
3 changed files with 17 additions and 0 deletions

View file

@ -80,6 +80,11 @@ class StateHandler(object):
def contains(self, key: Text) -> bool:
return key in self.state_
class BotIdentity(object):
def __init__(self, name: str, email: str) -> None:
self.name = name
self.email = email
self.mention = '@**' + name + '**'
class ExternalBotHandler(object):
def __init__(
@ -129,6 +134,9 @@ class ExternalBotHandler(object):
def storage(self) -> StateHandler:
return self._storage
def identity(self) -> BotIdentity:
return BotIdentity(self.full_name, self.email)
def send_message(self, message: (Dict[str, Any])) -> Dict[str, Any]:
if not self._rate_limit.is_legal():
self._rate_limit.show_error_and_exit()

View file

@ -1,6 +1,8 @@
import configparser
import sys
from zulip_bots.lib import BotIdentity
class SimpleStorage:
def __init__(self):
self.data = dict()
@ -36,6 +38,9 @@ class TerminalBotHandler:
self.storage = SimpleStorage()
self.message_server = SimpleMessageServer()
def identity(self):
return BotIdentity("bot name", "bot-email@domain")
def send_message(self, message):
if message['type'] == 'stream':
print('''

View file

@ -21,6 +21,7 @@ from zulip_bots.test_file_utils import (
read_bot_fixture_data,
)
from zulip_bots.lib import BotIdentity
class StubBotHandler:
def __init__(self) -> None:
@ -33,6 +34,9 @@ class StubBotHandler:
def reset_transcript(self) -> None:
self.transcript = [] # type: List[Tuple[str, Dict[str, Any]]]
def identity(self) -> BotIdentity:
return BotIdentity(self.full_name, self.email)
def send_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
self.transcript.append(('send_message', message))
return self.message_server.send(message)