Skip to content

Commit

Permalink
don't reply
Browse files Browse the repository at this point in the history
  • Loading branch information
DeekshithSH committed Jul 15, 2024
1 parent 97e4029 commit f5d1854
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 143 deletions.
34 changes: 14 additions & 20 deletions internal/commands/start.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
package commands

import (
"EverythingSuckz/fsb/config"
"EverythingSuckz/fsb/internal/utils"

"github.com/celestix/gotgproto/dispatcher"
"github.com/celestix/gotgproto/dispatcher/handlers"
"github.com/celestix/gotgproto/ext"
"github.com/celestix/gotgproto/storage"
)

func (m *command) LoadStart(dispatcher dispatcher.Dispatcher) {
log := m.log.Named("start")
defer log.Sugar().Info("Loaded")
dispatcher.AddHandler(handlers.NewCommand("start", start))
// dispatcher.AddHandler(handlers.NewCommand("start", start))
}

func start(ctx *ext.Context, u *ext.Update) error {
chatId := u.EffectiveChat().GetID()
peerChatId := ctx.PeerStorage.GetPeerById(chatId)
if peerChatId.Type != int(storage.TypeUser) {
return dispatcher.EndGroups
}
if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
ctx.Reply(u, "You are not allowed to use this bot.", nil)
return dispatcher.EndGroups
}
ctx.Reply(u, "Hi, send me any file to get a direct streamble link to that file.", nil)
return dispatcher.EndGroups
}
// func start(ctx *ext.Context, u *ext.Update) error {
// chatId := u.EffectiveChat().GetID()
// peerChatId := ctx.PeerStorage.GetPeerById(chatId)
// if peerChatId.Type != int(storage.TypeUser) {
// return dispatcher.EndGroups
// }
// if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
// ctx.Reply(u, "You are not allowed to use this bot.", nil)
// return dispatcher.EndGroups
// }
// ctx.Reply(u, "Hi, send me any file to get a direct streamble link to that file.", nil)
// return dispatcher.EndGroups
// }
233 changes: 110 additions & 123 deletions internal/commands/stream.go
Original file line number Diff line number Diff line change
@@ -1,135 +1,122 @@
package commands

import (
"fmt"
"net/url"
"strings"

"EverythingSuckz/fsb/config"
"EverythingSuckz/fsb/internal/utils"

"github.com/celestix/gotgproto/dispatcher"
"github.com/celestix/gotgproto/dispatcher/handlers"
"github.com/celestix/gotgproto/ext"
"github.com/celestix/gotgproto/storage"
"github.com/celestix/gotgproto/types"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
)

func (m *command) LoadStream(dispatcher dispatcher.Dispatcher) {
log := m.log.Named("start")
defer log.Sugar().Info("Loaded")
dispatcher.AddHandler(
handlers.NewMessage(nil, sendLink),
)
// dispatcher.AddHandler(
// handlers.NewMessage(nil, sendLink),
// )
}

func supportedMediaFilter(m *types.Message) (bool, error) {
if not := m.Media == nil; not {
return false, dispatcher.EndGroups
}
switch m.Media.(type) {
case *tg.MessageMediaDocument:
return true, nil
case *tg.MessageMediaPhoto:
return false, nil
case tg.MessageMediaClass:
return false, dispatcher.EndGroups
default:
return false, nil
}
}
// func supportedMediaFilter(m *types.Message) (bool, error) {
// if not := m.Media == nil; not {
// return false, dispatcher.EndGroups
// }
// switch m.Media.(type) {
// case *tg.MessageMediaDocument:
// return true, nil
// case *tg.MessageMediaPhoto:
// return false, nil
// case tg.MessageMediaClass:
// return false, dispatcher.EndGroups
// default:
// return false, nil
// }
// }

