-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from twilio-labs/open-source-mar-11-2021
open source push 3-11-2021
- Loading branch information
Showing
22 changed files
with
3,222 additions
and
3,912 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
from socless import * | ||
from slack_helpers import slack_client, find_user, get_channel_id, paginated_api_call | ||
from socless import socless_bootstrap | ||
from slack_helpers import slack_client, resolve_slack_target, paginated_api_call | ||
|
||
|
||
def handle_state(user_id, target_channel_id): | ||
""" | ||
Check if user is in certain slack channel | ||
:param user_id: user id of the user invoking the slash command | ||
:param target_channel_id: the channel id to be checked if user is in | ||
:return boolean value indicating whether the user is in the channel | ||
def handle_state(user_id: str, target_channel_id: str): | ||
"""Check if user is in a particular slack channel. | ||
Args: | ||
user_id: user id of the user invoking the slash command | ||
target_channel_id: the channel id to be checked if user is in | ||
Returns: | ||
ok: (bool) True if user is found in the channel | ||
""" | ||
|
||
ret = paginated_api_call(slack_client.conversations_members, | ||
'members', | ||
target_channel_id | ||
) | ||
if user_id not in ret['members']: | ||
return {"ok": False} | ||
else: | ||
response = paginated_api_call( | ||
api_method=slack_client.conversations_members, | ||
response_objects_name="members", | ||
channel=target_channel_id, | ||
) | ||
|
||
if user_id in response: | ||
return {"ok": True} | ||
|
||
return {"ok": False} | ||
|
||
|
||
def lambda_handler(event, context): | ||
return socless_bootstrap(event, context, handle_state) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
from socless import * | ||
from socless import socless_bootstrap | ||
import os | ||
import slack | ||
from slack_helpers import slack_client | ||
|
||
|
||
def handle_state(channel_name, is_private): | ||
def handle_state(channel_name, user_ids=[], is_private=False): | ||
"""Create slack channel and invite any provided slack_ids. | ||
Args: | ||
channel_name (str): The name of channel to be created. | ||
user_ids (list) : slack_ids of users invited to new channel. Private Channels require a user. | ||
is_private (boolean): if the channel is private. | ||
Token_Type: xoxp (slack legacy user token) | ||
Note: | ||
- See https://api.slack.com/methods/conversations.create for more details on how to create private channel | ||
""" | ||
Create slack channel | ||
Args: | ||
channel_name (str): The name of channel to be created. | ||
is_private (boolean): if the channel private | ||
Token_Type: xoxp | ||
Note: | ||
- See https://api.slack.com/methods/conversations.create for more details on how to create private channel | ||
""" | ||
|
||
SOCLESS_USER_TOKEN = os.environ['SOCLESS_USER_TOKEN'] | ||
slack_api_client = slack.WebClient(SOCLESS_USER_TOKEN) | ||
|
||
try: | ||
res = slack_api_client.conversations_create( | ||
name=channel_name, | ||
is_private=is_private | ||
|
||
if isinstance(user_ids, str): | ||
user_ids = user_ids.split(",") | ||
|
||
create_response = slack_client.conversations_create( | ||
name=channel_name, is_private=is_private, user_ids=user_ids | ||
) | ||
|
||
created_channel_id = create_response["channel"]["id"] | ||
bot_user_id = create_response["channel"]["creator"] | ||
|
||
if user_ids: | ||
user_ids = [x.strip() for x in user_ids if x != bot_user_id] | ||
|
||
invite_response = slack_client.conversations_invite( | ||
channel=created_channel_id, users=user_ids | ||
) | ||
created_channel_id = res["channel"]['id'] | ||
return { | ||
"ok": True, | ||
"created_channel_id": created_channel_id, | ||
"channel_name": channel_name | ||
} | ||
except Exception as e: | ||
s = str(e) | ||
err_msg = s.split("'detail': ", 1)[1] | ||
err_msg = err_msg[:len(err_msg) - 1] | ||
return { | ||
"ok": False, | ||
"error": err_msg | ||
} | ||
|
||
return { | ||
"ok": True, | ||
"created_channel_id": created_channel_id, | ||
"channel_name": channel_name, | ||
"added_users": user_ids, | ||
} | ||
|
||
|
||
def lambda_handler(event, context): | ||
return socless_bootstrap(event, context, handle_state) | ||
return socless_bootstrap(event, context, handle_state) |
Oops, something went wrong.