Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AshokShau committed Jul 22, 2024
2 parents bedec8f + 9452869 commit e35dff5
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 13 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: build

on: [push,pull_request]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.1'

- name: Build
run: go build -v ./...
11 changes: 11 additions & 0 deletions FallenSub/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ func FindInInt64Slice(slice []int64, val int64) bool {
return false
}

// FindInStringSlice Find takes a slice and looks for an element in it. If found it will
// return true, otherwise it will return a bool of false.
func FindInStringSlice(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

// RemoveFromInt64Slice Find takes a slice and looks for an element in it. If found it will
func RemoveFromInt64Slice(s []int64, r int64) []int64 {
for i, v := range s {
Expand Down
16 changes: 16 additions & 0 deletions FallenSub/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dispatcher

import (
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)

var Dispatcher = NewDispatcher()

// NewDispatcher creates a new dispatcher and loads modules.
func NewDispatcher() *ext.Dispatcher {
dispatcher := ext.NewDispatcher(nil)
modules.LoadModules(dispatcher)

return dispatcher
}
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Force Sub Bot Documentation</title>
</head>

<body>

<h1>Force Sub Bot</h1>
# Force Sub Bot
[![Go Build](https://github.com/Abishnoi69/Force-Sub-Bot/workflows/build/badge.svg)](https://github.com/Abishnoi69/Force-Sub-Bot/actions?query=workflow%3Abuild+event%3Apush+branch%3Amain)
<p>This project includes a Telegram bot designed to enforce subscription to a specific channel before allowing users to interact in a group chat. It's built using <a href="https://go.dev">Go</a> and integrates with the Telegram Bot API using <a href="https://github.com/PaulSonOfLars/gotgbot">gotgbot</a>.</p>

<section>
Expand Down
73 changes: 73 additions & 0 deletions api/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package api

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/dispatcher"
"github.com/PaulSonOfLars/gotgbot/v2"
)

var (
allowedTokens = strings.Split(os.Getenv("BOT_TOKEN"), " ")
lenAllowedTokens = len(allowedTokens)
)

const (
statusCodeSuccess = 200
)

// Bot Handles all incoming traffic from webhooks.
func Bot(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path

split := strings.Split(url, "/")
if len(split) < 2 {
fmt.Println(w, "url path too short")
w.WriteHeader(statusCodeSuccess)

return
}

botToken := split[len(split)-2]

bot, _ := gotgbot.NewBot(botToken, &gotgbot.BotOpts{DisableTokenCheck: true})

// Delete the webhook in case token is unauthorized.
if lenAllowedTokens > 0 && allowedTokens[0] != "" && !config.FindInStringSlice(allowedTokens, botToken) {
_, _ = bot.DeleteWebhook(&gotgbot.DeleteWebhookOpts{}) // It doesn't matter if it errors
w.WriteHeader(statusCodeSuccess)
return
}

var update gotgbot.Update

body, err := io.ReadAll(r.Body)
if err != nil {
_, err = fmt.Fprintf(w, "Error reading request body: %v", err)
w.WriteHeader(statusCodeSuccess)
return
}

err = json.Unmarshal(body, &update)
if err != nil {
fmt.Println("failed to unmarshal body ", err)
w.WriteHeader(statusCodeSuccess)

return
}

bot.Username = split[len(split)-1]

err = dispatcher.Dispatcher.ProcessUpdate(bot, &update, map[string]any{})
if err != nil {
fmt.Printf("error while processing update: %v", err)
}

w.WriteHeader(statusCodeSuccess)
}
10 changes: 4 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"time"

"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/dispatcher"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"time"
)

func main() {
Expand All @@ -14,10 +15,7 @@ func main() {
config.ErrorLog.Fatal("failed to create new bot:", err)
}

dispatcher := ext.NewDispatcher(nil)
modules.LoadModules(dispatcher)

updater := ext.NewUpdater(dispatcher, nil)
updater := ext.NewUpdater(dispatcher.Dispatcher, nil)
err = updater.StartPolling(b, &ext.PollingOpts{
DropPendingUpdates: true,
GetUpdatesOpts: &gotgbot.GetUpdatesOpts{
Expand Down
Loading

0 comments on commit e35dff5

Please sign in to comment.