――Ich wurde von den folgenden Artikeln beeinflusst.
»Was ich tun wollte
--Vorbereitung
python.py
import sys
import requests
import json
import re
class SlackApiWrapper:
def __init__(self, proxy_flg=None):
'''
proxy_flg:True:Verwenden Sie 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
#Verketten Sie vim Zeilenumbrüche
# data = "".join(data)
# data = re.sub(r"\n", "", data)
# None Proxy
# sl = SlackApiWrapper(False)
# Proxy
sl = SlackApiWrapper(True)
#Post Line bricht kontinuierlich
for text in data:
#Entweder
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