Ejemplo n.º 1
0
def unextract_channels(text, bot):
    statsd = stats.get_statsd_client()
    with statsd.timer('parser.unextract_channels'):
        # Example: #my-channel
        _channel_labels = re.findall('(^#[\w\-_]+| #[\w\-_]+)', text)
        for label in _channel_labels:
            channel = slack.get_channel_by_name(bot, label.strip())
            if not channel:
                continue
            text = text.replace(
                '#{}'.format(channel['name']),
                '<#{0}|{1}>'.format(channel['id'], channel['name']))
        return text
Ejemplo n.º 2
0
Archivo: api.py Proyecto: lyft/omnibot
def get_channel_by_name(team_name, bot_name, channel_name):
    """
    Returns a channel object from slack, for the provided `channel_name` in
    the `team_name` using the specified `bot_name`.

    .. :quickref: Channel; Get a channel from a team, via the channel_name

    **Example request**:

    .. sourcecode:: http

       GET /api/v1/slack/get_channel/myteam/mybot/general HTTP/1.1

    **Example response**:

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

       {
         "channel": {
           "id": "C4VQ6NUNN",
           "name": "general",
           "is_channel": true,
           "created": 1491515285,
           "creator": "U4WF56QGP",
           "is_archived": false,
           "is_general": true,
           "unlinked": 0,
           "name_normalized": "general",
           "is_shared": false,
           "is_org_shared": false,
           "is_member": false,
           "is_private": false,
           "is_mpim": false,
           "members": [
             "U4WF56QGP",
             "U6HQQ19EC",
             "U6J3LTKSQ",
             "U6J4EGP44",
             "U6JDF1JBU",
             "U6JEGTFDZ",
             "U6JERPMJ7",
             "U6JG691MJ",
             "U6JGEQ0J0",
             "U6SAVUK44",
             "U750C7B37",
             "U7DH0H802"
           ],
           "topic": {
             "value": "test123",
             "creator": "U6J3LTKSQ",
             "last_set": 1507156612
           },
           "purpose": {
             "value": "This channel is for team-wide communication.",
             "creator": "",
             "last_set": 0
           },
           "previous_names": [],
           "num_members": 9
         }
      }

    :param team_name: The team to search for this channel, as configured in
                      omnibot.
    :type team_name: str
    :param bot_name: The bot to use for the request, as configured in omnibot.
    :type bot_name: str
    :param channel_name: The name of the channel to get.
    :type channel_name: str
    :reqheader x-envoy-internal: Header that indicates whether or not this
                                 request is coming from an internal service
                                 or not. This is auto-set by envoy and doesn't
                                 need to be explicitly set.
    :resheader Content-Type: application/json
    :statuscode 200: success
    :statuscode 404: channel with specified channel_name could not be found
                     in the specified team using the specified bot.
    """
    logger.debug(
        'Getting channel for team={} bot={} channel={}.',
        extra={
            'team': team_name,
            'bot': bot_name,
            'channel': channel_name,
        },
    )
    try:
        team = Team.get_team_by_name(team_name)
    except TeamInitializationError:
        return jsonify({'error': 'provided team name was not found.'}), 404
    try:
        bot = Bot.get_bot_by_name(team, bot_name)
    except BotInitializationError:
        return jsonify({'error': 'provided bot name was not found.'}), 404
    channel = slack.get_channel_by_name(bot, channel_name)
    if channel is None:
        logger.debug(
            'Failed to get channel',
            extra=merge_logging_context(
                {'channel': channel_name},
                bot.logging_context,
            ),
        )
        return jsonify({'error': 'provided channel_name was not found.'}), 404
    return jsonify(channel)