diff --git a/README.md b/README.md index c532953..7520978 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ A Telegram Bot Token is required for using the Telegram bot from which the commi - Add [@MissRose_bot](https://telegram.dog/MissRose_bot) - Type the command `/id` and send it to the group. +### - `topic_id` (optional) +Use this only if you have topics enabled. + ## How to use Add the following lines of code in your YML file. diff --git a/action.yml b/action.yml index 39efcec..9b679d8 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,9 @@ inputs: chat_id: description: 'The ID of the chat where you want the bot to send the message' required: true + topic_id: + description: The ID of the topic where you want to receive the notifications. + required: false git_event: description: The GitHub context json used to fetch repository info. default: ${{ toJSON(github) }} diff --git a/main.go b/main.go index 481800d..a7ccf79 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( func main() { tg_token := os.Getenv("INPUT_BOT_TOKEN") chatID := os.Getenv("INPUT_CHAT_ID") + topicID := os.Getenv("INPUT_TOPIC_ID") gitEventRaw := os.Getenv("INPUT_GIT_EVENT") print(gitEventRaw) var gitEvent *types.Metadata @@ -21,7 +22,7 @@ func main() { if err != nil { panic(err) } - error := utils.SendMessage(tg_token, chatID, text, markupText, markupUrl) + error := utils.SendMessage(tg_token, chatID, text, markupText, markupUrl, topicID) if error.Description != "" { panic(error.String()) } diff --git a/main_test.go b/main_test.go index e6eeb51..70772f5 100644 --- a/main_test.go +++ b/main_test.go @@ -11,7 +11,7 @@ import ( dotenv "github.com/joho/godotenv" ) -func loadEnvs(t *testing.T) (string, string) { +func loadEnvs(t *testing.T) (string, string, string) { err := dotenv.Load() if err != nil { t.Fatal("Error loading .env file") @@ -24,7 +24,8 @@ func loadEnvs(t *testing.T) (string, string) { if chatID == "" { t.Fatal("Chat ID not specified in .env file") } - return tg_token, chatID + topicID := os.Getenv("TOPIC_ID") + return tg_token, chatID, topicID } func parse(t *testing.T, rawData []byte) (string, string, string) { @@ -42,13 +43,13 @@ func parse(t *testing.T, rawData []byte) (string, string, string) { } func TestCommitMessage(t *testing.T) { - token, chatID := loadEnvs(t) + token, chatID, topicID := loadEnvs(t) data, err := ioutil.ReadFile("events/commit.json") if err != nil { t.Fatal(err) } text, markupText, markupUrl := parse(t, data) - error := utils.SendMessage(token, chatID, text, markupText, markupUrl) + error := utils.SendMessage(token, chatID, text, markupText, markupUrl, topicID) if error.Description != "" { t.Fatal(error.String()) } diff --git a/utils/telegram.go b/utils/telegram.go index 6391040..82802a8 100644 --- a/utils/telegram.go +++ b/utils/telegram.go @@ -10,13 +10,14 @@ import ( "net/url" ) -func SendMessage(token string, chatID string, text string, markupText string, markupUrl string) (error types.Error) { +func SendMessage(token string, chatID string, text string, markupText string, markupUrl string, topicID string) (error types.Error) { apiBaseUri, _ := url.Parse("https://api.telegram.org") req_url, _ := url.Parse(fmt.Sprint(apiBaseUri, "/bot", token, "/sendMessage")) data := map[string]string{ "chat_id": chatID, "text": text, "disable_web_page_preview": "true", + "message_thread_id": topicID, "parse_mode": "html", } kyb, err := json.Marshal(map[string][][]map[string]string{