Skip to content

Commit

Permalink
nil but ...
Browse files Browse the repository at this point in the history
* add how to deploy on VPS
* improve connect with Redis and MongoDB
* typo's
  • Loading branch information
AshokShau committed Aug 18, 2024
1 parent 06ae756 commit 328b69a
Show file tree
Hide file tree
Showing 26 changed files with 157 additions and 122 deletions.
59 changes: 41 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align='center'>
<a href="https://github.com/divideprojects/Alita_Robot/actions/workflows/ci.yml"><img src="https://github.com/divideprojects/Alita_Robot/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
<a href="https://github.com/divideprojects/Alita_Robot/actions/workflows/release.yml" alt="made-with-go"> <img src="https://github.com/divideprojects/Alita_Robot/actions/workflows/release.yml/badge.svg" alt="Release"/> </a>
<a href="https://github.com/divideprojects/Alita_Robot/actions/workflows/release.yml"> <img src="https://github.com/divideprojects/Alita_Robot/actions/workflows/release.yml/badge.svg" alt="Release"/> </a>
</p>


Expand All @@ -17,30 +17,29 @@
</p>

<p align='center'>
<a href="https://go.dev/" alt="made-with-go"> <img src="https://img.shields.io/badge/Made%20with-Go-1f425f.svg?style=flat-square&logo=Go&color=blue" /> </a>
<a href="https://go.dev/"> <img src="https://img.shields.io/badge/Made%20with-Go-1f425f.svg?style=flat-square&logo=Go&color=blue" alt="made-with-go"/> </a>
<a href="https://deepsource.io/gh/DivideProjects/Alita_Robot/?ref=repository-badge"><img src="https://static.deepsource.io/deepsource-badge-light-mini.svg" alt="DeepSource"></a>
<a href="https://makeapullrequest.com" alt="PRs Welcome"> <img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" /> </a>
<a href="https://makeapullrequest.com"> <img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" alt="PRs Welcome"/> </a>
</p>

