Python Slack



  1. Python Slackbot
  2. Python Slack Notification

In this week’s episode, Madison shows us how to use Neo4j with Python using the Py2neo package and includes steps and template code for recreating her walkthrough.

Tom shows us how to model git commits with Neo4j and explains some contrasts between the same use case in graph and relational setups. Alex announces a Neo4j community migration from Slack to Discord to expand more interactive features. EarthlingDavey on Github published an early release repository for OS Open Names data in Neo4j that provides latitude and longitude for a location in Great Britain.

In this post I will demonstrate how to send messages to slack using python based on the status of an event. We will keep it basic, that when something is down or up, it should send a slack message with the status, message, color and embed your grafana dashboard links inside the alert (or any links that you would like).

And finally, Neo4j hosts a variety of use cases each week for 30 minutes in weekly live demos, Lju discusses some scenarios that fit a graph database, Elaine highlights the new Neo4j certifications, and Sixing publishes a post exploring the KEGG disease database with Neo4j.

  1. In this article I will introduce Tableau2Slack, a Python 3.7 script that utilizes the Tableau Server Client Python library and Slack Developer Kit for Python to:. open a specific view on a Tableau Server (including a Tableau Online server). download an image of that view to disk. trigger a Slack Bot to upload the image to your Slack.
  2. Slack; Join us on Slack! Socialize with, learn from, and teach 36,504 other members. Email. Emails are only used for user invitations and are not tracked or shared. I agree to the Python Community Code of Conduct.

Featured Community Member: Andreas Kollegger


This week’s featured community member is Andreas Kollegger.

Andreas Kollegger – This Week’s Featured Community Member

Andreas is Developer Relations Community Representative here at Neo4j, recently taking on quite a bit of the community leadership and efforts. He has been a part of the Neo4j community since the early days of the company (before he was an employee!) and has continued to be involved in giving his all to provide the best experience for Neo4j users.

In the early years, Andreas created a lot of community content, including training materials and online presentations. As one of the first 10 employees at Neo4j, he devoted much of his time to community efforts and presenting at meetups and road-shows. He became Director of Product Management for Developer Tools where he led the strategy and development for Neo4j Desktop and Browser. He also was influential in the early days of Neo4j Bloom. Through his efforts in that area, he introduced the concepts of the Relate framework and graph apps in Neo4j Desktop. Recently, he created Gram, a textual format for graph data. You can find him on many of our community platforms like Discord, the Community Forum, and Medium. Thank you, Andreas, for your dedicated commitment to improving Neo4j and encouraging current and new users!

Integrating Python Code with Py2neo


Madison writes a helpful post on using Neo4j with Py2neo, which is a Python package that operates similar to a driver for connecting to Neo4j. We get a bit of background and how it works, then dive right in to a walkthrough! First, our environment gets set up with details for both Windows and Mac to prepare a Python virtual environment, start a Neo4j database, and install required packages.

Next, Madison gives us a template code sample that we can substitute into our environment for easy spin-up, which we can tweak later, if needed. She even gives us some ideas for using parameters and lists in Py2neo with a link for the documentation included. Wrapping up, she leaves us with a couple of other helpful articles to continue our learning and hints at a future follow-up from her. We’d love to see more!

Modeling Git Commits with Neo4j


Tom published an excellent article on his process for modeling git commits for the coderadar project. At the start, Tom tested his approach with JPA entities and a relational database. Queries were cumbersome and join tables occupied a large amount of space. He felt there must be a better way.

Enter graphs! Neo4j was the choice, as it would store the relationships of commits and files more simply and also provided good support for Spring Data Neo4j. He was able to get up and running pretty quickly with the skills he needed and walks us through his graph data model and querying the data with Cypher. While he agrees the relational model was feasible, he advocates that the graph model improved and simplified his use case.

Neo4j Community is migrating from Slack to Discord


Neo4j announced that we are moving our Slack community to Discord! Our community was growing too big, and we wanted to take advantage of some features available in Discord that weren’t possible in Slack, such as group video calls and screen sharing.

In Discord, we have the ability to pop into different channels (as with Slack) and even have continuous video/audio channels so you can virtually co-work, ask questions, or chat live with others. We have created a couple of specific channels for our NODES 2021 event and a general AMA channel where you can join office hours with Neo4j staff for questions or chat. Join our Discord community to join in the fun!

