Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented auto-kicking members if no introduction provided in 24 hours of joining the group #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions telegram-bot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
43 changes: 8 additions & 35 deletions telegram-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -220,6 +191,8 @@ func main() {
deletenote(ID, update.Message.Text, *client)
case "fetchnote":
fetchnote(ID, update.Message.Text, *client)
case "introduction":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case "introduction":
case "intro":

introverify(update.Message.From, ID, *client)
default:
bot.Send(tbot.NewMessage(ID, "I don't know that command"))
}
Expand All @@ -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)
}
}
}
Expand Down
130 changes: 130 additions & 0 deletions telegram-bot/newUser.go
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introverify?? 😅 Please write a better name for the function.

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."))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bot.Send(tbot.NewMessage(ID, "No need to introduce again, I already know you."))
bot.Send(tbot.NewMessage(ID, "Thanks but no need to introduce again, I already know you."))

}

}

func introkick(ID int64, client mongo.Client) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func introkick(ID int64, client mongo.Client) {
func noIntroKick(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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the stale comments

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the Day return the date? In that case, if curDay is 1 and JoinDate.Day() is 31. The result may be negative.

Try taking the mod of difference if that's the case.

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`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please make the deadline as 48 hours

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.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 := tbot.NewMessage(ID, User+", Hey, This is your friendly osdc-bot. Please introduce yourself on the OSDC group in the next 12 hours or I will not be able to verify your presence and will have to kick you. Just a simple introduction about your interests, experience will be enough.")

reply.ParseMode = "markdown"
bot.Send(reply)
}
}
}
}
}
}