Alita is a Telegram Group managment bot made using **[Gotgbot](https://github.com/PaulSonOfLars/gotgbot)** and **[Go](https://go.dev)**, which makes it modern and faster than most of the exisitng Telegram Chat Managers.
Alita is a Telegram Group Managment bot made using **[Gotgbot](https://github.com/PaulSonOfLars/gotgbot)** and **[Go](https://go.dev)**, which makes it modern and faster than most of the exisitng Telegram Chat Managers.

**Alita's features over other bots:**
- Modern
- Fast
- Fully asynchronous
- Fully open-source
- Frequently updated
- Multi Language Support

Can be found on Telegram as [@Alita_Robot](https://t.me/Alita_Robot)
</br>

Alita is currently available in 1 Language as of now:
Alita is currently available in one Language as of now:
- **US English**

More languages can be managed in the _locales_ folder. Although we have added support for languages in bot, we still need contributors to translate the bot to other languages.

More langauges have been added to locales folder but need some configuration.
More languages have been added to the locales folder but need some configuration.

Help us bring more languages to the bot by contributing to the project on [Crowdin](https://crowdin.com/project/alitarobot)

Expand All @@ -50,34 +49,58 @@ Help us bring more languages to the bot by contributing to the project on [Crowd
- Linux/Windows machine (Ubuntu/Debain-based OS Preferred or Windows 10/Server2016 and later)

or
- Docker (Easiest way to setup the bot)
- Docker (Easiest way to set up the bot)

## How to setup

First Step!
- Star **** the repository!!
- Fork **🔱** the repository before deploying and make changes in the fork.
- Fork **🔱** the repository before deploying and making changes in the fork.

It really motivates me to continue this project further.

### Deploy to Heroku
- Get your `API_ID` and `API_HASH` from [here](https://my.telegram.org/)
### Deploy to Heroku:
- Get your Bot Token from [@BotFather](https://t.me/BotFather)

**Note:** As it is banned with an unknown reason in heroku, follow the below steps carefully!
**Note:** As it is banned for an unknown reason in heroku, follow the below steps carefully!
- First Fork this repo `:)`
- secondly, create an empty app in the Heroku, with a custom app name; and select Europe region for faster speed.
- Now go to the deploy tab of your app and under deployment method select GitHub and connect it.
- after that, type the forked repo name and select it now select main branch and enable auto deploy, at last click on deploy now button!
</h3>
<p align="center"><a href="https://heroku.com/deploy?template=https://github.com/divideprojects/Alita_Robot"> <img src="https://img.shields.io/badge/Redirect%20To%20Heroku-black?style=for-the-badge&logo=heroku" width="200" height="35.45"/></a></p>
- Now go to the deploy tab of your app and under deployment method, select GitHub and connect it.
- after that, type the forked repo name and select it now select the main branch and enable auto deploy, at last click on deploy now button!

<p align="center"><a href="https://heroku.com/deploy?template=https://github.com/divideprojects/Alita_Robot"> <img src="https://img.shields.io/badge/Redirect%20To%20Heroku-black?style=for-the-badge&logo=heroku" width="200" height="35.45" alt="Heroku-Deploy"/></a></p>

- Lastly, you must fill up all the vars in heroku (if using heroku) as directed in app.json file and now turn on the worker dyno to run it `:)`

**Must Add in heroku [config](https://dashboard.heroku.com/apps/yourappname/settings) :**
- `DB_URI` : Get one from [mongodb](https://cloud.mongodb.com/)
- `OWNER_ID` : your telegram id
- `MESSAGE_DUMP` : must be start with `-100` or your bot added in as **admin**

### Deploy to VPS:
#### 1. Install Go
Follow the instructions to install Go on your system: [Go Installation Guide](https://golang.org/doc/install)

#### 2. Clone the repository
```bash
git clone https://github.com/divideprojects/Alita_Robot.git
cd Alita_Robot
```

#### 3. Set up the environment
Copy the sample environment file and edit it as needed:
```shell
cp sample.env .env
vi .env
```

#### 4. Build and run the bot
```bash
go build -o alita_robot main.go
./alita_robot
```


### Traditional

- Download a binary from [releases](https://github.com/divideprojects/Alita_Robot/releases) page specific to your PC.
Expand All @@ -91,7 +114,7 @@ We currently publish a `ghcr.io/divideprojects/alita_robot` Docker image based o

Set the Environmental vars using the `-e` flag in while running the container.

If all works well, bot should send message to the **MESSAGE_DUMP** Group!
If all works well, bot should send a message to the **MESSAGE_DUMP** Group!

### Other

Expand Down Expand Up @@ -130,5 +153,5 @@ gpg --verify alita_robot_2.0.1_darwin_amd64.tar.gz.sig alita_robot_2.0.1_darwin_

### Copyright & License

* Copyright (C) 2020-2023 by [Divkix](https://divkix.me) ❤️️
* Copyright © 2020–2024 by [Divkix](https://divkix.me) ❤️️
* Licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 29 June 2007](https://github.com/divideprojects/Alita_Robot/blob/master/LICENSE)
26 changes: 8 additions & 18 deletions alita/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ var (
BotToken string
DatabaseURI string
MainDbName string
BotVersion string = "2.1.3"
BotVersion = "2.1.3"
ApiServer string
WorkingMode = "worker"
Debug = false
DropPendingUpdates = true
OwnerId int64
MessageDump int64
RedisAddress string
RedisPassword string
RedisDB int
RedisURI string
)

// init initializes the config variables.
Expand All @@ -43,10 +41,10 @@ func init() {
},
)

// load goenv config
godotenv.Load()
// load dotenv config
_ = godotenv.Load()

// set necessary variables
// set the necessary variables
Debug = typeConvertor{str: os.Getenv("DEBUG")}.Bool()
DropPendingUpdates = typeConvertor{str: os.Getenv("DROP_PENDING_UPDATES")}.Bool()
DatabaseURI = os.Getenv("DB_URI")
Expand Down Expand Up @@ -93,16 +91,8 @@ func init() {
}

// redis config
RedisAddress = os.Getenv("REDIS_ADDRESS")
if os.Getenv("REDIS_ADDRESS") == "" {
RedisAddress = "localhost:6379"
}
RedisPassword = os.Getenv("REDIS_PASSWORD")
if os.Getenv("REDIS_PASSWORD") == "" {
RedisPassword = ""
}
RedisDB = typeConvertor{str: os.Getenv("REDIS_DB")}.Int()
if os.Getenv("REDIS_DB") == "" {
RedisDB = 0
RedisURI = os.Getenv("REDIS_URI")
if RedisURI == "" {
RedisURI = "redis://localhost:6379"
}
}
4 changes: 2 additions & 2 deletions alita/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func (t typeConvertor) StringArray() []string {
return allUpdates
}

// IntArray will return an int array from a comma separated string
// Int Array will return an int array from a comma separated string
func (t typeConvertor) Int() int {
val, _ := strconv.Atoi(t.str)
return val
}

// Int64Array will return an int64 array from a comma separated string
// Int64 Array will return an int64 array from a comma separated string
func (t typeConvertor) Int64() int64 {
val, _ := strconv.ParseInt(t.str, 10, 64)
return val
Expand Down
3 changes: 2 additions & 1 deletion alita/db/admin_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"errors"
log "github.com/sirupsen/logrus"

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -23,7 +24,7 @@ func checkAdminSetting(chatID int64) (adminSrc *AdminSettings) {
defaultAdminSrc := &AdminSettings{ChatId: chatID, AnonAdmin: false}

err := findOne(adminSettingsColl, bson.M{"_id": chatID}).Decode(&adminSrc)
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
adminSrc = defaultAdminSrc
err := updateOne(adminSettingsColl, bson.M{"_id": chatID}, defaultAdminSrc)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion alita/db/antiflood_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"errors"
log "github.com/sirupsen/logrus"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -28,7 +29,7 @@ func checkFloodSetting(chatID int64) (floodSrc *FloodSettings) {
defaultFloodSrc := &FloodSettings{ChatId: chatID, Limit: 0, Mode: defaultFloodsettingsMode}

err := findOne(antifloodSettingsColl, bson.M{"_id": chatID}).Decode(&floodSrc)
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
floodSrc = defaultFloodSrc
err := updateOne(antifloodSettingsColl, bson.M{"_id": chatID}, defaultFloodSrc)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion alita/db/blacklists_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"errors"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -26,7 +27,7 @@ func checkBlacklistSetting(chatID int64) (blSrc *BlacklistSettings) {
Reason: "Automated Blacklisted word %s",
}
errS := findOne(blacklistsColl, bson.M{"_id": chatID}).Decode(&blSrc)
if errS == mongo.ErrNoDocuments {
if errors.Is(errS, mongo.ErrNoDocuments) {
blSrc = defaultBlacklistSrc
err := updateOne(blacklistsColl, bson.M{"_id": chatID}, defaultBlacklistSrc)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions alita/db/channels_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"errors"
log "github.com/sirupsen/logrus"

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -15,7 +16,7 @@ type Channel struct {

func GetChannelSettings(channelId int64) (channelSrc *Channel) {
err := findOne(channelColl, bson.M{"_id": channelId}).Decode(&channelSrc)
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
channelSrc = nil
} else if err != nil {
log.Errorf("[Database] getChannelSettings: %v - %d ", err, channelId)
Expand Down Expand Up @@ -52,7 +53,7 @@ func UpdateChannel(channelId int64, channelName, username string) {
func GetChannelIdByUserName(username string) int64 {
var cuids *Channel
err := findOne(channelColl, bson.M{"username": username}).Decode(&cuids)
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
return 0
} else if err != nil {
log.Errorf("[Database] GetChannelByUserName: %v - %d", err, cuids.ChannelId)
Expand Down
3 changes: 2 additions & 1 deletion alita/db/chats_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"errors"
log "github.com/sirupsen/logrus"

"github.com/divideprojects/Alita_Robot/alita/utils/string_handling"
Expand All @@ -19,7 +20,7 @@ type Chat struct {

func GetChatSettings(chatId int64) (chatSrc *Chat) {
err := findOne(chatColl, bson.M{"_id": chatId}).Decode(&chatSrc)
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
chatSrc = &Chat{}
} else if err != nil {
log.Errorf("[Database] getChatSettings: %v - %d ", err, chatId)
Expand Down
5 changes: 3 additions & 2 deletions alita/db/connections_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"errors"
log "github.com/sirupsen/logrus"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -30,7 +31,7 @@ func ToggleAllowConnect(chatID int64, pref bool) {
func GetChatConnectionSetting(chatID int64) (connectionSrc *ConnectionSettings) {
defaultConnectionSrc := &ConnectionSettings{ChatId: chatID, AllowConnect: false}
errF := findOne(connectionSettingsColl, bson.M{"_id": chatID}).Decode(&connectionSrc)
if errF == mongo.ErrNoDocuments {
if errors.Is(errF, mongo.ErrNoDocuments) {
connectionSrc = defaultConnectionSrc
err := updateOne(connectionSettingsColl, bson.M{"_id": chatID}, connectionSrc)
if err != nil {
Expand All @@ -46,7 +47,7 @@ func GetChatConnectionSetting(chatID int64) (connectionSrc *ConnectionSettings)
func getUserConnectionSetting(userID int64) (connectionSrc *Connections) {
defaultConnectionSrc := &Connections{UserId: userID, Connected: false}
errF := findOne(connectionColl, bson.M{"_id": userID}).Decode(&connectionSrc)
if errF == mongo.ErrNoDocuments {
if errors.Is(errF, mongo.ErrNoDocuments) {
connectionSrc = defaultConnectionSrc
err := updateOne(connectionColl, bson.M{"_id": userID}, connectionSrc)
if err != nil {
Expand Down
62 changes: 32 additions & 30 deletions alita/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,48 @@ var (
notesSettingsColl *mongo.Collection
)

// dbInstance func
// dbInstance initializes the MongoDB client and collections
func init() {
mongoClient, err := mongo.NewClient(
options.Client().ApplyURI(config.DatabaseURI),
)
// Create a new MongoDB client
mongoClient, err := mongo.NewClient(options.Client().ApplyURI(config.DatabaseURI))
if err != nil {
log.Errorf("[Database][Client]: %v", err)
log.Fatalf("[Database][Client]: %v", err)
}

ctx, cancel := context.WithTimeout(bgCtx, 10*time.Second)
defer cancel()

err = mongoClient.Connect(ctx)
if err != nil {
log.Errorf("[Database][Connect]: %v", err)
if err = mongoClient.Connect(ctx); err != nil {
log.Fatalf("[Database][Connect]: %v", err)
}

// Open Connections to Collections
// Get the database reference
db := mongoClient.Database(config.MainDbName)

// Initialize collections
log.Info("Opening Database Collections...")
adminSettingsColl = mongoClient.Database(config.MainDbName).Collection("admin")
blacklistsColl = mongoClient.Database(config.MainDbName).Collection("blacklists")
pinColl = mongoClient.Database(config.MainDbName).Collection("pins")
userColl = mongoClient.Database(config.MainDbName).Collection("users")
reportChatColl = mongoClient.Database(config.MainDbName).Collection("report_chat_settings")
reportUserColl = mongoClient.Database(config.MainDbName).Collection("report_user_settings")
devsColl = mongoClient.Database(config.MainDbName).Collection("devs")
chatColl = mongoClient.Database(config.MainDbName).Collection("chats")
channelColl = mongoClient.Database(config.MainDbName).Collection("channels")
antifloodSettingsColl = mongoClient.Database(config.MainDbName).Collection("antiflood_settings")
connectionColl = mongoClient.Database(config.MainDbName).Collection("connection")
connectionSettingsColl = mongoClient.Database(config.MainDbName).Collection("connection_settings")
disableColl = mongoClient.Database(config.MainDbName).Collection("disable")
rulesColl = mongoClient.Database(config.MainDbName).Collection("rules")
warnSettingsColl = mongoClient.Database(config.MainDbName).Collection("warns_settings")
warnUsersColl = mongoClient.Database(config.MainDbName).Collection("warns_users")
greetingsColl = mongoClient.Database(config.MainDbName).Collection("greetings")
lockColl = mongoClient.Database(config.MainDbName).Collection("locks")
filterColl = mongoClient.Database(config.MainDbName).Collection("filters")
notesColl = mongoClient.Database(config.MainDbName).Collection("notes")
notesSettingsColl = mongoClient.Database(config.MainDbName).Collection("notes_settings")
adminSettingsColl = db.Collection("admin")
blacklistsColl = db.Collection("blacklists")
pinColl = db.Collection("pins")
userColl = db.Collection("users")
reportChatColl = db.Collection("report_chat_settings")
reportUserColl = db.Collection("report_user_settings")
devsColl = db.Collection("devs")
chatColl = db.Collection("chats")
channelColl = db.Collection("channels")
antifloodSettingsColl = db.Collection("antiflood_settings")
connectionColl = db.Collection("connection")
connectionSettingsColl = db.Collection("connection_settings")
disableColl = db.Collection("disable")
rulesColl = db.Collection("rules")
warnSettingsColl = db.Collection("warns_settings")
warnUsersColl = db.Collection("warns_users")
greetingsColl = db.Collection("greetings")
lockColl = db.Collection("locks")
filterColl = db.Collection("filters")
notesColl = db.Collection("notes")
notesSettingsColl = db.Collection("notes_settings")

log.Info("Done opening all database collections!")
}

Expand Down
Loading

0 comments on commit 328b69a

Please sign in to comment.