Skip to content

Commit

Permalink
Merge branch 'mattermost_botacct' of https://github.com/btotharye/rasa
Browse files Browse the repository at this point in the history
…into btotharye-mattermost_botacct
  • Loading branch information
tmbo committed Feb 20, 2020
2 parents 8d19242 + cb0104c commit 23af1a3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog/5250.enhancement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for mattermost connector to use bot accounts.
11 changes: 9 additions & 2 deletions docs/user-guide/connectors/mattermost.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Once you have them you can add these to your ``credentials.yml``.

Getting Credentials
^^^^^^^^^^^^^^^^^^^
Mattermost now uses bot accounts for better security. So you can use their guide to create
your bot to get your token required for the `credentials.yml` file.

For more information on creating a bot account please see
`Bot Creation <https://docs.mattermost.com/developer/bot-accounts.html#bot-account-creation>`_.

For information on converting existing user account into bot account please see
`User Conversion <https://docs.mattermost.com/developer/bot-accounts.html#how-do-i-convert-an-existing-account-to-a-bot-account>`_.

**How to set up the outgoing webhook:**

Expand Down Expand Up @@ -50,8 +58,7 @@ you need to supply a ``credentials.yml`` with the following content:
mattermost:
url: "https://chat.example.com/api/v4"
team: "community"
user: "[email protected]" # actual username of your bot user, not displayname
pw: "password"
token: "xxxxx" # the token for the bot account from creating the bot step.
webhook_url: "https://server.example.com/webhooks/mattermost/webhook"
The endpoint for receiving Mattermost channel messages
Expand Down
36 changes: 9 additions & 27 deletions rasa/core/channels/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ def __init__(
self,
url: Text,
team: Text,
user: Text,
pw: Text,
token: Text,
bot_channel: Text,
webhook_url: Optional[Text],
) -> None:
self.url = url
self.team = team
self.user = user
self.pw = pw
self.token = token
self.bot_channel = bot_channel
self.webhook_url = webhook_url

super().__init__(url, team)
super().login(user, pw)
super().__init__(url, team, token=token)

async def send_text_message(
self, recipient_id: Text, text: Text, **kwargs: Any
Expand Down Expand Up @@ -120,15 +117,12 @@ def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChanne
return cls(
credentials.get("url"),
credentials.get("team"),
credentials.get("user"),
credentials.get("pw"),
credentials.get("token"),
credentials.get("webhook_url"),
)
# pytype: enable=attribute-error

def __init__(
self, url: Text, team: Text, user: Text, pw: Text, webhook_url: Text
) -> None:
def __init__(self, url: Text, team: Text, token: Text, webhook_url: Text) -> None:
"""Create a Mattermost input channel.
Needs a couple of settings to properly authenticate and validate
messages.
Expand All @@ -137,16 +131,14 @@ def __init__(
url: Your Mattermost team url including /v4 example
https://mysite.example.com/api/v4
team: Your mattermost team name
user: Your mattermost userid that will post messages
pw: Your mattermost password for your user
token: Your mattermost bot token
webhook_url: The mattermost callback url as specified
in the outgoing webhooks in mattermost example
https://mysite.example.com/webhooks/mattermost/webhook
"""
self.url = url
self.team = team
self.user = user
self.pw = pw
self.token = token
self.webhook_url = webhook_url

async def message_with_trigger_word(
Expand All @@ -165,12 +157,7 @@ async def message_with_trigger_word(

try:
out_channel = MattermostBot(
self.url,
self.team,
self.user,
self.pw,
self.bot_channel,
self.webhook_url,
self.url, self.team, self.token, self.bot_channel, self.webhook_url,
)
user_msg = UserMessage(
text,
Expand Down Expand Up @@ -198,12 +185,7 @@ async def action_from_button(

try:
out_channel = MattermostBot(
self.url,
self.team,
self.user,
self.pw,
self.bot_channel,
self.webhook_url,
self.url, self.team, self.token, self.bot_channel, self.webhook_url,
)
context_action = UserMessage(
action,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ slackclient==1.3.1
python-telegram-bot==11.1.0
twilio==6.26.3
webexteamssdk==1.1.1
mattermostwrapper==2.1
mattermostwrapper==2.2
rocketchat_API==0.6.31
colorhash==1.0.2
pika==1.0.1
Expand Down
5 changes: 2 additions & 3 deletions tests/core/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,9 @@ def test_mattermost_channel():
url="http://chat.example.com/api/v4",
# the name of your team for mattermost
team="community",
# the username of your bot user that will post messages
user="[email protected]",
# the bot token of the bot account that will post messages
token="xxxxx",
# the password of your bot user that will post messages
pw="password",
# the webhook-url your bot should listen for messages
webhook_url="YOUR_WEBHOOK_URL",
)
Expand Down

0 comments on commit 23af1a3

Please sign in to comment.