|
| 1 | +package program |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + runTypes := map[string][2]string{ |
| 16 | + "Go": {"6","go"}, |
| 17 | + "C": {"7","c"}, |
| 18 | + "C++": {"7","cpp"}, |
| 19 | + "Java": {"8","java"}, |
| 20 | + "C#": {"10","cs"}, |
| 21 | + "Python": {"15","py3"}, |
| 22 | + "Lua": {"17","lua"}, |
| 23 | + } |
| 24 | + |
| 25 | + zero.OnCommand("runList").Handle(func(ctx *zero.Ctx) { |
| 26 | + ctx.Send("Run 语种<<<\n代码块\n>>>\n支持语种: Go || Python || Java || C/C++ || C# || Lua") |
| 27 | + }) |
| 28 | + |
| 29 | + zero.OnRegex("(?is:Run (.+?)<<<(.+?)>>>)").Handle(func(ctx *zero.Ctx) { |
| 30 | + getType := ctx.State["regex_matched"].([]string)[1] |
| 31 | + if runType,exist:=runTypes[getType];exist{ |
| 32 | + println("正在尝试执行",getType,"代码块") |
| 33 | + getCode := ctx.State["regex_matched"].([]string)[2] |
| 34 | + getCode = strings.Replace(getCode,"[","[",-1) |
| 35 | + getCode = strings.Replace(getCode,"]","]",-1) |
| 36 | + |
| 37 | + res := runCode(getCode,runType) |
| 38 | + if res["errors"] == "\n\n"{ |
| 39 | + ctx.Send(fmt.Sprintf( |
| 40 | + "[CQ:at,qq=%d]本次%s语言代码执行结果如下:\n%s", |
| 41 | + ctx.Event.UserID, |
| 42 | + getType, |
| 43 | + res["output"][:len(res["output"])-1], |
| 44 | + )) |
| 45 | + }else { |
| 46 | + ctx.Send(fmt.Sprintf( |
| 47 | + "[CQ:at,qq=%d]本次%s语言代码执行失败:%s", |
| 48 | + ctx.Event.UserID, |
| 49 | + getType, |
| 50 | + res["errors"], |
| 51 | + )) |
| 52 | + } |
| 53 | + }else { |
| 54 | + ctx.Send(fmt.Sprintf( |
| 55 | + "[CQ:at,qq=%d][%s]语言不是受支持的编程语种呢~", |
| 56 | + ctx.Event.UserID, |
| 57 | + getType, |
| 58 | + )) |
| 59 | + } |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func runCode(code string,runType [2]string) map[string]string { |
| 64 | + //对菜鸟api发送数据并返回结果 |
| 65 | + result := map[string]string{} |
| 66 | + api := "https://tool.runoob.com/compile2.php" |
| 67 | + headers := map[string]string{ |
| 68 | + "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", |
| 69 | + "Referer": "https://c.runoob.com/", |
| 70 | + "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0", |
| 71 | + } |
| 72 | + data := map[string]string{ |
| 73 | + "code": code, |
| 74 | + "token": "4381fe197827ec87cbac9552f14ec62a", |
| 75 | + "stdin": "", |
| 76 | + "language": runType[0], |
| 77 | + "fileext": runType[1], |
| 78 | + } |
| 79 | + json.Unmarshal(netPost(api,data,headers),&result) |
| 80 | + return result |
| 81 | +} |
| 82 | + |
| 83 | +func netPost(api string,data map[string]string,headers map[string]string) []byte { |
| 84 | + //发送POST请求获取返回数据 |
| 85 | + client := &http.Client{ |
| 86 | + Timeout: time.Duration(6 * time.Second), |
| 87 | + } |
| 88 | + |
| 89 | + param := url.Values{} |
| 90 | + for key,value := range data{ |
| 91 | + param.Set(key,value) |
| 92 | + } |
| 93 | + |
| 94 | + request,_ := http.NewRequest("POST",api,strings.NewReader(param.Encode())) |
| 95 | + for key,value := range headers{ |
| 96 | + request.Header.Add(key,value) |
| 97 | + } |
| 98 | + res,_ := client.Do(request) |
| 99 | + defer res.Body.Close() |
| 100 | + result,_ := ioutil.ReadAll(res.Body) |
| 101 | + return result |
| 102 | +} |
0 commit comments