Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • heyalter/bots
1 result
Show changes
Commits on Source (3)
zuliprc
isobot
!isobot/
muellbot
!muellbot/
module gitli.stratum0.org/heyalter/isobot
module gitli.stratum0.org/heyalter/bots
go 1.16
go 1.19
require (
github.com/go-ini/ini v1.67.0
github.com/ifo/gozulipbot v0.0.1
github.com/xanzy/go-gitlab v0.50.0
)
require (
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
google.golang.org/appengine v1.3.0 // indirect
)
......@@ -38,6 +38,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
......
File moved
File moved
......@@ -9,37 +9,17 @@ import (
"regexp"
"time"
"github.com/go-ini/ini"
"github.com/ifo/gozulipbot"
"github.com/xanzy/go-gitlab"
)
type API struct {
Email string `ini:"email"`
Key string `ini:"key"`
Site string `ini:"site"`
}
"gitli.stratum0.org/heyalter/bots"
)
type Config struct {
API `ini:"api"`
bots.ZuliprcAPISection `ini:"api"`
GitLabWebhookToken string
}
func loadConfig(filename string) (*Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
cfg, err := ini.Load(file)
if err != nil {
return nil, err
}
var ret Config
err = cfg.StrictMapTo(&ret)
return &ret, err
}
var isoURLRegexp *regexp.Regexp = regexp.MustCompile(`data-external-link="false"\s+data-link="/heyalter/heyalter-ubuntu-iso/-/jobs/\d+/artifacts/file/([^"]*\.iso)"`)
......@@ -64,10 +44,12 @@ func HandleWebhook(w http.ResponseWriter, req *http.Request, webhookToken string
if event.ObjectAttributes.Status != "success" {
// Too early, not important
w.WriteHeader(http.StatusAccepted)
return
}
if event.ObjectAttributes.Ref != event.Project.DefaultBranch {
// Not the default branch => not a release => ignoring
w.WriteHeader(http.StatusNoContent)
return
}
......@@ -117,33 +99,22 @@ func HandleWebhook(w http.ResponseWriter, req *http.Request, webhookToken string
log.Println("Couldn't send chat message for job", build.ID, ":", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
} }
}
func main() {
filename := "zuliprc"
listen := "localhost:8080"
if len(os.Args) > 1 {
filename = os.Args[1]
if len(os.Args) > 2 {
listen = os.Args[2]
}
}
cfg, err := loadConfig(filename)
var cfg Config
zlbot, err := bots.InitZulip(&cfg)
if err != nil {
fmt.Println("Couldn't load config", filename, "because of the error", err)
fmt.Println("Couldn't load config because of the error", err)
return
}
var zlbot = gozulipbot.Bot{
Email: cfg.API.Email,
APIKey: cfg.API.Key,
APIURL: cfg.API.Site,
Backoff: 1 * time.Second,
}
zlbot.Init()
listen := "localhost:8080"
if len(os.Args) > 2 {
listen = os.Args[2]
}
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { HandleWebhook(w, req, cfg.GitLabWebhookToken, &zlbot) })
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { HandleWebhook(w, req, cfg.GitLabWebhookToken, zlbot) })
log.Fatal(http.ListenAndServe(listen, nil))
}
# executable
/muellbot
# secrets
zuliprc
https://commons.wikimedia.org/wiki/File:Trash_Can.svg
package main
import (
"encoding/csv"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/ifo/gozulipbot"
"golang.org/x/text/encoding/charmap"
"gitli.stratum0.org/heyalter/bots"
)
type Config struct {
bots.ZuliprcAPISection `ini:"api"`
}
func main() {
var cfg Config
zlbot, err := bots.InitZulip(&cfg)
if err != nil {
fmt.Println("Couldn't load config because of the error", err)
return
}
now := time.Now()
link := "https://alba-bs.de/service/abfuhrtermine/ajax-kalender.html?tx_mfabfallkalender_mfabfallkalender%5Baction%5D=makecsv&tx_mfabfallkalender_mfabfallkalender%5Bcontroller%5D=Abfallkalender&tx_mfabfallkalender_mfabfallkalender%5Bmf-trash-hausnr%5D=1&tx_mfabfallkalender_mfabfallkalender%5Bmf-trash-hausnrzusatz%5D=&tx_mfabfallkalender_mfabfallkalender%5Bmf-trash-strasse%5D=Am%20Wendentor&tx_mfabfallkalender_mfabfallkalender%5Bmf-trash-thisyear%5D=" + strconv.Itoa(now.Year())
resp, err := http.Get(link)
if err != nil {
log.Println("req error", err)
return
}
defer resp.Body.Close()
rdr := charmap.Windows1252.NewDecoder().Reader(resp.Body)
crd := csv.NewReader(rdr)
crd.Comma = ';'
_, err = crd.Read()
if err != nil {
panic(err)
}
// TODO expect DATUM;ABFALLART;BEHÄLTERGRÖSSE
for {
line, err := crd.Read()
if err != nil {
break
}
date, err := time.Parse("02.01.2006", line[0])
if err != nil {
panic(err)
}
diff := date.Sub(now)
if diff > 0 && diff < 2*24*time.Hour {
if line[1] == "LVP" {
line[1] = "Leichtverpackung (Gelbe Tonne)"
}
// da wir vom anderen Torhaus den Müllplan nutzen müssen für korrekte Abholzeiten,
// ignorieren wir hier die Tonnen, die wir nicht nutzen.
if line[1] == "Biotonne" || line[1] == "Altpapier" {
continue
}
text := fmt.Sprintf("Am %02d wird %s %s abgeholt! Bitte vorher rausstellen.", date.Day(), line[2], line[1])
if _, err := zlbot.Message(gozulipbot.Message{
Stream: "HeyAlter_HQ Braunschweig",
Topic: "Müllabholung",
Content: text,
}); err != nil {
log.Println("Couldn't send chat message:", err)
return
}
}
}
}
package bots
import (
"os"
"net/url"
"time"
"github.com/go-ini/ini"
"github.com/ifo/gozulipbot"
)
func loadConfig(filename string, config ZuliprcProvider) error {
file, err := os.Open(filename)
if err != nil {
return err
}
cfg, err := ini.Load(file)
if err != nil {
return err
}
return cfg.StrictMapTo(config)
}
func InitZulip(config ZuliprcProvider) (*gozulipbot.Bot, error) {
filename := "zuliprc"
if len(os.Args) > 1 {
filename = os.Args[1]
}
if err := loadConfig(filename, config); err != nil {
return nil, err
}
apiurl, err := url.JoinPath(config.GetZulipSiteURL(), "/api/v1/")
if err != nil {
return nil, err
}
var bot = &gozulipbot.Bot{
Email: config.GetBotEmail(),
APIKey: config.GetAuthKey(),
APIURL: apiurl,
Backoff: 1 * time.Second,
}
bot.Init()
return bot, nil
}
package bots
type ZuliprcProvider interface {
GetBotEmail() string
GetAuthKey() string
GetZulipSiteURL() string
}
type ZuliprcAPISection struct {
Email string `ini:"email"`
Key string `ini:"key"`
Site string `ini:"site"`
}
func (zas ZuliprcAPISection) GetBotEmail() string {
return zas.Email
}
func (zas ZuliprcAPISection) GetAuthKey() string {
return zas.Key
}
func (zas ZuliprcAPISection) GetZulipSiteURL() string {
return zas.Site
}
var _ ZuliprcProvider = ZuliprcAPISection{}