func sendLink(ctx *ext.Context, u *ext.Update) error {
chatId := u.EffectiveChat().GetID()
peerChatId := ctx.PeerStorage.GetPeerById(chatId)
if peerChatId.Type != int(storage.TypeUser) {
return dispatcher.EndGroups
}
if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
ctx.Reply(u, "You are not allowed to use this bot.", nil)
return dispatcher.EndGroups
}
supported, err := supportedMediaFilter(u.EffectiveMessage)
if err != nil {
return err
}
if !supported {
ctx.Reply(u, "Sorry, this message type is unsupported.", nil)
return dispatcher.EndGroups
}
update, err := utils.ForwardMessages(ctx, chatId, config.ValueOf.LogChannelID, u.EffectiveMessage.ID)
if err != nil {
utils.Logger.Sugar().Error(err)
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
return dispatcher.EndGroups
}
messageID := update.Updates[0].(*tg.UpdateMessageID).ID
if err != nil {
utils.Logger.Sugar().Error(err)
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
return dispatcher.EndGroups
}
doc := update.Updates[1].(*tg.UpdateNewChannelMessage).Message.(*tg.Message).Media
file, err := utils.FileFromMedia(doc)
if err != nil {
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
return dispatcher.EndGroups
}
fullHash := utils.PackFile(
file.FileName,
file.FileSize,
file.MimeType,
file.ID,
)
hash := utils.GetShortHash(fullHash)
link := fmt.Sprintf("%s/stream/%d?hash=%s", config.ValueOf.Host, messageID, hash)
text := []styling.StyledTextOption{styling.Code(link)}
markup := &tg.ReplyInlineMarkup{
Rows: []tg.KeyboardButtonRow{{
Buttons: []tg.KeyboardButtonClass{
&tg.KeyboardButtonURL{
Text: "Download",
URL: link + "&d=true",
},
},
}},
}
if strings.Contains(file.MimeType, "video") || strings.Contains(file.MimeType, "audio") || strings.Contains(file.MimeType, "pdf") {
markup.Rows[0].Buttons = append(markup.Rows[0].Buttons, &tg.KeyboardButtonURL{
Text: "Stream",
URL: link,
})
markup.Rows = append(markup.Rows, tg.KeyboardButtonRow{
Buttons: []tg.KeyboardButtonClass{
&tg.KeyboardButtonURL{
Text: "VLC",
URL: fmt.Sprintf("%s/player/%d?hash=%s&player=%s&name=%s", config.ValueOf.Host, messageID, hash, "vlc", url.QueryEscape(file.FileName)),
},
&tg.KeyboardButtonURL{
Text: "MX Player",
URL: fmt.Sprintf("%s/player/%d?hash=%s&player=%s&name=%s", config.ValueOf.Host, messageID, hash, "mxplayer", url.QueryEscape(file.FileName)),
},
},
})
}
fmt.Println(markup)
if strings.Contains(link, "http://localhost") {
_, err = ctx.Reply(u, text, &ext.ReplyOpts{
NoWebpage: false,
ReplyToMessageId: u.EffectiveMessage.ID,
})
} else {
_, err = ctx.Reply(u, text, &ext.ReplyOpts{
Markup: markup,
NoWebpage: false,
ReplyToMessageId: u.EffectiveMessage.ID,
})
}
if err != nil {
utils.Logger.Sugar().Error(err)
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
}
return dispatcher.EndGroups
}
// func sendLink(ctx *ext.Context, u *ext.Update) error {
// chatId := u.EffectiveChat().GetID()
// peerChatId := ctx.PeerStorage.GetPeerById(chatId)
// if peerChatId.Type != int(storage.TypeUser) {
// return dispatcher.EndGroups
// }
// if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
// ctx.Reply(u, "You are not allowed to use this bot.", nil)
// return dispatcher.EndGroups
// }
// supported, err := supportedMediaFilter(u.EffectiveMessage)
// if err != nil {
// return err
// }
// if !supported {
// ctx.Reply(u, "Sorry, this message type is unsupported.", nil)
// return dispatcher.EndGroups
// }
// update, err := utils.ForwardMessages(ctx, chatId, config.ValueOf.LogChannelID, u.EffectiveMessage.ID)
// if err != nil {
// utils.Logger.Sugar().Error(err)
// ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
// return dispatcher.EndGroups
// }
// messageID := update.Updates[0].(*tg.UpdateMessageID).ID
// if err != nil {
// utils.Logger.Sugar().Error(err)
// ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
// return dispatcher.EndGroups
// }
// doc := update.Updates[1].(*tg.UpdateNewChannelMessage).Message.(*tg.Message).Media
// file, err := utils.FileFromMedia(doc)
// if err != nil {
// ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
// return dispatcher.EndGroups
// }
// fullHash := utils.PackFile(
// file.FileName,
// file.FileSize,
// file.MimeType,
// file.ID,
// )
// hash := utils.GetShortHash(fullHash)
// link := fmt.Sprintf("%s/stream/%d?hash=%s", config.ValueOf.Host, messageID, hash)
// text := []styling.StyledTextOption{styling.Code(link)}
// markup := &tg.ReplyInlineMarkup{
// Rows: []tg.KeyboardButtonRow{{
// Buttons: []tg.KeyboardButtonClass{
// &tg.KeyboardButtonURL{
// Text: "Download",
// URL: link + "&d=true",
// },
// },
// }},
// }
// if strings.Contains(file.MimeType, "video") || strings.Contains(file.MimeType, "audio") || strings.Contains(file.MimeType, "pdf") {
// markup.Rows[0].Buttons = append(markup.Rows[0].Buttons, &tg.KeyboardButtonURL{
// Text: "Stream",
// URL: link,
// })
// markup.Rows = append(markup.Rows, tg.KeyboardButtonRow{
// Buttons: []tg.KeyboardButtonClass{
// &tg.KeyboardButtonURL{
// Text: "VLC",
// URL: fmt.Sprintf("%s/player/%d?hash=%s&player=%s&name=%s", config.ValueOf.Host, messageID, hash, "vlc", url.QueryEscape(file.FileName)),
// },
// &tg.KeyboardButtonURL{
// Text: "MX Player",
// URL: fmt.Sprintf("%s/player/%d?hash=%s&player=%s&name=%s", config.ValueOf.Host, messageID, hash, "mxplayer", url.QueryEscape(file.FileName)),
// },
// },
// })
// }
// fmt.Println(markup)
// if strings.Contains(link, "http://localhost") {
// _, err = ctx.Reply(u, text, &ext.ReplyOpts{
// NoWebpage: false,
// ReplyToMessageId: u.EffectiveMessage.ID,
// })
// } else {
// _, err = ctx.Reply(u, text, &ext.ReplyOpts{
// Markup: markup,
// NoWebpage: false,
// ReplyToMessageId: u.EffectiveMessage.ID,
// })
// }
// if err != nil {
// utils.Logger.Sugar().Error(err)
// ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
// }
// return dispatcher.EndGroups
// }
50 changes: 50 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"EverythingSuckz/fsb/internal/types"
"crypto/md5"
"encoding/hex"
"fmt"
"reflect"
"strconv"
)

