Skip to content

Commit

Permalink
Add capability to ignore channels (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Divkix committed Jun 3, 2022
1 parent d23b481 commit 0353cd0
Show file tree
Hide file tree
Showing 764 changed files with 380,298 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build Stage: Build bot using the alpine image, also install doppler in it
FROM golang:1.18.2-alpine3.15 AS builder
FROM golang:1.18-alpine AS builder
RUN apk add --update --no-cache git
WORKDIR /app
COPY . .
Expand Down
14 changes: 0 additions & 14 deletions Dockerfile.doppler

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Can be found on telegram as [@RestrictChannelRobot](https://telegram.dog/Restric
# Variables
---------------
- `BOT_TOKEN` - Get it from [@BotFather](https://t.me/BotFather)
- `DB_URI` - MongoDB Connection String
- `DB_NAME` - MongoDB Database Name
-----------------

* Some advanced configuration may also require other variables, but with just bot token you are good to go!
Expand Down
10 changes: 10 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"description": "Bot Token, Get It From @BotFather.",
"value": "",
"required": true
},
"DB_URI": {
"description": "MongoDB Connection String.",
"value": "",
"required": true
},
"DB_NAME": {
"description": "MongoDB Database Name.",
"value": "",
"required": true
}
},
"buildpacks": [
Expand Down
4 changes: 0 additions & 4 deletions captain-definition

This file was deleted.

5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var (
botToken string
apiUrl string
webhookUrl string
databaseUrl string
databaseName string
)

func init() {
Expand Down Expand Up @@ -42,6 +44,9 @@ func init() {
"Port for the webhook",
)

flag.StringVar(&databaseUrl, "DB_URI", os.Getenv("DB_URI"), "Database URI for MongoDB")
flag.StringVar(&databaseName, "DB_NAME", os.Getenv("DB_NAME"), "Bot database name in MongoDB")

if apiUrl == "" {
apiUrl = "https://api.telegram.org"
}
Expand Down
108 changes: 108 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"context"
"time"

log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

type IgnoreList struct {
ChatId int64 `bson:"_id,omitempty" json:"_id,omitempty"`
IgnoredChannels []int64 `bson:"ignored_channels" json:"ignored_channels"`
}

var (
mongoClient *mongo.Client

// Contexts
tdCtx = context.TODO()
bgCtx = context.Background()

// define collections
ignoreListCollection *mongo.Collection
)

func init() {
mongoClient, err := mongo.NewClient(
options.Client().ApplyURI(databaseUrl),
)
if err != nil {
log.Errorf("[Database][Client]: %v", err)
}

ctx, cancel := context.WithTimeout(bgCtx, 10*time.Second)
defer cancel()

err = mongoClient.Connect(ctx)
if err != nil {
log.Errorf("[Database][Connect]: %v", err)
}

// Open Connections to Collections
log.Info("Opening Database Collections...")
ignoreListCollection = mongoClient.Database(databaseName).Collection("ignore_list")
}

func updateOne(collecion *mongo.Collection, filter bson.M, data interface{}) (err error) {
_, err = collecion.UpdateOne(tdCtx, filter, bson.M{"$set": data}, options.Update().SetUpsert(true))
if err != nil {
log.Errorf("[Database][updateOne]: %v", err)
}
return
}

func findOne(collecion *mongo.Collection, filter bson.M) (res *mongo.SingleResult) {
res = collecion.FindOne(tdCtx, filter)
return
}

func getIgnoreSettings(chatID int64) *IgnoreList {
return _checkIgnoreSettings(chatID)
}

func _checkIgnoreSettings(chatID int64) (ignorerc *IgnoreList) {
defaultIgnoreSettings := &IgnoreList{ChatId: chatID, IgnoredChannels: make([]int64, 0)}

errS := findOne(ignoreListCollection, bson.M{"_id": chatID}).Decode(&ignorerc)
if errS == mongo.ErrNoDocuments {
ignorerc = defaultIgnoreSettings
err := updateOne(ignoreListCollection, bson.M{"_id": chatID}, defaultIgnoreSettings)
if err != nil {
log.Errorf("[Database][_checkIgnoreSettings]: %v ", err)
}
} else if errS != nil {
log.Errorf("[Database][_checkIgnoreSettings]: %v", errS)
ignorerc = defaultIgnoreSettings
}
return ignorerc
}

func ignoreChat(chatID, ignoreChannelId int64) {
ignorerc := _checkIgnoreSettings(chatID)
ignorerc.IgnoredChannels = append(
ignorerc.IgnoredChannels,
ignoreChannelId,
)
err := updateOne(ignoreListCollection, bson.M{"_id": chatID}, ignorerc)
if err != nil {
log.Errorf("[Database] ignoreChat: %v", err)
}
}

func unignoreChat(chatID, ignoreChannelId int64) {
ignorerc := _checkIgnoreSettings(chatID)
for i, v := range ignorerc.IgnoredChannels {
if v == ignoreChannelId {
ignorerc.IgnoredChannels = append(ignorerc.IgnoredChannels[:i], ignorerc.IgnoredChannels[i+1:]...)
break
}
}
err := updateOne(ignoreListCollection, bson.M{"_id": chatID}, ignorerc)
if err != nil {
log.Errorf("[Database] unignoreChat: %v", err)
}
}
21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,23 @@ module github.com/DivideProjects/GoLangTgBot
// +heroku goVersion go1.18
go 1.18

require github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.8
require (
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.8
github.com/sirupsen/logrus v1.8.1
go.mongodb.org/mongo-driver v1.9.1
)

require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
golang.org/x/text v0.3.5 // indirect
)
60 changes: 60 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.8 h1:ISVCuGnT0yYmdlfhJ8LQS4G2UXf9SuDWpvTSH86Mh2I=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.8/go.mod h1:r815fYWTudnU9JhtsJAxUtuV7QrSgKpChJkfTSMFpfg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w=
github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c=
go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU=
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 0353cd0

Please sign in to comment.