Skip to content

Commit

Permalink
Add Telegram support
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Jun 27, 2019
1 parent 9348c13 commit 86aab80
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ This client implements all NewReleases API features.
- Get project release note
- Get tracked providers
- Get added Slack Channels
- Get added Telegram Chats
- Get added Hangouts Chat webhooks
- Get added Microsoft Teams webhooks
- Get added custom Webhooks
Expand Down
2 changes: 2 additions & 0 deletions newreleases.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Client struct {
Projects *ProjectsService
Releases *ReleasesService
SlackChannels *SlackChannelsService
TelegramChats *TelegramChatsService
HangoutsChatWebhooks *HangoutsChatWebhooksService
MicrosoftTeamsWebhooks *MicrosoftTeamsWebhooksService
Webhooks *WebhooksService
Expand Down Expand Up @@ -85,6 +86,7 @@ func newClient(httpClient *http.Client) (c *Client) {
c.Projects = (*ProjectsService)(&c.service)
c.Releases = (*ReleasesService)(&c.service)
c.SlackChannels = (*SlackChannelsService)(&c.service)
c.TelegramChats = (*TelegramChatsService)(&c.service)
c.HangoutsChatWebhooks = (*HangoutsChatWebhooksService)(&c.service)
c.MicrosoftTeamsWebhooks = (*MicrosoftTeamsWebhooksService)(&c.service)
c.Webhooks = (*WebhooksService)(&c.service)
Expand Down
2 changes: 2 additions & 0 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Project struct {
URL string `json:"url"`
EmailNotification EmailNotification `json:"email_notification,omitempty"`
SlackIDs []string `json:"slack_channels,omitempty"`
TelegramChatIDs []string `json:"telegram_chats,omitempty"`
HangoutsChatWebhookIDs []string `json:"hangouts_chat_webhooks,omitempty"`
MSTeamsWebhookIDs []string `json:"microsoft_teams_webhooks,omitempty"`
WebhookIDs []string `json:"webhooks,omitempty"`
Expand Down Expand Up @@ -146,6 +147,7 @@ func (s *ProjectsService) get(ctx context.Context, projectRef string) (project *
type ProjectOptions struct {
EmailNotification *EmailNotification `json:"email_notification"`
SlackIDs []string `json:"slack_channels"`
TelegramChatIDs []string `json:"telegram_chats"`
HangoutsChatWebhookIDs []string `json:"hangouts_chat_webhooks"`
MSTeamsWebhookIDs []string `json:"microsoft_teams_webhooks"`
WebhookIDs []string `json:"webhooks"`
Expand Down
2 changes: 2 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ var (
Provider: "github",
EmailNotification: newreleases.EmailNotificationHourly,
SlackIDs: []string{"slack123"},
TelegramChatIDs: []string{"telegram123"},
HangoutsChatWebhookIDs: []string{"hangouts123"},
MSTeamsWebhookIDs: []string{"teams123"},
WebhookIDs: []string{"webhook123", "webhook124"},
Expand All @@ -414,6 +415,7 @@ var (
projectOptions = &newreleases.ProjectOptions{
EmailNotification: &newreleases.EmailNotificationHourly,
SlackIDs: []string{"slack123"},
TelegramChatIDs: []string{"telegram123"},
HangoutsChatWebhookIDs: []string{"hangouts123"},
MSTeamsWebhookIDs: []string{"teams123"},
WebhookIDs: []string{"webhook123", "webhook124"},
Expand Down
34 changes: 34 additions & 0 deletions telegram_chats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2019, NewReleases Go client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package newreleases

import (
"context"
"net/http"
)

// TelegramChatsService provides information about Telegram notifications.
type TelegramChatsService service

// TelegramChat holds information about a Telegram Chat which receives
// notifications.
type TelegramChat struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
}

// List returns all connected Slack Channels.
func (s *TelegramChatsService) List(ctx context.Context) (channels []TelegramChat, err error) {

type TelegramChatsResponse struct {
Chats []TelegramChat `json:"chats"`
}

var r TelegramChatsResponse
err = s.client.request(ctx, http.MethodGet, "v1/telegram-chats", nil, &r)
return r.Chats, err
}
53 changes: 53 additions & 0 deletions telegram_chats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2019, NewReleases Go client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package newreleases_test

import (
"context"
"fmt"
"net/http"
"testing"

"newreleases.io/newreleases"
)

func TestTelegramChatsService_List(t *testing.T) {
client, mux, _, teardown := newClient(t, "")
defer teardown()

mux.HandleFunc("/v1/telegram-chats", requireMethod("GET", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", jsonContentType)
fmt.Fprintln(w, telegramChatsServiceList)
}))

got, err := client.TelegramChats.List(context.Background())
if err != nil {
t.Fatal(err)
}

assertEqual(t, "", got, telegramChatsServiceListWant)
}

var (
telegramChatsServiceList = `
{
"chats": [
{
"id": "00q3pe8gf7322d8n52bgrl551",
"type": "group",
"name": "My Telegram Chat"
}
]
}
`
telegramChatsServiceListWant = []newreleases.TelegramChat{
{
ID: "00q3pe8gf7322d8n52bgrl551",
Type: "group",
Name: "My Telegram Chat",
},
}
)

0 comments on commit 86aab80

Please sign in to comment.