Skip to content

Commit 8bff2b0

Browse files
committed
Update core to use mattermostautodriver instead of mattermostdriver (attzonko#337)
* create_ephemeral_post -> create_post_ephemeral * create_reaction -> save_reaction * create_direct_message_channel -> create_direct_channel * get_channels_for_user -> get_channels_for_team_for_user * get_thread -> get_post_thread * get_reactions_of_post -> get_reactions * webhooks.call_webhook -> call_webhook * mattermostdriver -> mattermostautodriver
1 parent 54f0aae commit 8bff2b0

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

mmpy_bot/driver.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import queue
2+
import warnings
23
from pathlib import Path
34
from typing import Dict, List, Optional, Sequence, Union
45

5-
import mattermostdriver
6+
import mattermostautodriver
67
from aiohttp.client import ClientSession
78

89
from mmpy_bot.threadpool import ThreadPool
910
from mmpy_bot.webhook_server import WebHookServer
1011
from mmpy_bot.wrappers import Message, WebHookEvent
1112

1213

13-
class Driver(mattermostdriver.Driver):
14+
class Driver(mattermostautodriver.Driver):
1415
user_id: str = ""
1516
username: str = ""
1617

1718
def __init__(self, *args, num_threads=10, **kwargs):
18-
"""Wrapper around the mattermostdriver Driver with some convenience functions
19-
and attributes.
19+
"""Wrapper around the mattermostautodriver Driver with some convenience
20+
functions and attributes.
2021
2122
Arguments:
2223
- num_threads: int, number of threads to use for the default worker threadpool.
@@ -67,7 +68,7 @@ def create_post(
6768
)
6869

6970
if ephemeral_user_id:
70-
return self.posts.create_ephemeral_post(
71+
return self.posts.create_post_ephemeral(
7172
{
7273
"user_id": ephemeral_user_id,
7374
"post": post,
@@ -77,9 +78,15 @@ def create_post(
7778
return self.posts.create_post(post)
7879

7980
def get_thread(self, post_id: str):
80-
"""Wrapper around driver.posts.get_thread, which for some reason returns
81+
warnings.warn(
82+
"get_thread is deprecated. Use get_post_thread instead", DeprecationWarning
83+
)
84+
return self.get_post_thread(post_id)
85+
86+
def get_post_thread(self, post_id: str):
87+
"""Wrapper around driver.posts.get_post_thread, which for some reason returns
8188
duplicate and wrongly ordered entries in the ordered list."""
82-
thread_info = self.posts.get_thread(post_id)
89+
thread_info = self.posts.get_post_thread(post_id)
8390

8491
id_stamps = []
8592
for id, post in thread_info["posts"].items():
@@ -96,7 +103,7 @@ def get_user_info(self, user_id: str):
96103

97104
def react_to(self, message: Message, emoji_name: str):
98105
"""Adds an emoji reaction to the given message."""
99-
return self.reactions.create_reaction(
106+
return self.reactions.save_reaction(
100107
{
101108
"user_id": self.user_id,
102109
"post_id": message.id,
@@ -158,9 +165,9 @@ def direct_message(
158165
):
159166
# Private/direct messages are sent to a special channel that
160167
# includes the bot and the recipient
161-
direct_id = self.channels.create_direct_message_channel(
162-
[self.user_id, receiver_id]
163-
)["id"]
168+
direct_id = self.channels.create_direct_channel([self.user_id, receiver_id])[
169+
"id"
170+
]
164171

165172
return self.create_post(
166173
channel_id=direct_id,
@@ -192,10 +199,22 @@ def upload_files(
192199
) -> List[str]:
193200
"""Given a list of file paths and the channel id, uploads the corresponding
194201
files and returns a list their internal file IDs."""
195-
file_dict = {}
202+
file_list = []
196203
for path in file_paths:
197204
path = Path(path)
198-
file_dict[path.name] = Path(path).read_bytes()
205+
# Note: 'files' should be a name of an expected attribute in the body
206+
# but seems to be ignored when simply uploading files to mattermost
207+
file_list.append(
208+
(
209+
"files",
210+
(
211+
path.name,
212+
Path(path).read_bytes(),
213+
),
214+
)
215+
)
199216

200-
result = self.files.upload_file(channel_id, file_dict)
217+
result = self.files.upload_file(
218+
files=file_list, data={"channel_id": channel_id}
219+
)
201220
return list(info["id"] for info in result["file_infos"])

mmpy_bot/plugins/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pathlib import Path
55

66
import click
7-
import mattermostdriver
7+
import mattermostautodriver
88

99
from mmpy_bot.function import listen_to
1010
from mmpy_bot.plugins.base import Plugin
@@ -65,7 +65,7 @@ async def hello_ephemeral(self, message: Message):
6565
permissions."""
6666
try:
6767
self.driver.reply_to(message, "hello sender!", ephemeral=True)
68-
except mattermostdriver.exceptions.NotEnoughPermissions:
68+
except mattermostautodriver.exceptions.NotEnoughPermissions:
6969
self.driver.reply_to(
7070
message, "I do not have permission to create ephemeral posts!"
7171
)
@@ -85,7 +85,7 @@ async def hello_file(self, message: Message):
8585
@listen_to("^!hello_webhook$", re.IGNORECASE, category="webhook")
8686
async def hello_webhook(self, message: Message):
8787
"""A webhook that says hello."""
88-
self.driver.webhooks.call_webhook(
88+
self.driver.client.call_webhook(
8989
"eauegoqk4ibxigfybqrsfmt48r",
9090
options={
9191
"username": "webhook_test", # Requires the right webhook permissions

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
aiohttp>=3.7.4.post0
22
click>=7.0
3-
mattermostdriver>=7.3.2
3+
mattermostautodriver>=1.2.0
44
schedule>=0.6.0
55
Sphinx>=1.3.3

tests/integration_tests/test_direct_message_plugin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def bot_and_user_direct_channel(channel):
4141
# which is implemented by mattermost as a channel
4242
driver.create_post(OFF_TOPIC_ID, trigger)
4343

44-
user_channels = driver.channels.get_channels_for_user(driver.user_id, TEAM_ID)
44+
user_channels = driver.channels.get_channels_for_team_for_user(
45+
driver.user_id, TEAM_ID
46+
)
4547
channels = list(filter(bot_and_user_direct_channel, user_channels))
4648

4749
# We need to wait for the reply to be processed by mattermost
@@ -51,7 +53,7 @@ def bot_and_user_direct_channel(channel):
5153
for _ in range(retries):
5254
if len(channels) != 1:
5355
time.sleep(RESPONSE_TIMEOUT)
54-
user_channels = driver.channels.get_channels_for_user(
56+
user_channels = driver.channels.get_channels_for_team_for_user(
5557
driver.user_id, TEAM_ID
5658
)
5759
channels = list(filter(bot_and_user_direct_channel, user_channels))

tests/integration_tests/test_example_plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ def test_admin(self, driver):
2828
# Since this is not a direct message, we expect no reply at all
2929
post_id = driver.create_post(OFF_TOPIC_ID, "@main_bot admin")["id"]
3030
time.sleep(RESPONSE_TIMEOUT)
31-
thread_info = driver.get_thread(post_id)
31+
thread_info = driver.get_post_thread(post_id)
3232
assert len(thread_info["order"]) == 1
3333

3434
# For the direct message, we expect to have insufficient permissions, since
3535
# our name isn't admin
36-
private_channel = driver.channels.create_direct_message_channel(
36+
private_channel = driver.channels.create_direct_channel(
3737
[driver.user_id, MAIN_BOT_ID]
3838
)["id"]
3939
post = driver.create_post(private_channel, "admin")
@@ -93,7 +93,7 @@ def test_hello_ephemeral(self, driver):
9393
def test_react(self, driver):
9494
post_id = driver.create_post(OFF_TOPIC_ID, "@main_bot hello_react")["id"]
9595
time.sleep(RESPONSE_TIMEOUT)
96-
reactions = driver.reactions.get_reactions_of_post(post_id)
96+
reactions = driver.reactions.get_reactions(post_id)
9797
assert len(reactions) == 1
9898
assert reactions[0]["emoji_name"] == "+1"
9999

tests/integration_tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def expect_reply(driver: Driver, post: Dict, wait=RESPONSE_TIMEOUT, retries=1):
2727
reply = None
2828
for _ in range(retries + 1):
2929
time.sleep(wait)
30-
thread_info = driver.get_thread(post["id"])
30+
thread_info = driver.get_post_thread(post["id"])
3131
print(thread_info)
3232
reply_id = thread_info["order"][-1]
3333
if reply_id != post["id"]:

0 commit comments

Comments
 (0)