OS Open Names to Neo4j


EarthlingDavey created an early release of opennames-to-neo4j, which is a public API of places, roads, and postal codes in Great Britain. The data can be downloaded as CSV, GML, API, and Geopackage formats for automatically importing into Neo4j.

The Github repository gives a great overview of the architecture, graph data model, system requirements, and installation/usage for the data. We even get some examples and ways to extend functionality. It’s still an early release, but the library is intriguing and useful. EarthlingDavey is using it for a project to convert user location search to latitude and longitude coordinates for the purpose of distance calculations. For any feedback or questions, the author points us to create a Github issue or contact him on the Neo4j Discord community.

Weekly Live Demos, Spotting a Graph-Shaped Problem, Neo4j Certifications


  • Neo4j hosts live demos with Neo4j experts for 30 minutes every week. The demos rotate, so if you miss one you wanted to see, just check the schedule again for the next offering! Right now, there are demos for supply chain, algorithms playground (NEuler), and fraud detection.
  • Lju writes a great blog post on graphs and some scenarios that fit a graph database. We get an introduction to graphs in general, then walk through 4 scenarios where graphs are often the best fit. Each scenario is covered in-depth and even lists some use cases that apply to each. Lju’s post is super helpful if you are new to graphs or just trying to explain it to others!
  • Elaine walks us through the new Neo4j certifications and what we can expect from each of the offerings. There are now 3 exams available to demonstrate and test your knowledge of Neo4j, including the new graph data science certification.
  • Sixing published another article on Neo4j and medical analysis, this time using the KEGG disease database. Here, he analyzes multipurpose drugs, as well as the COVID-19 and similar SARS viruses.

Tweet of the Week


The latest tutorial in the Basic series shows how to use FANTASIA to send speech from @UnrealEngine to @Microsoft Language Understanding Intelligent Service and use the obtained data to query @neo4j!https://t.co/tBTesaLW9V via @YouTube#UE4

— FANTASIA UE4 Plugin (@FANTASIA4UE) April 3, 2021

Bots are a useful way to interact with chat services such asSlack. If you have never built a bot before, thispost provides an easy starter tutorial for combining theSlack API with Python to create your first bot.

We will walk through setting up your development environment, obtaining aSlack API bot token and coding our simple bot in Python.

Tools We Need

Our bot, which we will name 'StarterBot', requires Python and the Slack API.To run our Python code we need:

  • Either Python 2 or 3
  • pip andvirtualenv to handle Pythonapplication dependencies
  • Free Slack account - you need to be signed into atleast one workspace where you have access to building apps.

It is also useful to have the Slack API docs handywhile you're building this tutorial.

All the code for this tutorial is available open source under the MIT licensein the slack-starterbot publicrepository.

Establishing Our Environment

Slack

We now know what tools we need for our project so let's get our developmentenvironment set up. Go to the terminal (or Command Prompt on Windows) andchange into the directory where you want to store this project. Withinthat directory, create a new virtualenv to isolate our applicationdependencies from other Python projects.

Python Slackbot

Activate the virtualenv:

Your prompt should now look like the one in this screenshot.

The official slackclient API helper library built by Slack can send andreceive messages from a Slack channel. Install the slackclient library withthe pip command:

When pip is finished you should see output like this and you'll beback at the prompt.

We also need to create a Slack App to recievean API token for your bot. Use 'Starter Bot' as your App name. If you are signedinto more than one workspace, pick a Development Workspace from the dropdown.

After submitting the form, keep the app configuration page open.

Slack APIs and App Configuration

We want our Starter Bot to appear like any other user in your team - it willparticipate in conversations inside channels, groups, and DMs. In a SlackApp, this is called a bot user, whichwe set up by choosing 'Bot Users' under the 'Features' section. Afterclicking 'Add a Bot User', you should choose a display name, choose adefault username, and save your choices by clicking 'Add Bot User'. You'llend up with a page that looks like the following:

The slackclient library makes it simple to use Slack'sRTM API and Web API.We'll use both to implement Starter Bot, and they each require authentication.Conveniently, the bot user we created earlier can be used to authenticate forboth APIs.

