示例#1
0
    def build_message(self):
        event_class = None

        # Handle supported event types
        if self.source == 'aws.health':
            from events.cloudwatch.health import HealthEvent
            event_class = HealthEvent

        if event_class:
            return event_class(self.event).build_message()

        # Create message and attachment
        message = SlackMessage(text=self.detail_type, )
        attachment = SlackAttachment(title=self.detail_type,
                                     text=json.dumps(self.detail),
                                     footer='CloudWatch Event')

        # Source
        attachment.add_field(title='Source', value=self.source)

        # Account
        attachment.add_field(title='Account', value=self.account_id)

        # Region
        attachment.add_field(title='Region', value=self.region)

        # Resources
        if self.resources:
            attachment.add_field(title='Resources',
                                 value=', '.join(self.resources),
                                 short=False)

        # Add attachment
        message.add_attachment(attachment)
        return message
示例#2
0
def main(testing=False):
    from slack import SlackMessage
    
    setproctitle('trac_ticket')

    logging.basicConfig(level='DEBUG' if testing else 'INFO')
    
    with open('.slack_token') as f:
        token = f.read().strip()
    slack = SlackMessage(token, testing=testing)
    slack.run()
    logging.error('SlackMessage has stopped')
示例#3
0
    def build_message(self):
        alarm_state = self.message['NewStateValue']
        if alarm_state == 'ALARM':
            color = 'danger'
        elif alarm_state == 'OK':
            color = 'good'
        else:
            color = 'warning'

        message = SlackMessage()
        attachment = SlackAttachment(title=self.subject,
                                     text=self.message['NewStateReason'],
                                     footer='CloudWatch Alarm',
                                     color=color)

        # Alarm
        attachment.add_field(
            title='Alarm',
            value='<%s|%s>' % (build_alarm_link(
                self.alarm_name, region=self.region), self.alarm_name))

        # Build fields for each dimension
        for dimension in self.trigger['Dimensions']:
            title = dimension['name']
            value = dimension['value']

            # Add link to certain dimensions
            if title == 'InstanceId':
                value = '<%s|%s>' % (build_ec2_search(
                    value, region=self.region, method='instanceId'), value)

            attachment.add_field(title=title, value=value)

        # Metric
        attachment.add_field(
            title='Metric',
            value='%s/%s' %
            (self.trigger['Namespace'], self.trigger['MetricName']))

        # Account Information
        if self.account_alias:
            account = '%s (%s)' % (self.account_alias, self.account_id)
        else:
            account = self.account_id

        attachment.add_field(title='Account', value=account)

        # Region
        attachment.add_field(title='Region', value=self.message['Region'])

        # Add the attachment
        message.add_attachment(attachment)
        return message
示例#4
0
    def build_message(self):
        event_class = None

        if 'AutoScalingGroupARN' in self.message:
            from events.sns.autoscaling import AutoScalingEvent
            event_class = AutoScalingEvent

        elif 'AlarmName' in self.message:
            from events.sns.alarm import AlarmEvent
            event_class = AlarmEvent

        if event_class:
            return event_class(self.event).build_message()

        message = SlackMessage()

        if isinstance(self.message, dict):
            # If message is a dict, split attributes into attachment fields
            attachment = SlackAttachment(
                title=self.subject,
                text=None,
            )

            for key, value in self.message.items():
                short = True

                # If value is a list/dict, convert to JSON and use full width field
                if isinstance(value, (list, dict)):
                    value = json.dumps(value)
                    short = False

                # Add the field
                attachment.add_field(title=key, value=value, short=short)

            # Add attachment to the message
            message.add_attachment(attachment)

        else:
            # Fallback to a plain message
            message.text = '*%(subject)s*\n\n%(message)s' % {
                'subject': self.subject,
                'message': self.message
            }

        return message
示例#5
0
    def build_message(self):
        message = SlackMessage()

        # Build the text
        text = ''
        for item in self.detail['eventDescription']:
            text = item['latestDescription'] + '\n' + text

        # Create attachment
        attachment = SlackAttachment(
            title=self.detail['eventTypeCode'],
            title_link=build_phd_link(self.detail['eventArn']),
            text=text,
            footer='Health Event',
            color='danger'
        )

        # Service
        attachment.add_field(
            title='Service',
            value=self.service
        )

        # Region
        attachment.add_field(
            title='Region',
            value=self.region
        )

        # Affected resources
        attachment.add_field(
            title='Resources',
            value=', '.join(self.resources),
            short=False
        )

        # Account Information
        attachment.add_field(
            title='Account',
            value=self.account_id
        )

        # Add attachment and return message
        message.add_attachment(attachment)
        return message
示例#6
0
# coding: utf-8
# 参考:「みんなのRaspberry Pi入門」リックテレコム 石井もルナ・江崎徳秀 著

# spi, time ライブラリをインポート
import spidev
import time
import RPi.GPIO as GPIO
from slack import SlackMessage 

ledflag = False
slackPost = SlackMessage()

GPIO.setmode(GPIO.BCM) 
led_channel = 4
GPIO.setup(led_channel, GPIO.OUT)
GPIO.output(led_channel, GPIO.HIGH)

# SpiDev オブジェクトのインスタンスを生成
spi = spidev.SpiDev()

# ポート0、デバイス0のSPI をオープン
spi.open(0, 0)

# 最大クロックスピードを1MHz に設定
spi.max_speed_hz=1000000

# 1 ワードあたり8ビットに設定
spi.bits_per_word=8

