Skip to content

Commit 27a5fbf

Browse files
FEAT: add support for topics
1 parent ce582cc commit 27a5fbf

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ A Telegram Bot Token is required for using the Telegram bot from which the commi
1717
- Add [@MissRose_bot](https://telegram.dog/MissRose_bot)
1818
- Type the command `/id` and send it to the group.
1919

20+
### - `topic_id` (optional)
21+
Use this only if you have topics enabled.
22+
2023
## How to use
2124

2225
Add the following lines of code in your YML file.

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ inputs:
1010
chat_id:
1111
description: 'The ID of the chat where you want the bot to send the message'
1212
required: true
13+
topic_id:
14+
description: The ID of the topic where you want to receive the notifications.
15+
required: false
1316
git_event:
1417
description: The GitHub context json used to fetch repository info.
1518
default: ${{ toJSON(github) }}

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
func main() {
1111
tg_token := os.Getenv("INPUT_BOT_TOKEN")
1212
chatID := os.Getenv("INPUT_CHAT_ID")
13+
topicID := os.Getenv("INPUT_TOPIC_ID")
1314
gitEventRaw := os.Getenv("INPUT_GIT_EVENT")
1415
print(gitEventRaw)
1516
var gitEvent *types.Metadata
@@ -21,7 +22,7 @@ func main() {
2122
if err != nil {
2223
panic(err)
2324
}
24-
error := utils.SendMessage(tg_token, chatID, text, markupText, markupUrl)
25+
error := utils.SendMessage(tg_token, chatID, text, markupText, markupUrl, topicID)
2526
if error.Description != "" {
2627
panic(error.String())
2728
}

main_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
dotenv "github.com/joho/godotenv"
1212
)
1313

14-
func loadEnvs(t *testing.T) (string, string) {
14+
func loadEnvs(t *testing.T) (string, string, string) {
1515
err := dotenv.Load()
1616
if err != nil {
1717
t.Fatal("Error loading .env file")
@@ -24,7 +24,8 @@ func loadEnvs(t *testing.T) (string, string) {
2424
if chatID == "" {
2525
t.Fatal("Chat ID not specified in .env file")
2626
}
27-
return tg_token, chatID
27+
topicID := os.Getenv("TOPIC_ID")
28+
return tg_token, chatID, topicID
2829
}
2930

3031
func parse(t *testing.T, rawData []byte) (string, string, string) {
@@ -42,13 +43,13 @@ func parse(t *testing.T, rawData []byte) (string, string, string) {
4243
}
4344

4445
func TestCommitMessage(t *testing.T) {
45-
token, chatID := loadEnvs(t)
46+
token, chatID, topicID := loadEnvs(t)
4647
data, err := ioutil.ReadFile("events/commit.json")
4748
if err != nil {
4849
t.Fatal(err)
4950
}
5051
text, markupText, markupUrl := parse(t, data)
51-
error := utils.SendMessage(token, chatID, text, markupText, markupUrl)
52+
error := utils.SendMessage(token, chatID, text, markupText, markupUrl, topicID)
5253
if error.Description != "" {
5354
t.Fatal(error.String())
5455
}

utils/telegram.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010
"net/url"
1111
)
1212

13-
func SendMessage(token string, chatID string, text string, markupText string, markupUrl string) (error types.Error) {
13+
func SendMessage(token string, chatID string, text string, markupText string, markupUrl string, topicID string) (error types.Error) {
1414
apiBaseUri, _ := url.Parse("https://api.telegram.org")
1515
req_url, _ := url.Parse(fmt.Sprint(apiBaseUri, "/bot", token, "/sendMessage"))
1616
data := map[string]string{
1717
"chat_id": chatID,
1818
"text": text,
1919
"disable_web_page_preview": "true",
20+
"message_thread_id": topicID,
2021
"parse_mode": "html",
2122
}
2223
kyb, err := json.Marshal(map[string][][]map[string]string{

0 commit comments

Comments
 (0)