Windows 10でPingを受け付けるようにする
Windows 10では標準でpingに反応しないので、ICMPを許可する。
管理者権限のPowerShell上で
Get-NetFirewallRule -Name FPS-ICMP4-ERQ-In | Set-NetFirewallRule -enabled true;
CoffeeScript2でSlackの認証テスト
# https://ja.ojit.com/so/slack/3498196 SLACK_USER_TOKEN = 'xoxp-881518928711-868191264403-00x0dfd0v1766-10458077c--36681ce0599334ca5' SLACK_BOT_TOKEN = 'xoxb-881518928711-1609760895585-rfrigjirjirPECn47fgdfvf--ljIV' BASE_URL = 'https://slack.com/api/' headers = {'Authorization': "Bearer #{SLACK_BOT_TOKEN}"} request = require('request') postOptions = uri: BASE_URL + 'auth.test' method: 'POST' json: true headers: headers # DEBUG console.log BASE_URL + 'auth.test' request postOptions, (error, response, body) -> if error console.log 'OOPPPPS....we hit an error in auth.test: ' + error else console.log 'auth.test response: ' + JSON.stringify(response) return #res.send("Sent Test...check logs");
C:\Somewhere>.\node_modules\.bin\js2coffee auth-test.js > auth-test.coffee
C:\Somewhere>.\node_modules\.bin\coffee.cmd auth-test.coffee
Node.jsでSlackのauth.testを叩く
// https://ja.ojit.com/so/slack/3498196 const SLACK_USER_TOKEN = 'xoxp-881518928711-868191264403-fffffffff66-10458077c9cd0df36681ce0599334ca5'; const SLACK_BOT_TOKEN = 'xoxb-881518928711-1609760895585ffffffffffffff3i4DPECn47tSFsVljIV'; const BASE_URL = 'https://slack.com/api/'; var headers = { 'Authorization': `Bearer ${SLACK_USER_TOKEN}` }; var request = require('request'); var postOptions = { uri: BASE_URL + 'auth.test', method: 'POST', json: true, headers: headers, }; // DEBUG console.log(uri); request(postOptions, (error, response, body) => { if (error) { console.log("OOPPPPS....we hit an error in auth.test: " + error); } else { console.log("auth.test response: " + JSON.stringify(response)); } }); //res.send("Sent Test...check logs");
Node.jsでSlackにメッセージを投稿する(SlackAPI)
// Post message to slack // using request_promise // // 2020-12-24 (Thu.) // Ryo Chiba const request_promise = require("request-promise"); const SLACK_USER_TOKEN = 'xoxp-881518928711-868191264403-yyyyyyyyyyyyy6-10458077c9cd0df36681ce05 99334ca5'; const CHANNEL_ID = 'GSX2FOKA03'; const USER_NAME = 'poco'; const text = 'Node.js posting message to Slack...'; (async () => { const res = await request_promise({ uri: 'https://slack.com/api/chat.postMessage', method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', 'charset': 'utf-8' }, form: { token: SLACK_USER_TOKEN, channel: CHANNEL_ID, username: USER_NAME, text: text }, json: true }); console.log(res); })();
PythonでSlackにメッセージを投稿する(SlackAPI)
#!/usr/bin/python3 # -*- coding:utf-8 -*- # Post message to Slack # using requests # 2020-12-24 (Thu.) import json import requests def slack_postmsg(url, token, channel, text): headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json; charset=utf-8'} payload = {'channel': channel, 'text': text, 'as_user': True,} req = requests.post(url, data=json.dumps(payload), headers=headers) print(req.text) SLACKAPI_URL = 'https://slack.com/api/chat.postMessage' SLACK_USER_TOKEN = "xoxp-881518928711-rrrrrrrrrrrrrrrrrrrr-xxxxxxxxxxx-10458077c9cd0df36681ce0599334ca5" channel_id = 'GSX09FY83' text = "message post from Python" slack_postmsg(SLACKAPI_URL, SLACK_USER_TOKEN, channel_id, text)
実行例:
$ python3 post-write-msg.py
{"ok":true,"channel":"GSX2FKASJ","ts":"1608777828.001500","message":{"bot_id":"B01HK4MEUD8","type":"message","text":"message post from Python","user":"URJ5M7SBV","ts":"1608777828.001500","team":"TRXF8TALX","bot_profile":{"id":"B01HK4MEUD8","deleted":false,"name":"msg-cleaner","updated":1608770884,"app_id":"A01HXN8CAJV","icons":{"image_36":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_36.png","image_48":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/bot_48.png","image_72":"https:\/\/a.slack-edge.com\/80588\/img\/plugins\/app\/service_72.png"},"team_id":"TRXF8TALX"}}}
PythonでSlackのメッセージを削除する(SlackAPI)
#!/usr/bin/python3 # -*- coding:utf-8 -*- # Delete Slack message (SlackAPI) # 2020-12-24 (Thu.) import json import requests # Define function def slack_delmsg(url, token, channel, ts): headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json; charset=utf-8'} payload = {'channel': channel, 'ts': ts} req = requests.post(url, data=json.dumps(payload), headers=headers) print(req.text) SLACKAPI_URL = 'https://slack.com/api/chat.delete' SLACK_USER_TOKEN = "xoxp-1538991196854-xxxxxxxxxxxxx-yyyyyyyyyyyyy-9093aef89f0d6e9f21f23abc0xyzxyz" channel_id = 'C01FZZ90F8' ts_p = '1607327126.001000' slack_delmsg(SLACKAPI_URL, SLACK_USER_TOKEN, channel_id, ts_p)