# ダミーデータを設定(1111 1111)
dummy = 0xff
示例#7
0
    def build_message(self):
        if self.event_type not in self.VALID_EVENTS:
            return

        if self.event_type == event_types.EVENT_AUTOSCALING_INSTANCE_LAUNCH:
            color = 'good'
        elif self.event_type == event_types.EVENT_AUTOSCALING_INSTANCE_TERMINATE:
            color = 'warning'
        else:
            color = 'danger'

        message = SlackMessage()
        attachment = SlackAttachment(
            title=self.subject,
            text=self.message['Cause'].replace('  ', '.\n\n'),
            footer=self.message['Service'],
            color=color,
        )

        # Auto Scaling Group
        asg_name = self.message['AutoScalingGroupName']
        attachment.add_field(
            title='Auto Scaling Group Name',
            value='<%s|%s>' %
            (build_asg_link(asg_name, region=self.region), asg_name))

        # Instance ID
        instance_id = self.message['EC2InstanceId']
        attachment.add_field(
            title='EC2 Instance ID',
            value='<%s|%s>' % (build_ec2_search(
                instance_id, method='instanceId',
                region=self.region), instance_id))

        # Subnet
        subnet_id = self.message['Details'].get('Subnet ID')
        subnet_name = subnet_id
        try:
            subnet = self.ec2.Subnet(subnet_id)
            for item in subnet.tags:
                if item['Key'] == 'Name':
                    subnet_name = item['Value']
                    break
        except ClientError:
            subnet = None

        # Availability Zone
        availability_zone = self.message['Details'].get('Availability Zone')

        # Combine subnet and availability zone into a single field
        attachment.add_field(
            title='Subnet',
            value='<%s|%s (%s)>' % (build_ec2_search(
                subnet_id, method='subnetId',
                region=self.region), subnet_name, availability_zone))

        # Get VPC if possible
        if subnet:
            vpc_name = subnet.vpc.id
            for item in subnet.vpc.tags:
                if item['Key'] == 'Name':
                    vpc_name = item['Value']
                    break

            attachment.add_field(
                title='VPC',
                value='<%s|%s>' % (build_ec2_search(
                    subnet.vpc.id, method='vpcId',
                    region=self.region), vpc_name),
            )

        # Account Information
        if self.account_alias:
            account = '%s (%s)' % (self.account_alias, self.account_id)
        else:
            account = self.account_id

        attachment.add_field(title='Account', value=account)

        # Region Information
        attachment.add_field(title='Region', value=self.region)

        # Add the attachment
        message.add_attachment(attachment)
        return message
示例#8
0
    parser.add_argument(
        '--radarr-key', '-rk',
        help='Radarr API key, find it on Radarr > Settings > General'
    )
    parser.add_argument(
        '--tmdb-key', '-tk',
        help='TMDB API Key, register app on tmdb to obtain API Key'
    )
    args = parser.parse_args()
    return args


args = _argparse()

radarr = RadarrApi(args.radarr_url, args.radarr_key)
radarr.unmonitorMovieIfNeeded(os.environ.get("radarr_movie_id"), os.environ.get("radarr_eventtype"))
radarr.loadData(os.environ.get("radarr_download_id"))
link = "https://www.themoviedb.org/movie/"+os.environ.get("radarr_movie_tmdbid", "")

tmdb = TmdbApi(args.tmdb_key)
tmdb.loadMovieData(radarr.tmdbId)
tmdb.downloadMovieProductionImage()

message = SlackMessage(args.webhook_url)
message.package("*"+ os.environ.get("radarr_movie_title", "") +"* ("+ radarr.year +") ["+ os.environ.get("radarr_moviefile_quality", "") +"]")
message.constructor("`" +radarr.indexer+"` _"+os.environ.get("radarr_moviefile_releasegroup", "")+"_ ("+radarr.sizeOnDisk+")")
message.release(radarr.releaseTitle)
message.link(link)
message.save()

message.notify()
示例#9
0
sonarr = SonarrApi(args.sonarr_url, args.sonarr_key)
sonarr.loadData(os.environ.get("sonarr_series_id", ""),
                os.environ.get("sonarr_episodefile_id", ""),
                os.environ.get("sonarr_download_id", ""))
sonarr.unmonitorMovieIfNeeded(os.environ.get("sonarr_eventtype"))

networkName = tmdb.normalizeNetworkName(sonarr.network)
networkLogoEmoji = tmdb.downloadImageIfNeeded(networkLogoUrl, networkName)

season = os.environ.get("sonarr_episodefile_seasonnumber", "")
episode = os.environ.get("sonarr_episodefile_episodenumbers", "")

link = ""
tvMaze = TvMazeApi(os.environ.get("sonarr_series_tvmazeid", ""))
link = tvMaze.getEpisodeUrl(season, episode)

message = SlackMessage(args.webhook_url)
message.package("*" + os.environ.get("sonarr_series_title", "") + " - " +
                season + "x" + episode + " - " +
                os.environ.get("sonarr_episodefile_episodetitles", "") +
                "* [" + os.environ.get("sonarr_episodefile_quality", "") + "]")
message.constructor("`" + sonarr.indexer + "` _" +
                    os.environ.get("sonarr_episodefile_releasegroup", "") +
                    "_ (" + sonarr.sizeOnDisk + ")")
message.link(link)
message.iconUrl = networkLogoUrl
message.iconEmoji = networkLogoEmoji
message.save()

message.notify()