Skip to content

Commit 522e2af

Browse files
Merge pull request #18 from cloudnative-id/feature-telegram-channel
Feature: telegram channel
2 parents 19e7317 + 3a81c2e commit 522e2af

11 files changed

Lines changed: 115 additions & 41 deletions

File tree

.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ telegram:
55
token: "bot_token"
66
- chatId: "group_two_chatid"
77
token: "bot_token"
8+
channel:
9+
- username: "username_channel_one"
10+
token: "bot_token"
11+
- username: "username_channel_two"
12+
token: "bot_token"
813
twitter:
914
enabled: false
1015
account:

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Changelog
22
All notable changes to this project should be documented in this file.
33

4+
### v0.0.6
5+
- support multiple telegram channel
6+
47
### v0.0.5
5-
- support for multiple telegram group
6-
- support for multiple twitter account
8+
- support multiple telegram group
9+
- support multiple twitter account
710
- refactor configuration to file based configuration
811

912
### v0.0.4

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# community-operator
22

3-
![Version: 0.0.5](https://img.shields.io/badge/Version-0.0.5-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.0.5](https://img.shields.io/badge/AppVersion-0.0.5-informational?style=flat-square)
3+
![Version: 0.0.6](https://img.shields.io/badge/Version-0.0.6-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.0.6](https://img.shields.io/badge/AppVersion-0.0.6-informational?style=flat-square)
44

55
The Community Operator provides Kubernetes native deployment and management of your community. The purpose of this project is to simplify and automate community management in top of Kubernetes clusters.
66

charts/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
apiVersion: v1
33
name: community-operator
44
type: application
5-
version: 0.0.5
6-
appVersion: 0.0.5
5+
version: 0.0.6
6+
appVersion: 0.0.6
77
description: community-operator for managing community lifecycle
88
home: https://github.com/cloudnative-id/community-operator
99
keywords:

charts/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ helm install ./charts --name community-operator
1111

1212
| Key | Type | Default | Description |
1313
|-----|------|---------|-------------|
14+
| config.channel[0].token | string | `"bot_token"` | |
15+
| config.channel[0].username | string | `"username_channel_one"` | |
16+
| config.channel[1].token | string | `"bot_token"` | |
17+
| config.channel[1].username | string | `"username_channel_two"` | |
1418
| config.telegram.enabled | bool | `true` | |
1519
| config.telegram.group[0].chatId | string | `"group_one_chatid"` | |
1620
| config.telegram.group[0].token | string | `"bot_token"` | |
@@ -28,7 +32,7 @@ helm install ./charts --name community-operator
2832
| operator.image | string | `"cloudnativeid/community-operator"` | |
2933
| operator.pullPolicy | string | `"Always"` | |
3034
| operator.replica | int | `1` | |
31-
| operator.tag | string | `"0.0.5"` | |
35+
| operator.tag | string | `"0.0.6"` | |
3236

3337
Specify each parameter using the `--set key=value[,key=value]` argument to helm install. For example:
3438
```

charts/values.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
operator:
22
image: "cloudnativeid/community-operator"
3-
tag: "0.0.5"
3+
tag: "0.0.6"
44
pullPolicy: "Always"
55
replica: 1
66

@@ -12,6 +12,12 @@ config:
1212
token: "bot_token"
1313
- chatId: "group_two_chatid"
1414
token: "bot_token"
15+
channel:
16+
- username: "username_channel_one"
17+
token: "bot_token"
18+
- username: "username_channel_two"
19+
token: "bot_token"
20+
1521
twitter:
1622
enabled: false
1723
account:

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ type Config struct {
77
ChatID string `yaml:"chatId"`
88
Token string `yaml:"token"`
99
} `yaml:"group"`
10+
Channel []struct {
11+
Username string `yaml:"username"`
12+
Token string `yaml:"token"`
13+
} `yaml:"channel"`
1014
} `yaml:"telegram"`
1115
Twitter struct {
1216
Enabled bool `yaml:"enabled"`

dispatcher/dispatcher.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"os"
78
"sort"
89
"strings"
910
"text/template"
1011
"time"
1112

1213
"github.com/cloudnative-id/community-operator/config"
1314
communityv1alpha1 "github.com/cloudnative-id/community-operator/pkg/apis/community/v1alpha1"
15+
log "github.com/sirupsen/logrus"
1416
"sigs.k8s.io/yaml"
1517
)
1618

@@ -23,30 +25,34 @@ type Dispatcher struct {
2325
func (dp *Dispatcher) getConfig() {
2426
configFile, err := ioutil.ReadFile("/etc/community-operator/community-operator-config.yaml")
2527
if err != nil {
26-
panic(err)
28+
log.Fatal(err)
2729
}
2830

2931
err = yaml.Unmarshal(configFile, &dp.config)
3032
if err != nil {
31-
panic(err)
33+
log.Fatal(err)
3234
}
3335
}
3436

3537
func (dp *Dispatcher) Start() {
38+
log.SetFormatter(&log.JSONFormatter{})
39+
log.SetOutput(os.Stdout)
40+
3641
dp.getConfig()
3742
}
3843

39-
func (dp *Dispatcher) sendTelegram(message bytes.Buffer, picture string) {
40-
_, err := dp.telegramDispatcher.SendMessage(message)
44+
func (dp *Dispatcher) sendTelegram(message bytes.Buffer, picture string, chatType string) {
45+
_, err := dp.telegramDispatcher.SendMessage(message, chatType)
4146
if err != nil {
42-
panic(err)
47+
log.Fatal(err)
4348
}
4449

4550
time.Sleep(10 * time.Second)
4651

47-
_, err = dp.telegramDispatcher.SendImage(picture)
52+
_, err = dp.telegramDispatcher.SendImage(picture, chatType)
4853
if err != nil {
49-
panic(err)
54+
log.Error(err)
55+
log.Error("Cannot send image, skipping")
5056
}
5157
}
5258

@@ -56,15 +62,15 @@ func (dp *Dispatcher) sendTwitter(message bytes.Buffer, picture string) {
5662

5763
id, err := dp.twitterDispacher.SendMessage(msg[0])
5864
if err != nil {
59-
panic(err)
65+
log.Fatal(err)
6066
}
6167

6268
time.Sleep(5 * time.Second)
6369

6470
for i := 1; i < len(msg); i++ {
6571
id, err = dp.twitterDispacher.ReplyMessage(msg[i], id)
6672
if err != nil {
67-
panic(err)
73+
log.Fatal(err)
6874
}
6975

7076
time.Sleep(5 * time.Second)
@@ -86,12 +92,12 @@ func populateTemplate(dispatcher string, data interface{}) bytes.Buffer {
8692

8793
tpl, err := template.ParseFiles(tmpl)
8894
if err != nil {
89-
panic(err)
95+
log.Fatal(err)
9096
}
9197

9298
err = tpl.Execute(&output, data)
9399
if err != nil {
94-
panic(err)
100+
log.Fatal(err)
95101
}
96102

97103
return output
@@ -107,7 +113,14 @@ func (dp *Dispatcher) SendWeekly(weeklyData communityv1alpha1.WeeklySpec) {
107113
dp.telegramDispatcher.Start(config.Token, config.ChatID)
108114

109115
telegramData := populateTemplate("telegram", weeklyData)
110-
dp.sendTelegram(telegramData, weeklyData.Image)
116+
dp.sendTelegram(telegramData, weeklyData.Image, "group")
117+
}
118+
119+
for _, config := range dp.config.Telegram.Channel {
120+
dp.telegramDispatcher.Start(config.Token, config.Username)
121+
122+
telegramData := populateTemplate("telegram", weeklyData)
123+
dp.sendTelegram(telegramData, weeklyData.Image, "channel")
111124
}
112125
}
113126

@@ -127,7 +140,14 @@ func (dp *Dispatcher) SendMeetup(meetupData communityv1alpha1.MeetupSpec) {
127140
dp.telegramDispatcher.Start(config.Token, config.ChatID)
128141

129142
telegramData := populateTemplate("telegram", meetupData)
130-
dp.sendTelegram(telegramData, meetupData.Image)
143+
dp.sendTelegram(telegramData, meetupData.Image, "group")
144+
}
145+
146+
for _, config := range dp.config.Telegram.Channel {
147+
dp.telegramDispatcher.Start(config.Token, config.Username)
148+
149+
telegramData := populateTemplate("telegram", meetupData)
150+
dp.sendTelegram(telegramData, meetupData.Image, "channel")
131151
}
132152
}
133153

@@ -147,7 +167,14 @@ func (dp *Dispatcher) SendAnnouncement(announcementData communityv1alpha1.Announ
147167
dp.telegramDispatcher.Start(config.Token, config.ChatID)
148168

149169
telegramData := populateTemplate("telegram", announcementData)
150-
dp.sendTelegram(telegramData, announcementData.Image)
170+
dp.sendTelegram(telegramData, announcementData.Image, "group")
171+
}
172+
173+
for _, config := range dp.config.Telegram.Channel {
174+
dp.telegramDispatcher.Start(config.Token, config.Username)
175+
176+
telegramData := populateTemplate("telegram", announcementData)
177+
dp.sendTelegram(telegramData, announcementData.Image, "channel")
151178
}
152179
}
153180

dispatcher/telegram.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,73 @@ package dispatcher
22

33
import (
44
"bytes"
5+
"fmt"
56
"strconv"
67

78
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
9+
log "github.com/sirupsen/logrus"
810
)
911

1012
type TelegramDispatcher struct {
11-
token string
12-
chatID int
13-
bot *tgbotapi.BotAPI
14-
msg tgbotapi.MessageConfig
15-
pic tgbotapi.PhotoConfig
13+
token string
14+
credential string
15+
bot *tgbotapi.BotAPI
16+
msg tgbotapi.MessageConfig
17+
pic tgbotapi.PhotoConfig
1618
}
1719

18-
func (t *TelegramDispatcher) setCredential(token string, chatID string) {
19-
var err error
20-
20+
func (t *TelegramDispatcher) setCredential(token string, credential string) {
2121
t.token = token
22-
t.chatID, err = strconv.Atoi(chatID)
23-
if err != nil {
24-
panic(err)
25-
}
22+
t.credential = credential
2623
}
2724

28-
func (t *TelegramDispatcher) Start(token string, chatID string) {
25+
func (t *TelegramDispatcher) Start(token string, credential string) {
2926
var err error
3027

31-
t.setCredential(token, chatID)
28+
t.setCredential(token, credential)
3229
t.bot, err = tgbotapi.NewBotAPI(t.token)
3330
if err != nil {
34-
panic(err)
31+
log.Fatal(err)
3532
}
3633
}
3734

38-
func (t *TelegramDispatcher) SendMessage(output bytes.Buffer) (int, error) {
39-
t.msg = tgbotapi.NewMessage(int64(t.chatID), output.String())
35+
func (t *TelegramDispatcher) SendMessage(output bytes.Buffer, chatType string) (int, error) {
36+
switch chatType {
37+
case "group":
38+
chatID, err := strconv.Atoi(t.credential)
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
t.msg = tgbotapi.NewMessage(int64(chatID), output.String())
43+
case "channel":
44+
fmt.Println(t.credential)
45+
t.msg = tgbotapi.NewMessageToChannel(t.credential, output.String())
46+
}
47+
4048
t.msg.ParseMode = "markdown"
4149
t.msg.DisableWebPagePreview = true
4250

4351
msg, err := t.bot.Send(t.msg)
4452
return msg.MessageID, err
4553
}
4654

47-
func (t *TelegramDispatcher) SendImage(url string) (int, error) {
48-
t.pic = tgbotapi.NewPhotoShare(int64(t.chatID), url)
55+
func (t *TelegramDispatcher) SendImage(url string, chatType string) (int, error) {
56+
switch chatType {
57+
case "group":
58+
chatID, err := strconv.Atoi(t.credential)
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
t.pic = tgbotapi.NewPhotoShare(int64(chatID), url)
63+
case "channel":
64+
t.pic = tgbotapi.PhotoConfig{
65+
BaseFile: tgbotapi.BaseFile{
66+
BaseChat: tgbotapi.BaseChat{ChannelUsername: t.credential},
67+
FileID: url,
68+
UseExisting: true,
69+
},
70+
}
71+
}
4972

5073
msg, err := t.bot.Send(t.pic)
5174
return msg.MessageID, err

dispatcher/twitter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dispatcher
33
import (
44
"github.com/dghubble/go-twitter/twitter"
55
"github.com/dghubble/oauth1"
6+
log "github.com/sirupsen/logrus"
67
)
78

89
type TwitterDispatcher struct {
@@ -18,7 +19,7 @@ func (t *TwitterDispatcher) getCredential(apiKey string, apiSecretKey string, ac
1819
t.token = oauth1.NewToken(accessToken, accessTokenSecret)
1920

2021
if err != nil {
21-
panic(err)
22+
log.Fatal(err)
2223
}
2324
}
2425

0 commit comments

Comments
 (0)