--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