Post to vim → Python → Slack

Post to Slack from VIM via Python

--If you want to use Slack from VIM, you should use the following. -Slack, Vim, memo management and me

――I was influenced by the following articles. -Write a script in Shell and Python to notify Slack when the process is finished

――What I wanted to do --Post to Slack with Pure Python --Better if you can do it from VIM

--Preparation --Get your own Slack Token or Webhook URL --Once you get it, you can use both Webhook and API, so set the one you like. --If Python can be executed from the command prompt, it should work --Write the Path of python.py in vim scirpt

python.py


import sys
import requests
import json
import re


class SlackApiWrapper:
    def __init__(self, proxy_flg=None):
        '''
        proxy_flg:True:Use Proxy
                  False:Non-Proxy
        '''
        # Your infomation
        self.token = '<your token>'
        self.postWebhookUrl = '<your webhookurl>'

        # Post infomation
        self.postSlackUrl = 'https://slack.com/api/chat.postMessage'
        self.channel = '#general'
        self.username = 'vim_python'

        # if proxy
        if proxy_flg is None:
            self.proxy_flg = False
        else:
            self.proxy_flg = proxy_flg
            self.proxy_info = {
                "http": "http://hogehoge:8080",
                "https": "http://hogehoge:8080"
            }

    def post_api(self, text):
        json_data = {
            'token': self.token,
            'channel': self.channel,
            'text': text,
            'username': self.username,
            'unfurl_links': 'false',
            'pretty': 1

        }
        if self.proxy_flg:
            r = requests.post(self.postSlackUrl, params=json_data, proxies=self.proxy_info)
        else:
            r = requests.post(self.postSlackUrl, params=json_data)
        print(r.text)

    def post_webhook(self, text):
        json_data = {
            'text': text,
            'username': self.username,
            'link_names': 1
        }
        if self.proxy_flg:
            r = requests.post(self.postWebhookUrl, data=json.dumps(json_data), proxies=self.proxy_info)
        else:
            r = requests.post(self.postWebhookUrl, data=json.dumps(json_data))
        print(r.text)


def main(args):
    filename = None
    if args:
        filename = str(args[0])

    data = None
    if filename is None:
        data = ["test"]
    else:
        try:
            with open(filename, mode="r", encoding='utf-8') as fh:
                data = fh.readlines()
        except Exception as ex:
            raise ex
    
    #Concatenate vim line breaks
    # data = "".join(data)
    # data = re.sub(r"\n", "", data)

    # None Proxy
    # sl = SlackApiWrapper(False)

    # Proxy
    sl = SlackApiWrapper(True)

    #Post line breaks continuously
    for text in data:
        #Either
        sl.post_webhook(text)
        sl.post_api(text)


if __name__ == "__main__":
    main(sys.argv[1:])

_vimrc.local


"slack"{{{
function! s:post_slack_vim()"
    let s:file = tempname()
    let s:py_script = "<DL Python.py PATH>"
    silent execute ":write " . s:file
    silent execute '!python' . ' ' . s:py_script . ' ' . s:file
    call delete(s:file)
    unlet! s:file
    unlet! s:py_script
endfunction augroup END"
command! -nargs=0 PSlack call s:post_slack_vim()
"}}}

Recommended Posts

Post to vim → Python → Slack
Post from Python to Slack
Post to slack with Python 3
Post to Slack in Python
Image characters and post to slack (python slackbot)
Post to Slack via Subscriber
Post to Twitter using Python
POST messages from python to Slack via incoming webhook
Post Jenkins console output to Slack
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Post to slack in Go language
Post images from Python to Tumblr
Easily post to twitter with Python 3
[Nanonets] How to post Memo [Python]
Updated to Python 2.7.9
Send a message from Python to Slack
Post a message from IBM Cloud Functions to Slack in Python
Log in to Slack using requests in Python
"Backport" to python 2
Send a message from Slack to a Python server
[First post] Question ・ How to sweep out python
Post youtube soaring title to twitter with python3
Allow vim quickfix to jump to Python error messages
Sample to send slack notification with python lambda
[Python] Throw a message to the slack channel
[Python] Mention to multiple people with Slack API
Post an article with an image to WordPress with Python
Flow to complete Slack authentication with Flask (Python)
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Rewrite Python2 code to Python3 (2to3)
How to install python
python decorator to retry
Introduction to Python language
Introduction to OpenCV (python)-(2)
Slack chatbot creation Python
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
For those who want to write Python with vim
Try using Junos' On-box Python #Bonus 1 Something like ChatOps / Commit and post to Slack
Posted as an attachment to Slack on AWS Lambda (Python)
Selenium and python to open google
Connect to BigQuery with Python
[2020.8 latest] How to install Python
[python] Convert date to string
How to install Python [Windows]
Introduction to Python Django (2) Win
To flush stdout in Python
Convert numpy int64 to python int
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
Cheating from PHP to Python
A road to intermediate Python
POST json with Python3 script
Try to understand Python self