Skip to content

Commit

Permalink
Merge pull request #18 from cloudnative-id/feature-telegram-channel
Browse files Browse the repository at this point in the history
Feature: telegram channel
  • Loading branch information
zufardhiyaulhaq authored Dec 21, 2020
2 parents 19e7317 + 3a81c2e commit 522e2af
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ telegram:
token: "bot_token"
- chatId: "group_two_chatid"
token: "bot_token"
channel:
- username: "username_channel_one"
token: "bot_token"
- username: "username_channel_two"
token: "bot_token"
twitter:
enabled: false
account:
Expand Down
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Changelog
All notable changes to this project should be documented in this file.

### v0.0.6
- support multiple telegram channel

### v0.0.5
- support for multiple telegram group
- support for multiple twitter account
- support multiple telegram group
- support multiple twitter account
- refactor configuration to file based configuration

### v0.0.4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# community-operator

![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)
![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)

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.

Expand Down
4 changes: 2 additions & 2 deletions charts/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
apiVersion: v1
name: community-operator
type: application
version: 0.0.5
appVersion: 0.0.5
version: 0.0.6
appVersion: 0.0.6
description: community-operator for managing community lifecycle
home: https://github.com/cloudnative-id/community-operator
keywords:
Expand Down
6 changes: 5 additions & 1 deletion charts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ helm install ./charts --name community-operator

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| config.channel[0].token | string | `"bot_token"` | |
| config.channel[0].username | string | `"username_channel_one"` | |
| config.channel[1].token | string | `"bot_token"` | |
| config.channel[1].username | string | `"username_channel_two"` | |
| config.telegram.enabled | bool | `true` | |
| config.telegram.group[0].chatId | string | `"group_one_chatid"` | |
| config.telegram.group[0].token | string | `"bot_token"` | |
Expand All @@ -28,7 +32,7 @@ helm install ./charts --name community-operator
| operator.image | string | `"cloudnativeid/community-operator"` | |
| operator.pullPolicy | string | `"Always"` | |
| operator.replica | int | `1` | |
| operator.tag | string | `"0.0.5"` | |
| operator.tag | string | `"0.0.6"` | |

