<commit_before>package slack

import (
	"encoding/json"
	"fmt"
	"github.com/brettbuddin/victor/pkg/chat"
	"log"
	"net/http"
	"net/url"
	"os"
)

func init() {
	chat.Register("slack", func(r chat.Robot) chat.Adapter {
		team := os.Getenv("VICTOR_SLACK_TEAM")
		token := os.Getenv("VICTOR_SLACK_TOKEN")

		if team == "" || token == "" {
			log.Println("The following environment variables are required:")
			log.Println("VICTOR_SLACK_TEAM, VICTOR_SLACK_TOKEN")
			os.Exit(1)
		}

		return &slack{
			robot: r,
			team:  team,
			token: token,
		}
	})
}

type slack struct {
	robot       chat.Robot
	team, token string
}

func (s *slack) Run() {
	s.robot.HTTP().HandleFunc("/hubot/slack-webhook", func(w http.ResponseWriter, r *http.Request) {
		s.robot.Receive(&message{
			userID:      r.PostFormValue("user_id"),
			userName:    r.PostFormValue("user_name"),
			channelID:   r.PostFormValue("channel_id"),
			channelName: r.PostFormValue("channel_name"),
			text:        r.PostFormValue("text"),
		})
	}).Methods("POST")
}

func (s *slack) Send(channelID, msg string) {
	body, err := json.Marshal(&outgoingMessage{
		Channel:  channelID,
		Username: s.robot.Name(),
		Text:     msg,
	})

	if err != nil {
		log.Println("error sending to chat:", err)
	}

	endpoint := fmt.Sprintf("https://%s.slack.com/services/hooks/incoming-webhook?token=%s", s.team, s.token)
	http.PostForm(endpoint, url.Values{"payload": {string(body)}})
}

func (s *slack) Stop() {
}

type outgoingMessage struct {
	Channel  string `json:"channel"`
	Username string `json:"username"`
	Text     string `json:"text"`
}

type message struct {
	userID, userName, channelID, channelName, text string
}

func (m *message) UserID() string {
	return m.userID
}
<commit_msg>Adapted to new Incoming Webhooks API<commit_after>package slack

import (
	"encoding/json"
	"log"
	"net/http"
	"net/url"
	"os"

	"github.com/brettbuddin/victor/pkg/chat"
)

func init() {
	chat.Register("slack", func(r chat.Robot) chat.Adapter {
		slackOutgoingWebhookPath := os.Getenv("SLACK_OUTGOING_WEBHOOK")
		slackIncomingWebhookUri := os.Getenv("SLACK_INCOMING_WEBHOOK_URI")

		if slackOutgoingWebhookPath == "" || slackIncomingWebhookUri == "" {
			log.Println("The following environment variable is required:")
			log.Println("SLACK_INCOMING_WEBHOOK_URI, SLACK_OUTGOING_WEBHOOK")
			os.Exit(1)
		}

		return &slack{
			robot:               r,
			outgoingWebhookPath: slackOutgoingWebhookPath,
			incomingWebhookUri:  slackIncomingWebhookUri,
		}
	})
}

type slack struct {
	robot               chat.Robot
	incomingWebhookUri  string
	outgoingWebhookPath string
}

func (s *slack) Run() {
	s.robot.HTTP().HandleFunc(s.outgoingWebhookPath, func(w http.ResponseWriter, r *http.Request) {
		s.robot.Receive(&message{
			userID:      r.PostFormValue("user_id"),
			userName:    r.PostFormValue("user_name"),
			channelID:   r.PostFormValue("channel_id"),
			channelName: r.PostFormValue("channel_name"),
			text:        r.PostFormValue("text"),
		})
	}).Methods("POST")
}

func (s *slack) Send(channelID, msg string) {
	body, err := json.Marshal(&outgoingMessage{
		Channel:  channelID,
		Username: s.robot.Name(),
		Text:     msg,
	})

	if err != nil {
		log.Println("error sending to chat:", err)
	}

	if resp, err := HttpClient.PostForm(s.incomingWebhookUri, url.Values{"payload": {string(body)}}); err != nil {
		log.Printf("Slack API call failed: %s", err)
	} else if resp.StatusCode != 200 {
		log.Printf("Slack API returned HTTP status code %d", resp.StatusCode)
	}
}

func (s *slack) Stop() {
}

type outgoingMessage struct {
	Channel  string `json:"channel"`
	Username string `json:"username"`
	Text     string `json:"text"`
}

type message struct {
	userID, userName, channelID, channelName, text string
}

func (m *message) UserID() string {
	return m.userID
}

{'fraction_non_alphanumeric': 0.14990811236545024, 'fraction_numerical': 0.0013126804935678655, 'mean_word_length': 4.093582887700535, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7522229', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1397, 'n_words': 286}