type HashableFileStruct struct {
FileName string
FileSize int64
MimeType string
FileID int64
}

func (f *HashableFileStruct) Pack() string {
hasher := md5.New()
val := reflect.ValueOf(*f)
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)

var fieldValue []byte
switch field.Kind() {
case reflect.String:
fieldValue = []byte(field.String())
case reflect.Int64:
fieldValue = []byte(strconv.FormatInt(field.Int(), 10))
}

hasher.Write(fieldValue)
}
return hex.EncodeToString(hasher.Sum(nil))
}

func PackFile(fileName string, fileSize int64, mimeType string, fileID int64) string {
return (&types.HashableFileStruct{FileName: fileName, FileSize: fileSize, MimeType: mimeType, FileID: fileID}).Pack()
}

func main() {
fullhash := PackFile(
"photo_2023-11-28_20-31-55.jpg",
95244,
"image/jpeg",
6240121537265930958,
)
fmt.Println(fullhash)
}
24 changes: 24 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import hashlib

class HashableFileStruct:
def __init__(self, fileName, fileSize, mimeType, fileID):
self.fileName = fileName
self.fileSize = fileSize
self.mimeType = mimeType
self.fileID = fileID

def pack(self):
hasher = hashlib.md5()
for field in [self.fileName, str(self.fileSize), self.mimeType, str(self.fileID)]:
hasher.update(field.encode())
return hasher.hexdigest()

def pack_file(fileName, fileSize, mimeType, fileID):
return HashableFileStruct(fileName, fileSize, mimeType, fileID).pack()

print(pack_file(
"",
12863576,
"video/mp4",
6066524942051052953,
)[:6])

0 comments on commit f5d1854

Please sign in to comment.