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:
parent
4c8d86c1d9
commit
56c59d915a
|
@ -80,6 +80,11 @@ class StateHandler(object):
|
||||||
def contains(self, key: Text) -> bool:
|
def contains(self, key: Text) -> bool:
|
||||||
return key in self.state_
|
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):
|
class ExternalBotHandler(object):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -129,6 +134,9 @@ class ExternalBotHandler(object):
|
||||||
def storage(self) -> StateHandler:
|
def storage(self) -> StateHandler:
|
||||||
return self._storage
|
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]:
|
def send_message(self, message: (Dict[str, Any])) -> Dict[str, Any]:
|
||||||
if not self._rate_limit.is_legal():
|
if not self._rate_limit.is_legal():
|
||||||
self._rate_limit.show_error_and_exit()
|
self._rate_limit.show_error_and_exit()
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import configparser
|
import configparser
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from zulip_bots.lib import BotIdentity
|
||||||
|
|
||||||
class SimpleStorage:
|
class SimpleStorage:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.data = dict()
|
self.data = dict()
|
||||||
|
@ -36,6 +38,9 @@ class TerminalBotHandler:
|
||||||
self.storage = SimpleStorage()
|
self.storage = SimpleStorage()
|
||||||
self.message_server = SimpleMessageServer()
|
self.message_server = SimpleMessageServer()
|
||||||
|
|
||||||
|
def identity(self):
|
||||||
|
return BotIdentity("bot name", "bot-email@domain")
|
||||||
|
|
||||||
def send_message(self, message):
|
def send_message(self, message):
|
||||||
if message['type'] == 'stream':
|
if message['type'] == 'stream':
|
||||||
print('''
|
print('''
|
||||||
|
|
|
@ -21,6 +21,7 @@ from zulip_bots.test_file_utils import (
|
||||||
read_bot_fixture_data,
|
read_bot_fixture_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from zulip_bots.lib import BotIdentity
|
||||||
|
|
||||||
class StubBotHandler:
|
class StubBotHandler:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
@ -33,6 +34,9 @@ class StubBotHandler:
|
||||||
def reset_transcript(self) -> None:
|
def reset_transcript(self) -> None:
|
||||||
self.transcript = [] # type: List[Tuple[str, Dict[str, Any]]]
|
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]:
|
def send_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
self.transcript.append(('send_message', message))
|
self.transcript.append(('send_message', message))
|
||||||
return self.message_server.send(message)
|
return self.message_server.send(message)
|
||||||
|
|
Loading…
Reference in a new issue