Using the Slack API with Python – A Simple Example
Here is a simple Python program that can be used to:
- Test the API
- Get a list of Slack Users
- Get a list of Slack Channels
- Get information about a Slack Channel
- Post a message to Slack Channel
Students in my Intro to Programming and Problems Solving class at Clark College learn how to build this and other things using Python.
Have fun with it!
# CTEC 121 / Intro to Programming and Problem Solving
# Lab - Using the Slack API
# by Bruce Elgort, 2016
# pip install slackclient to install SlackClient library
from slackclient import SlackClient
import json
def test_slack(sc):
# use for debugging
print("Testing API")
print(80 * "=")
print (sc.api_call("api.test"))
def get_channel_info(sc,channel):
# get info about the channel
print("Getting Channel Info")
print(80 * "=")
print (sc.api_call("channels.info", channel=channel))
def get_list_of_channels(sc):
print("Getting List of Channels")
print(80 * "=")
# get list of channels
channels = sc.api_call("channels.list")
channels = json.dumps(channels)
channels = json.loads(str(channels))
return channels
def display_channels(channels):
print("Display Channels")
print(80 * "=")
for i in channels['channels']:
print("Channel:",i['name'])
def post_message(sc,text,channel,icon_url,username):
print("Posting Message to Slack")
print(80 * "=")
# post a message into the #general channel
print (sc.api_call("chat.postMessage",channel=channel,text=text,username=username,icon_url=icon_url,unfurl_links="true"))
def get_users(sc):
print("Get Users")
print(80 * "=")
#call the users.list api call and get list of users
users = (sc.api_call("users.list"))
users = json.dumps(users)
users = json.loads(str(users))
return users
def display_users(sc,users):
print("User List")
print(80 * "=")
# display active users
for i in users['members']:
# don't display slackbot
if i['profile']['real_name'] != "slackbot":
# don't display deleted users
if not i['deleted']:
# display real name
print (i['profile']['real_name'])
def main():
# define variables
token = "your token"
channel = "a channel id"
username = "Username to use display for message function"
icon_url = "icon url for message function"
# connect to Slack
sc = SlackClient(token)
# test slack
test_slack(sc)
# get channel info
get_channel_info(sc,channel)
# get list of channels
channels = get_list_of_channels(sc)
# display channels
display_channels(channels)
# post message
post_message(sc,"Visit http://slack.com",channel,icon_url,username)
# get users
users = get_users(sc)
# display users
display_users(sc,users)
main()
MongoDB and Python: A Simple Example
This seems to be a very popular blog post on this blog. Here it is again for those who want to play around with Python and MongoDB.
The code below demonstrates how to use Python to connect to a MongoDB database. I chose to use a cloud based instance of MongoDB provided free of charge by MongoLab.com. The script demonstrates how to:
- Use the PyMongo library to connect to a Mongo database
- Insert documents into a collection
- Display all of the documents from the collection
I also used a local instance of MongoDB for testing. You will will need to use a Python package manager such as EasyInstall to install the PyMongo library.
Here is the Python code:
If you have any questions please let me know.
Co-Working in Vancouver, Washington
Yes, Penguins Fly
Finding Your Audience
In this week’s episode of Getting Work To Work, Chris Martin talks about finding your audience, how difficult and discouraging it can be to find the right people, the importance of having people that push you beyond your limits. and five ways to find your audience today.
Episode 2 is Out! Getting Work to Work
This Parody Of Every TED Talk Ever Is Absolutely Perfect
Your Email Says Absolutely Nothing
Here’s the sound a blind person hears when their screen reader reads your email aloud that only contains a graphical image announcing an event:
“Image, Event announcement”
Pro Tip: Don’t send email messages that have all the content in your super cool Photoshop/MSPaint graphic.
The Creative World’s Bullshit Industrial Complex
They are what philosopher Harry Frankfurt would call “bullshitters.” Those that are giving advice for the sake of giving advice, without any regard as to how it is actually implemented, if it can even be implemented at all. “It’s not important to [the bullshitter] what the world really is like,” he says in a short video documentary about the phenomenon (below). “What is important is how he’d like to represent himself.”
…
This Bullshit Industrial Complex has always existed. But thanks to the precarious economics and job prospects of the creative person, it is often in a creative’s financial interest to climb the bullshit pyramid. In the short term, it’s creating a class of (often young) creatives deluded into thinking they are doing something meaningful by sharing “advice.” Long term, it’s robbing us of a creative talent.
