Skip to content

Commit

Permalink
FEAT: add support for topics
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Nov 25, 2022
1 parent ce582cc commit 27a5fbf
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
Expand Down
9 changes: 5 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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())
}
Expand Down
3 changes: 2 additions & 1 deletion utils/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 27a5fbf

Please sign in to comment.