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 c6d3af1..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" @@ -81,39 +82,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{ @@ -144,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) @@ -174,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() { @@ -220,6 +191,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")) } @@ -229,7 +202,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 new file mode 100644 index 0000000..223c142 --- /dev/null +++ b/telegram-bot/newUser.go @@ -0,0 +1,130 @@ +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" +) + +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, + "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?", + ) + + 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 + 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+" 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.")) + } + +} + +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) + } + } + } + } + } +}