Click on the 'Install App' under the 'Settings' section. The button on this pagewill install the App into our Development Workspace. Once the App is installed,it displays a bot user oauth access token for authentication as the bot user.

A common practice for Python developers is to export secret tokens asenvironment variables. Back in your terminal, export the Slack token with thename SLACK_BOT_TOKEN:

Nice, now we are authorized to use the Slack RTM and Web APIs as a bot user.

Coding Our Starter Bot

We've got everything we need to write the Starter Bot code. Create a new filenamed starterbot.py and include the following code in it.

Python Slack Notification

With our dependencies imported we can use them to obtain the environmentvariable values and then instantiate the Slack client.

The code instantiates the SlackClient client with our SLACK_BOT_TOKENexported as an environment variable. It also declares a variable we can use tostore the Slack user ID of our Starter Bot. A few constants are also declared,and each of them will be explained as they are used in the code that follows.

The Slack client connects to the Slack RTM API. Once it's connected, it calls aWeb API method (auth.test) to findStarter Bot's user ID.

Each bot user has a user ID for each workspace the Slack App is installedwithin. Storing this user ID will help the program understand if someone hasmentioned the bot in a message.

Next, the program enters an infinite loop, where each time the loop runs theclient recieves any events that arrived from Slack's RTM API. Notice thatbefore the loop ends, the program pauses for one second so that it doesn't looptoo fast and waste your CPU time.

For each event that is read, the parse_bot_commands() function determines ifthe event contains a command for Starter Bot. If it does, then command willcontain a value and the handle_command() function determines whatto do with the command.

We've laid the groundwork for processing Slack events and calling Slack methodsin the program. Next, add three new functions above the previous snippet tocomplete handling commands:

The parse_bot_commands() function takes events from Slack and determinesif they are commands directed at Starter Bot. There are manyevent types that our bot will encounter, but tofind commands we only want to considermessage events. Message events also havesubtypes, but the commands we want to find won't have any subtype defined. Thefunction filters out uninteresting events by checking these properties. Now weknow the event represents a message with some text, but we want to find outif Starter Bot is being mentioned in the text. The parse_direct_mention()function will figure out of the message text starts with a mention, and thenwe compare that to the user ID we stored earlier for Starter Bot. If they arethe same, then we know this is a bot command, and return the command text withthe channel ID.

The parse_direct_mentions() function uses a regular expression to determineif a user is being mentioned at the beginning of the message. It returnsthe user ID and the remaining message (and None, None if no mention wasfound).

The last function, handle_command() is where in the future you'll add all theinteresting commands, humor, and personality for Starter Bot. For now, it hasjust one example command: do. If the command starts with a known command, itwill have an appropriate response. If not, a default response is used. Theresponse is sent back to Slack by calling thechat.postMessage Web APImethod with the channel.

Here is how the entire program should look when it's all put together(you can alsoview the file in GitHub):

Now that all of our code is in place we can run our Starter Bot on thecommand line with the python starterbot.py command.

In Slack, create a new channel and invite Starter Bot or invite it to anexisting channel.

Now start giving Starter Bot commands in your channel.

Additional Note: Currently there's an issue with the websocket package and the CA certificate it uses, so if you encounter an error like:

There are a couple of things that can be done:1. Downgrading the websocket-client library to 0.47.02. Or, download the certificate (wget https://www.tbs-certificats.com/issuerdata/DigiCertGlobalRootCA.crt), then set the environment variable export WEBSOCKET_CLIENT_CA_BUNDLE=DigiCertGlobalRootCA.crt

Wrapping Up

Alright, now you've got a simple Starter Bot with a bunch of places in thecode you can add whatever features you want to build.

There is a whole lot more that could be done using the Slack RTM API and Python.Check out these posts to learn what you could do:

  • Attach a persistent relational database orNoSQL back-end such asPostgreSQL, MySQL or SQLiteto save and retrieve user data
  • Add another channel to interact with the botvia SMSorphone calls
  • Integrate other web APIs such asGitHub or Twilio
  • Explore other Slack Platform APIs and the reasons you might use one over another.
  • Build an onboarding bot using the Slack Events API

Questions? Contact me via Twitter@fullstackpythonor @mattmakai. I'm also on GitHub withthe username mattmakai.

See something wrong in this post? Forkthis page's source on GitHuband submit a pull request.