def slack(results, token, user, icon, recipient, addendum=None): s = Slack(token=token, name=user, icon=icon) winners = ['*_' + o.name + '_*' if o.premium else o.name for o in results] message = 'Voting complete! Here are the results:\n\n' + '\n'.join(winners) if addendum: message += '\n\n' + addendum s.send(recipient, message)
def main(): parser = ArgumentParser(description="Sends a message via Terminal to a " "user or channel in Slack.") parser.add_argument("destination", help="Where to send the message. Can " "be either #channel or @user (or use neither # nor @ " "and we'll try to figure out what you mean).") parser.add_argument("message", help="The text to send.", nargs="+") args = parser.parse_args() s = Slack(config.TOKEN, name=config.USER, icon=config.ICON_URL, verbose=True) s.send(args.destination, " ".join(args.message))
def main(): """ Marks all of a user's slack channels as read. """ s = Slack(config.TOKEN, name=config.USER, verbose=True) s.channels() s.groups() for c in s.channel_list: if c["is_member"]: s.mark("#" + c["name"]) for g in s.group_list: s.mark(g["name"])
def main(): parser = ArgumentParser(description="Sends a Slack message containing " "information about today's weather forecast.") parser.add_argument("recipient", help="The Slack user(s) or channel(s) to " "to send the message to.", nargs="+") parser.add_argument("-c", "--city", help="The city whose weather you would" "like to check. Defaults to {}.".format( config.DEFAULT_CITY), default=config.DEFAULT_CITY) parser.add_argument("-n", "--notify", help="Tags the user (or @channel) in" " the message.", action="store_true") parser.add_argument("-t", "--terse", help="Delivers the weather with fewer" " words.", action="store_true") args = parser.parse_args() weather = check_weather(args.city) message, icon = format_message(weather, args.terse) s = Slack(config.SLACK_TOKEN, name=config.SLACK_USER, verbose=True) for recipient in args.recipient: s.send(recipient, message, icon=icon, notify=args.notify)
def at_startup(self, address, initial): if self.token is None: self.token = getpass('slack api token: ') # authenticate and test self.slack = Slack(self.token, self.name, self.icon) if self.slack.error: raise Exception("Slack Error: {}".format(self.slack.error)) # build message self.message += " A change has occurred at: {}".format(address)
class SlackTrigger(Trigger): """ Send a Slack message when polly registers a change. """ def __init__(self, recipients, token, message=None, name=None, icon=None): self.recipients = recipients self.name = name if name is not None else "Polly" self.icon = icon if icon is not None else ":bird:" self.token = token self.message = message if message is not None else ( "A site you're watching has been updated!" ) def at_startup(self, address, initial): if self.token is None: self.token = getpass('slack api token: ') # authenticate and test self.slack = Slack(self.token, self.name, self.icon) if self.slack.error: raise Exception("Slack Error: {}".format(self.slack.error)) # build message self.message += " A change has occurred at: {}".format(address) def on_change(self, old, new): for recipient in self.recipients: self.slack.send(recipient, self.message, notify=True, unfurl_links=True) if self.slack.error: print("Slack Error: {}".format(self.slack.error)) @classmethod def get_name(cls): return 'slack' @classmethod def from_file(cls, options): return cls(**options)
def slack_message(token, text, notify=False): s = Slack(token, name="LunchBot", icon="http://savage.startleddisbelief.com/LunchBot.png") return s.send("#food", text, notify=notify)