From 5bfccfb1a74f76817702b2ef4bec604370619985 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Mon, 6 Jul 2020 15:01:54 +0530 Subject: [PATCH 1/3] refactor welcome function to new file --- telegram-bot/main.go | 33 -------------------------------- telegram-bot/newUser.go | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 telegram-bot/newUser.go diff --git a/telegram-bot/main.go b/telegram-bot/main.go index c6d3af1..90d01c1 100644 --- a/telegram-bot/main.go +++ b/telegram-bot/main.go @@ -81,39 +81,6 @@ func dlmeetups(ID int64) { bot.Send(tbot.NewMessage(ID, finallist)) } -func welcome(user tbot.User, ID int64) { - User := fmt.Sprintf("[%v](tg://user?id=%v)", user.FirstName, user.ID) - var botSlice = make([]string, 0) - botSlice = append(botSlice, - "helping the hackers in here.", - "a bot made by the geeks for the geeks.", - "an Autobot on Earth to promote open source.", - "a distant cousin of the mars rover.", - "a friendly bot written in Golang.", - ) - var quesSlice = make([]string, 0) - quesSlice = append(quesSlice, - "which language do you work with?", - "what do you want to learn?", - "what is your current operating system?", - "if you are new to open source.", - "which technology fascinates you the most?", - "what have you been exploring recently?", - ) - - rand.Seed(time.Now().UnixNano()) - min := 0 - max := len(botSlice) - 1 - randomNum1 := (rand.Intn(max-min+1) + min) - randomNum2 := (rand.Intn(max-min+1) + min) - var welcomeMessage1 = fmt.Sprintf(botSlice[randomNum1]) - var welcomeMessage2 = fmt.Sprintf(quesSlice[randomNum2]) - reply := tbot.NewMessage(ID, "Welcome "+User+", I am the OSDC-bot, "+welcomeMessage1+ - " Please introduce yourself. To start with, you can tell us "+welcomeMessage2) - reply.ParseMode = "markdown" - bot.Send(reply) -} - //extracts the details of the user who sent the message to check whether the user is creator/admin. Returns true in this case else false. func memberdetails(ID int64, userid int) bool { response, _ := bot.GetChatMember(tbot.ChatConfigWithUser{ diff --git a/telegram-bot/newUser.go b/telegram-bot/newUser.go new file mode 100644 index 0000000..979fe6e --- /dev/null +++ b/telegram-bot/newUser.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "math/rand" + "time" + + tbot "github.com/go-telegram-bot-api/telegram-bot-api" +) + +func welcome(user tbot.User, ID int64) { + User := fmt.Sprintf("[%v](tg://user?id=%v)", user.FirstName, user.ID) + var botSlice = make([]string, 0) + botSlice = append(botSlice, + "helping the hackers in here.", + "a bot made by the geeks for the geeks.", + "an Autobot on Earth to promote open source.", + "a distant cousin of the mars rover.", + "a friendly bot written in Golang.", + ) + var quesSlice = make([]string, 0) + quesSlice = append(quesSlice, + "which language do you work with?", + "what do you want to learn?", + "what is your current operating system?", + "if you are new to open source.", + "which technology fascinates you the most?", + "what have you been exploring recently?", + ) + + rand.Seed(time.Now().UnixNano()) + min := 0 + max := len(botSlice) - 1 + randomNum1 := (rand.Intn(max-min+1) + min) + randomNum2 := (rand.Intn(max-min+1) + min) + var welcomeMessage1 = fmt.Sprintf(botSlice[randomNum1]) + var welcomeMessage2 = fmt.Sprintf(quesSlice[randomNum2]) + reply := tbot.NewMessage(ID, "Welcome "+User+", I am the OSDC-bot, "+welcomeMessage1+ + " Please introduce yourself. To start with, you can tell us "+welcomeMessage2) + reply.ParseMode = "markdown" + bot.Send(reply) +} From 521ae367e177ae7cd78752fef4d7ff013595fde0 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Tue, 7 Jul 2020 00:28:54 +0530 Subject: [PATCH 2/3] Acknowledge the users when they have introduced --- telegram-bot/go.sum | 1 + telegram-bot/main.go | 4 +++- telegram-bot/newUser.go | 53 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/telegram-bot/go.sum b/telegram-bot/go.sum index f3e134e..b5614a4 100644 --- a/telegram-bot/go.sum +++ b/telegram-bot/go.sum @@ -92,6 +92,7 @@ github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHM go.mongodb.org/mongo-driver v1.3.0 h1:ew6uUIeJOo+qdUUv7LxFCUhtWmVv7ZV/Xuy4FAUsw2E= go.mongodb.org/mongo-driver v1.3.0/go.mod h1:MSWZXKOynuguX+JSvwP8i+58jYCXxbia8HS3gZBapIE= go.mongodb.org/mongo-driver v1.3.2 h1:IYppNjEV/C+/3VPbhHVxQ4t04eVW0cLp0/pNdW++6Ug= +go.mongodb.org/mongo-driver v1.3.4 h1:zs/dKNwX0gYUtzwrN9lLiR15hCO0nDwQj5xXx+vjCdE= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= diff --git a/telegram-bot/main.go b/telegram-bot/main.go index 90d01c1..26b795c 100644 --- a/telegram-bot/main.go +++ b/telegram-bot/main.go @@ -187,6 +187,8 @@ func main() { deletenote(ID, update.Message.Text, *client) case "fetchnote": fetchnote(ID, update.Message.Text, *client) + case "introduction": + introverify(update.Message.From, ID, *client) default: bot.Send(tbot.NewMessage(ID, "I don't know that command")) } @@ -196,7 +198,7 @@ func main() { if user.IsBot && user.UserName != "osdcbot" { go kickUser(user.ID, ID) } else { - go welcome(user, ID) + go welcome(user, ID, *client) } } } diff --git a/telegram-bot/newUser.go b/telegram-bot/newUser.go index 979fe6e..010566e 100644 --- a/telegram-bot/newUser.go +++ b/telegram-bot/newUser.go @@ -1,14 +1,26 @@ package main import ( + "context" "fmt" + "log" "math/rand" "time" tbot "github.com/go-telegram-bot-api/telegram-bot-api" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" ) -func welcome(user tbot.User, ID int64) { +type newUser struct { + UserName string + FirstName string + UserID int + Introduction bool + JoinDate time.Time +} + +func welcome(user tbot.User, ID int64, client mongo.Client) { User := fmt.Sprintf("[%v](tg://user?id=%v)", user.FirstName, user.ID) var botSlice = make([]string, 0) botSlice = append(botSlice, @@ -28,6 +40,21 @@ func welcome(user tbot.User, ID int64) { "what have you been exploring recently?", ) + collection := client.Database("test").Collection("user") + data := newUser{ + UserName: user.UserName, + FirstName: user.FirstName, + UserID: user.ID, + Introduction: false, + JoinDate: time.Now(), + } + log.Println(data) + check, err := collection.InsertOne(context.TODO(), data) + log.Println(check) + if err != nil { + log.Fatal(err) + } + rand.Seed(time.Now().UnixNano()) min := 0 max := len(botSlice) - 1 @@ -36,7 +63,29 @@ func welcome(user tbot.User, ID int64) { var welcomeMessage1 = fmt.Sprintf(botSlice[randomNum1]) var welcomeMessage2 = fmt.Sprintf(quesSlice[randomNum2]) reply := tbot.NewMessage(ID, "Welcome "+User+", I am the OSDC-bot, "+welcomeMessage1+ - " Please introduce yourself. To start with, you can tell us "+welcomeMessage2) + " Please introduce yourself. To start with, you can tell us "+welcomeMessage2+" Start your message with /introduction so that I can verify you.") reply.ParseMode = "markdown" bot.Send(reply) } + +func introverify(user *tbot.User, ID int64, client mongo.Client) { + collection := client.Database("test").Collection("user") + filter := bson.M{"userid": user.ID, "introduction": false} + update := bson.M{"$set": bson.M{"introduction": true}} + result, err := collection.UpdateOne(context.TODO(), filter, update) + if err != nil { + // ErrNoDocuments means that the filter did not match any documents in the collection + if err == mongo.ErrNoDocuments { + log.Println("no documents") + bot.Send(tbot.NewMessage(ID, "No need to introduce again, I already know you.")) + return + } + log.Fatal(err) + } + if result.MatchedCount > 0 { + bot.Send(tbot.NewMessage(ID, "Thanks for introducing yourself, You are now a verified member of OSDC :) ")) + } else { + bot.Send(tbot.NewMessage(ID, "No need to introduce again, I already know you.")) + } + +} From 698a5609874c2acebffd0778807b1a64f9ac4107 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Tue, 7 Jul 2020 02:17:49 +0530 Subject: [PATCH 3/3] Kick user if not introduced themselves after 24H --- telegram-bot/main.go | 6 +++++- telegram-bot/newUser.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/telegram-bot/main.go b/telegram-bot/main.go index 26b795c..cf5ae77 100644 --- a/telegram-bot/main.go +++ b/telegram-bot/main.go @@ -10,6 +10,7 @@ import ( "time" "github.com/anaskhan96/soup" + "github.com/go-co-op/gocron" tbot "github.com/go-telegram-bot-api/telegram-bot-api" "go.mongodb.org/mongo-driver/mongo" @@ -111,7 +112,7 @@ func main() { fmt.Println("error in auth") log.Panic(err) } - bot.Debug = true + // bot.Debug = true log.Printf("Authorized on account %s", bot.Self.UserName) u := tbot.NewUpdate(0) @@ -141,6 +142,9 @@ func main() { } ID := update.Message.Chat.ID + cron := gocron.NewScheduler(time.Local) + cron.Every(12).Hour().Do(introkick, ID, *client) + cron.Start() log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) if update.Message.IsCommand() { switch update.Message.Command() { diff --git a/telegram-bot/newUser.go b/telegram-bot/newUser.go index 010566e..223c142 100644 --- a/telegram-bot/newUser.go +++ b/telegram-bot/newUser.go @@ -89,3 +89,42 @@ func introverify(user *tbot.User, ID int64, client mongo.Client) { } } + +func introkick(ID int64, client mongo.Client) { + collection := client.Database("test").Collection("user") + ctx := context.Background() + cursor, err := collection.Find(context.TODO(), bson.D{}) + if err != nil { + log.Fatal(err) + defer cursor.Close(ctx) + } else { + // iterate over docs using Next() + for cursor.Next(ctx) { + // Declare a result BSON object + var result newUser + err := cursor.Decode(&result) + if err != nil { + log.Fatal(err) + } else { + // log.Println(time.Now().Local().Day(), result.JoinDate.Day()) + if result.Introduction == false { + curHour := time.Now().Local().Hour() + curDay := time.Now().Local().Day() + User := fmt.Sprintf("[%v](tg://user?id=%v)", result.FirstName, result.UserID) + log.Println(curDay-result.JoinDate.Day(), curHour-result.JoinDate.Hour()) + if (curDay-result.JoinDate.Day() > 0 && curHour-result.JoinDate.Hour() > 0) || curDay-result.JoinDate.Day() > 1 { + go kickUser(result.UserID, ID) + collection.DeleteOne(context.TODO(), bson.M{"userid": result.UserID}) + reply := tbot.NewMessage(ID, User+" kicked. `Reason : No introduction within 24 hours of joining`") + reply.ParseMode = "markdown" + bot.Send(reply) + } else { + reply := tbot.NewMessage(ID, User+", Please introduce yourself in the next 12 hours or I will not be able to verify your presence and will have to kick you.") + reply.ParseMode = "markdown" + bot.Send(reply) + } + } + } + } + } +}