-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbot_local.py
More file actions
executable file
·68 lines (50 loc) · 1.64 KB
/
bot_local.py
File metadata and controls
executable file
·68 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""Run the bot locally to test it out"""
import asyncio
import sys
from bot import (
Bot,
get_channels_info,
get_envs,
load_repos_info,
)
class ConsoleBot(Bot):
"""Fake console bot"""
async def say(self, *, channel_id, text="", attachments=None, message_type=""):
"""Print messages to stdout"""
attachment_text = ""
if attachments is not None:
attachment_text = attachments[0].get("text", "")
line = f"{' '.join(word for word in [text, attachment_text, message_type] if word)} "
print(f"\033[92m{line}\033[0m")
async def async_main():
"""Handle command line arguments and run a command"""
envs = get_envs()
if len(sys.argv) < 3:
raise Exception("Expected arguments: channel_name command arg1 arg2...")
_, channel_name, *words = sys.argv
channels_info = await get_channels_info(envs["SLACK_ACCESS_TOKEN"])
try:
channel_id = channels_info[channel_name]
except KeyError as ex:
raise Exception(f"Unable to find channel by name {channel_name}") from ex
repos_info = load_repos_info(channels_info)
bot = ConsoleBot(
doof_id="console",
slack_access_token=envs["SLACK_ACCESS_TOKEN"],
github_access_token=envs["GITHUB_ACCESS_TOKEN"],
timezone=envs["TIMEZONE"],
npm_token=envs["NPM_TOKEN"],
repos_info=repos_info,
)
await bot.startup()
await bot.handle_message(
manager="mitodl_user",
channel_id=channel_id,
words=words,
)
def main():
"""Main function"""
asyncio.run(async_main())
if __name__ == "__main__":
main()