Bob the Bot
Steps
01) Open this project with Gitpod
this is a wip π§
When the project is opened with Gitpod, all dependencies are installed (see .gitpod.yml
)
02) Generate the project
cabu generate service-post bob
cd bob
go mod tidy
when cabu is run for the first time, it will pull the Capsule Builder Docker image
Source code generated
bob.go
package main
import (
hf "github.com/bots-garden/capsule/capsulemodule/hostfunctions"
/* string to json */
"github.com/tidwall/gjson"
/* create json string */
"github.com/tidwall/sjson"
)
func main() {
hf.SetHandleHttp(Handle)
}
func Handle(request hf.Request) (response hf.Response, errResp error) {
hf.Log("π Body: " + request.Body)
hf.Log("π URI: " + request.Uri)
hf.Log("π Method: " + request.Method)
name := gjson.Get(request.Body, "name")
headersResp := map[string]string{
"Content-Type": "application/json; charset=utf-8",
}
jsondoc := `{"message": ""}`
jsondoc, _ = sjson.Set(jsondoc, "message", "π hello " + name.Str)
return hf.Response{Body: jsondoc, Headers: headersResp}, nil
}
/*
curl -v -X POST http://localhost:8080 -H 'content-type: application/json' -d '{"name": "Bob"}'
*/
First build
cd bob
cabu build . bob.go bob.wasm
First Run
cd bob
capsule -wasm=./bob.wasm -mode=http -httpPort=8080
Call the service:
curl -v -X POST http://localhost:8080 -H 'content-type: application/json' -d '{"name": "Bob"}'
03) Prepare WebHook
- Go to Settings > Webhooks (Content type: application/json) / Issues + Issue comments
https://8080-botsgarden-bobthebot-3uk65iyrzav.ws-eu70.gitpod.io
/*
https://docs.github.com/en/rest/issues/comments#create-an-issue-comment
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER/comments \
-d '{"body":"Me too"}'
*/