This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
80 lines (65 loc) · 2.25 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
)
func main() {
// Put Your Bot Token via ENV Vars
b, err := gotgbot.NewBot(
os.Getenv("BOT_TOKEN"),
&gotgbot.BotOpts{
Client: http.Client{},
},
)
if err != nil {
panic("failed to create new bot: " + err.Error())
}
// Create updater and dispatcher.
updater := ext.NewUpdater(nil)
dispatcher := updater.Dispatcher
// Handlers for runnning commands.
dispatcher.AddHandler(handlers.NewCommand("start", start))
dispatcher.AddHandler(handlers.NewCommand("run", run))
err = updater.StartPolling(b, &ext.PollingOpts{DropPendingUpdates: true})
if err != nil {
log.Fatalf("[Polling] Failed to start polling: %w\n", err)
} else {
log.Println("[Polling] Started Polling...!")
}
// log msg telling that bot has started
fmt.Printf("%s has been started...!\nMade with ❤️ by @DivideProjects\n", b.User.Username)
// Idle, to keep updates coming in, and avoid bot stopping.
updater.Idle()
}
func start(bot *gotgbot.Bot, ctx *ext.Context) error {
msg := ctx.EffectiveMessage
// To ensure bot does not reply outside of private chats
if ctx.EffectiveChat.Type != "private" {
return ext.EndGroups
}
user_name := ctx.EffectiveUser.FirstName
// Following string is replied to cmd user on /start
start_msg := "*Hi %v*,\n" +
"I am a Simple Telegram made using [Go](https://go.dev)*\n" +
"Brought to You with ❤️ By @DivideProjects"
// For Checking either user joined channel or not
_, _ = msg.Reply(bot, fmt.Sprintf(start_msg, user_name), &gotgbot.SendMessageOpts{ParseMode: "Markdown"})
return ext.EndGroups
}
func run(bot *gotgbot.Bot, ctx *ext.Context) error {
chat := ctx.EffectiveChat
msg := ctx.EffectiveMessage
// To ensure bot does not reply outside of private chats
if chat.Type != "private" {
msg.Reply(bot, "This command only works in private chats!", nil)
return ext.EndGroups
}
text := "This command does nothing, you can build your bot by looking my source code here:\nhttps://github.com/divkix/GoLangTgBot"
_, _ = msg.Reply(bot, text, &gotgbot.SendMessageOpts{ParseMode: "Markdown", DisableWebPagePreview: false})
return ext.EndGroups
}