[PYTHON] How to POST to a specified channel without using Slack's Incoming WebHooks

How to POST to a specified channel without using Slack's Incoming WebHooks

It's for people who are using Slack for free and have reached the integration limit and have become sick. You want to try various integrations even in the free frame! ?? It's a waste to use a valuable integration frame for Incoming WebHooks! It is a poor spirit w インテグレーションいっぱい.PNG

table of contents

  1. Preparation
  2. Try POST with Python

Preparation

Go to the following page to get the token for each individual.


https://api.slack.com/web It will be the item of ʻAuthentication` at the bottom of the above page. 認証前.PNG

Click Create token in the image to generate a token for each user. Make a note of it as you will use it later.

認証トークン発行済.PNG

Create a Channel with the sample.


Create a channel from Slack of the team that issued the token. This time, suppose you created a Channel called sample. You can also POST to an existing channel, so you don't have to create a new one.

You're ready to go!

Try POST with Python

The following page has a list of API methods.   https://api.slack.com/methods This time, we will use the chat.postMessage method in this list.

sample.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

#proxy settings, if any
PROXIES = {
  "http": "http://hogehoge:port/",
  "https": "https://fugafuga:port/",
}

class SlackWrapper:
    #slack
    __token = 'your_slack_token' #Write the token you got earlier here
    __channel = '#sample' #Channel name you want to POST
    __postSlackUrl = 'https://slack.com/api/chat.postMessage' #This is fixed
    __icon_url = 'icon URL' #Specify the URL of the icon to post to Slack.
    __username = 'sample' #Username to post to Slack
    
    def __init__(self):
        pass

    def post(self, posttext):
        params = {'token': self.__token, 
                  'channel': self.__channel , 
                  'text':posttext,
                  'icon_url': self.__icon_url,
                  'username':self.__username,
                  'unfurl_links': 'false'
                  }
        #POST to Slack
        r = requests.post(self.__postSlackUrl, params=params, proxies=PROXIES)  
        #When there is no proxy
        # r = requests.post(self.__postSlackUrl, params=params)  

if __name__ == '__main__':
    slack = SlackWrapper()
    slack.post('Hello! Slack!')
    

Now you can POST to the specified channel without using Incoming WebHooks! You did it!

Recommended Posts

How to POST to a specified channel without using Slack's Incoming WebHooks
How to draw a graph using Matplotlib
How to install a package using a repository
How to code a drone using image recognition
How to notify a Discord channel in Python
How to upload to a shared drive using pydrive
How to uninstall a module installed using setup.py
How to set up a Python environment using pyenv
How to make a Python package using VS Code
How to post a ticket from the Shogun API
How to execute a command using subprocess in Python
How to use GitHub on a multi-person server without a password
How to transpose a 2D array using only python [Note]
How to generate a query using the IN operator in Django
How to call a function
Post to Twitter using Python
How to hack a terminal
How to display a specified column of files in Linux (awk)
How to paste a CSV file into an Excel file using Pandas
ABC170 E --How to solve without using multiset of Smart Infants
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to format a table using Pandas apply, pivot and swaplevel
I tried to make a 2channel post notification application with Python
How to save only a part of a long video using OpenCV
How to update a Tableau packaged workbook data source using Python
How to create a CSV dummy file containing Japanese using Faker
How to delete multiple specified positions (indexes) in a Python list
How to install python using anaconda
How to make a Japanese-English translation
How to write a Python class
How to put a symbolic link
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to make a crawler --Basic
[Nanonets] How to post Memo [Python]
How to create a config file
How to generate a new loggroup in CloudWatch using python within Lambda
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
How to call a POST request that supports Japanese (Shift-JIS) with requests
How to divide and process a data frame using the groupby function
How to make a model for object detection using YOLO in 3 hours
How to find out the number of CPUs without using the sar command
How to fix the initial population with a genetic algorithm using DEAP