Specify each parameter using the `--set key=value[,key=value]` argument to helm install. For example:
```
Expand Down
8 changes: 7 additions & 1 deletion charts/values.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
operator:
image: "cloudnativeid/community-operator"
tag: "0.0.5"
tag: "0.0.6"
pullPolicy: "Always"
replica: 1

Expand All @@ -12,6 +12,12 @@ config:
token: "bot_token"
- chatId: "group_two_chatid"
token: "bot_token"
channel:
- username: "username_channel_one"
token: "bot_token"
- username: "username_channel_two"
token: "bot_token"

twitter:
enabled: false
account:
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type Config struct {
ChatID string `yaml:"chatId"`
Token string `yaml:"token"`
} `yaml:"group"`
Channel []struct {
Username string `yaml:"username"`
Token string `yaml:"token"`
} `yaml:"channel"`
} `yaml:"telegram"`
Twitter struct {
Enabled bool `yaml:"enabled"`
Expand Down
55 changes: 41 additions & 14 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"text/template"
"time"

"github.com/cloudnative-id/community-operator/config"
communityv1alpha1 "github.com/cloudnative-id/community-operator/pkg/apis/community/v1alpha1"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)

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

err = yaml.Unmarshal(configFile, &dp.config)
if err != nil {
panic(err)
log.Fatal(err)
}
}

func (dp *Dispatcher) Start() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)

dp.getConfig()
}

func (dp *Dispatcher) sendTelegram(message bytes.Buffer, picture string) {
_, err := dp.telegramDispatcher.SendMessage(message)
func (dp *Dispatcher) sendTelegram(message bytes.Buffer, picture string, chatType string) {
_, err := dp.telegramDispatcher.SendMessage(message, chatType)
if err != nil {
panic(err)
log.Fatal(err)
}

time.Sleep(10 * time.Second)

_, err = dp.telegramDispatcher.SendImage(picture)
_, err = dp.telegramDispatcher.SendImage(picture, chatType)
if err != nil {
panic(err)
log.Error(err)
log.Error("Cannot send image, skipping")
}
}

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

id, err := dp.twitterDispacher.SendMessage(msg[0])
if err != nil {
panic(err)
log.Fatal(err)
}

time.Sleep(5 * time.Second)

for i := 1; i < len(msg); i++ {
id, err = dp.twitterDispacher.ReplyMessage(msg[i], id)
if err != nil {
panic(err)
log.Fatal(err)
}

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

tpl, err := template.ParseFiles(tmpl)
if err != nil {
panic(err)
log.Fatal(err)
}

err = tpl.Execute(&output, data)
if err != nil {
panic(err)
log.Fatal(err)
}

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

telegramData := populateTemplate("telegram", weeklyData)
dp.sendTelegram(telegramData, weeklyData.Image)
dp.sendTelegram(telegramData, weeklyData.Image, "group")
}

for _, config := range dp.config.Telegram.Channel {
dp.telegramDispatcher.Start(config.Token, config.Username)

telegramData := populateTemplate("telegram", weeklyData)
dp.sendTelegram(telegramData, weeklyData.Image, "channel")
}
}

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

telegramData := populateTemplate("telegram", meetupData)
dp.sendTelegram(telegramData, meetupData.Image)
dp.sendTelegram(telegramData, meetupData.Image, "group")
}

for _, config := range dp.config.Telegram.Channel {
dp.telegramDispatcher.Start(config.Token, config.Username)

telegramData := populateTemplate("telegram", meetupData)
dp.sendTelegram(telegramData, meetupData.Image, "channel")
}
}

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

telegramData := populateTemplate("telegram", announcementData)
dp.sendTelegram(telegramData, announcementData.Image)
dp.sendTelegram(telegramData, announcementData.Image, "group")
}

for _, config := range dp.config.Telegram.Channel {
dp.telegramDispatcher.Start(config.Token, config.Username)

telegramData := populateTemplate("telegram", announcementData)
dp.sendTelegram(telegramData, announcementData.Image, "channel")
}
}

Expand Down
61 changes: 42 additions & 19 deletions dispatcher/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,73 @@ package dispatcher

import (
"bytes"
"fmt"
"strconv"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
)

type TelegramDispatcher struct {
token string
chatID int
bot *tgbotapi.BotAPI
msg tgbotapi.MessageConfig
pic tgbotapi.PhotoConfig
token string
credential string
bot *tgbotapi.BotAPI
msg tgbotapi.MessageConfig
pic tgbotapi.PhotoConfig
}

func (t *TelegramDispatcher) setCredential(token string, chatID string) {
var err error

func (t *TelegramDispatcher) setCredential(token string, credential string) {
t.token = token
t.chatID, err = strconv.Atoi(chatID)
if err != nil {
panic(err)
}
t.credential = credential
}

func (t *TelegramDispatcher) Start(token string, chatID string) {
func (t *TelegramDispatcher) Start(token string, credential string) {
var err error

t.setCredential(token, chatID)
t.setCredential(token, credential)
t.bot, err = tgbotapi.NewBotAPI(t.token)
if err != nil {
panic(err)
log.Fatal(err)
}
}

func (t *TelegramDispatcher) SendMessage(output bytes.Buffer) (int, error) {
t.msg = tgbotapi.NewMessage(int64(t.chatID), output.String())
func (t *TelegramDispatcher) SendMessage(output bytes.Buffer, chatType string) (int, error) {
switch chatType {
case "group":
chatID, err := strconv.Atoi(t.credential)
if err != nil {
log.Fatal(err)
}
t.msg = tgbotapi.NewMessage(int64(chatID), output.String())
case "channel":
fmt.Println(t.credential)
t.msg = tgbotapi.NewMessageToChannel(t.credential, output.String())
}

t.msg.ParseMode = "markdown"
t.msg.DisableWebPagePreview = true

msg, err := t.bot.Send(t.msg)
return msg.MessageID, err
}

func (t *TelegramDispatcher) SendImage(url string) (int, error) {
t.pic = tgbotapi.NewPhotoShare(int64(t.chatID), url)
func (t *TelegramDispatcher) SendImage(url string, chatType string) (int, error) {
switch chatType {
case "group":
chatID, err := strconv.Atoi(t.credential)
if err != nil {
log.Fatal(err)
}
t.pic = tgbotapi.NewPhotoShare(int64(chatID), url)
case "channel":
t.pic = tgbotapi.PhotoConfig{
BaseFile: tgbotapi.BaseFile{
BaseChat: tgbotapi.BaseChat{ChannelUsername: t.credential},
FileID: url,
UseExisting: true,
},
}
}

msg, err := t.bot.Send(t.pic)
return msg.MessageID, err
Expand Down
3 changes: 2 additions & 1 deletion dispatcher/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dispatcher
import (
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
log "github.com/sirupsen/logrus"
)

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

if err != nil {
panic(err)
log.Fatal(err)
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/gomodule/oauth1 v0.0.0-20181215000758-9a59ed3b0a84
github.com/norwoodj/helm-docs v1.4.0 // indirect
github.com/operator-framework/operator-sdk v0.18.1
github.com/sirupsen/logrus v1.5.0
github.com/spf13/pflag v1.0.5
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/zufardhiyaulhaq/ngrok-operator v0.0.0-20201031134108-a79211c21da1 // indirect
Expand Down

0 comments on commit 522e2af

Please sign in to comment.