forked from Reeshuxd/AutoApproverBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (83 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Approver Bot
// Copyright (C) 2021 Reeshuxd (@reeshuxd)
package main
import (
"fmt"
"net/http"
"os"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
)
func main() {
bot, err := gotgbot.NewBot(
os.Getenv("TOKEN"),
&gotgbot.BotOpts{
APIURL: "",
Client: http.Client{},
GetTimeout: gotgbot.DefaultGetTimeout,
PostTimeout: gotgbot.DefaultPostTimeout,
},
)
if err != nil {
fmt.Println("Failed to create bot:", err.Error())
}
updater := ext.NewUpdater(
&ext.UpdaterOpts{
ErrorLog: nil,
DispatcherOpts: ext.DispatcherOpts{
Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
fmt.Println("an error occurred while handling update:", err.Error())
return ext.DispatcherActionNoop
},
Panic: nil,
ErrorLog: nil,
MaxRoutines: 0,
},
})
dp := updater.Dispatcher
// Commands
dp.AddHandler(handlers.NewCommand("start", Start))
dp.AddHandler(handlers.NewChatJoinRequest(nil, Approve))
// Start Polling()
poll := updater.StartPolling(bot, &ext.PollingOpts{DropPendingUpdates: true})
if poll != nil {
fmt.Println("Failed to start bot:", poll.Error())
}
fmt.Printf("@%s has been sucesfully started\n💝Made by @ItsReeshu\n", bot.Username)
updater.Idle()
}
func Start(bot *gotgbot.Bot, ctx *ext.Context) error {
if ctx.EffectiveChat.Type != "private" {
return nil
}
user := ctx.EffectiveSender.User
text := `
<b>Hello <a href="tg://user?id=%v">%v</a></b>
I am a bot made for accepting newly coming join requests at the time they comes.
I am made with <a href="go.dev">golang</a> to give a better performance!
Bot made with 💝 by <a href="t.me/AboutReeshu">Reeshu</a> for you!
<b>Support Chat:</b> @UserChatRoom
`
ctx.EffectiveMessage.Reply(
bot,
fmt.Sprintf(text, user.Id, user.FirstName),
&gotgbot.SendMessageOpts{
ReplyMarkup: gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{{
{Text: "My Source Code", Url: "https://github.com/Reeshuxd/AutoApproverBot"},
}},
},
ParseMode: "html",
DisableWebPagePreview: true,
},
)
return nil
}
func Approve(bot *gotgbot.Bot, ctx *ext.Context) error {
_, err := bot.ApproveChatJoinRequest(ctx.EffectiveChat.Id, ctx.EffectiveSender.User.Id)
if err != nil {
fmt.Println("Error while approving:", err.Error())
}
return nil
}