Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nathenapse committed May 15, 2018
1 parent 4c119da commit 2b48a93
Show file tree
Hide file tree
Showing 16 changed files with 853 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TELEGRAM_TOKEN=<Telegram bot token at @BotFather>
TELEGRAM_USER_ID=<Telegram user id @getidsbot>
# DEFAULT_DOWNLOADER put the full command
# example
# DEFAULT_DOWNLOADER=curl -O
DEFAULT_DOWNLOADER=wget --content-disposition
# Username of the user u want to run command on as
RUNAS=<username of the pc>
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
vendor/
.realize.yaml
build/
.DS_Store
44 changes: 44 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# telegram-commander
self deployed telegram bot that will help with running simple commands
Self deployed telegram bot that will help with running simple commands



- [DESCRIPTION](#description)
- [INSTALLATION](#installation)
- [DEVELOPMENT](#development)
- [TODO](#todo)
- [LIMITATIONS](#limitations)
- [LICENSE](#license)

## DESCRIPTION

**telegram-commander** is a self deployed telegram bot that let's you run commands on a remote pc through [telegram](https://telegram.org). There are default commands that will help you run simple commands like Upload a file to pc, Download a file from pc, Download file from internet and Run simple commands and more to come.

## REQUIREMENTS

- GoLang > 1.9.1
- One of terminal file downloaders wget, curl, axel


## INSTALLATION

- Download the latest release binary zip
- unzip the file
- inside there is a .env.example file
- To install it first clone the project on your pc
- Create a telegram bot using [@BotFather](https://t.me/botfather)
- Set the name of the bot
- Set the username of the bot
- Get your telegram user id using [@IDBot](https://t.me/myidbot)
- Copy .env.example to .env
```bash
cp .env.example .env
```
- Set your bot and telegram user id on .env
```YAML
TELEGRAM_TOKEN=<Telegram bot token at @BotFather>
TELEGRAM_USER_ID=<Telegram user id @getidsbot>
# DEFAULT_DOWNLOADER put the full command
# example
# DEFAULT_DOWNLOADER=curl -O
DEFAULT_DOWNLOADER=wget --content-disposition
# Username of the user u want to run command on as
RUNAS=<username of the pc>
```
- Run the executable
```bash
./telegram-commander
```
- If you want to run it as a service
- First install the service
```bash
sudo ./telegram-commander -service install
```
- you can use your systems service manager to handle the service
```bash
sudo service telegram-commander start
sudo service telegram-commander stop
```

## DEVELOPMENT
- get the package
```bash
go get github.com/nathenapse/telegram-commander
```

## TODO

- [ ] Torrent Downloads
- [ ] Youtube Downloads
- [ ] Better Documentation

## LIMITATIONS

- Works on linux and mac

## LICENSE

MIT
35 changes: 35 additions & 0 deletions command/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package command

import (
"os/exec"
"os/user"
"syscall"

"github.com/nathenapse/telegram-commander/config"
tb "gopkg.in/tucnak/telebot.v2"
)

// Base is where all structs inherit
type Base struct {
}

// Validate Default implementation of Validate
func (b *Base) Validate(m *tb.Message) bool {
return m.Payload != ""
}

// SetUser is to set the user for the command
func SetUser(cmd *exec.Cmd, conf *config.Config) *exec.Cmd {
// TODO: handle error
usr, _ := user.Current()

if usr.Uid == "0" {
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: conf.Userid, Gid: conf.Groupid}
cmd.Dir = conf.User.HomeDir
} else {
cmd.Dir = usr.HomeDir
}

return cmd
}
50 changes: 50 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package command

import (
"bytes"
"os/exec"

"github.com/nathenapse/telegram-commander/config"
"github.com/nathenapse/telegram-commander/custom"
tb "gopkg.in/tucnak/telebot.v2"
)

// Command the basic command
type Command struct {
*Base
}

// Exec executes the command
func (c *Command) Exec(b *tb.Bot, m *tb.Message, conf *config.Config) {

cmd := exec.Command("sh", "-c", m.Text)

var out bytes.Buffer
var errb bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errb
cmd = SetUser(cmd, conf)

err := cmd.Run()

if err != nil {
custom.SendLong(b)(m.Sender, err.Error()+":"+errb.String(), &tb.SendOptions{
ReplyTo: m,
})
} else {
custom.SendLong(b)(m.Sender, out.String(), &tb.SendOptions{
ReplyTo: m,
})
}

}

// IsInline return if command is /command somethingelse
func (c *Command) IsInline() bool {
return false
}

// GetExample return the how to use of this command
func (c *Command) GetExample() string {
return "Just use it inline"
}
43 changes: 43 additions & 0 deletions command/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package command

import (
"os"

"github.com/nathenapse/telegram-commander/config"
"github.com/nathenapse/telegram-commander/custom"
tb "gopkg.in/tucnak/telebot.v2"
)

// Download to download files
type Download struct {
*Base
}

// Exec to locate file and send
func (d *Download) Exec(b *tb.Bot, m *tb.Message, conf *config.Config) {

path := conf.User.HomeDir + "/" + m.Payload
_, err := os.Stat(path)

if err == nil {
file := &tb.Document{File: tb.FromDisk(path), Caption: path}
b.Send(m.Sender, file, &tb.SendOptions{
ReplyTo: m,
})
} else {
custom.SendLong(b)(m.Sender, err.Error(), &tb.SendOptions{
ReplyTo: m,
})
}

}

// IsInline return if command is /command somethingelse
func (d *Download) IsInline() bool {
return true
}

// GetExample return the how to use of this command
func (d *Download) GetExample() string {
return "please use /file relative/path/to/home"
}
61 changes: 61 additions & 0 deletions command/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package command

import (
"bytes"
"os/exec"

"github.com/nathenapse/telegram-commander/config"
"github.com/nathenapse/telegram-commander/custom"
tb "gopkg.in/tucnak/telebot.v2"
)

// Downloader to download files
type Downloader struct {
*Base
}

// Exec to locate file and send
func (d *Downloader) Exec(b *tb.Bot, m *tb.Message, conf *config.Config) {

go b.Reply(m, conf.Downloader+" "+m.Payload)

// TODO: confirm to overwrite
cmd := exec.Command("sh", "-c", conf.Downloader+" "+m.Payload)

var out bytes.Buffer
var errb bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errb

cmd = SetUser(cmd, conf)

go b.Reply(m, "Download, starting")
err := cmd.Run()

if err != nil && errb.String() != "" {
go b.Send(m.Sender, "Download, Stoped with error", &tb.SendOptions{
ReplyTo: m,
})
go custom.SendLong(b)(m.Sender, err.Error()+":"+errb.String(), &tb.SendOptions{
ReplyTo: m,
})
} else {
go b.Send(m.Sender, "Download, Finished", &tb.SendOptions{
ReplyTo: m,
})
go custom.SendLong(b)(m.Sender, out.String(), &tb.SendOptions{
ReplyTo: m,
})
}

}

// IsInline return if command is /command somethingelse
func (d *Downloader) IsInline() bool {
return true
}

// GetExample return the how to use of this command
func (d *Downloader) GetExample() string {
return "please use /download url"
}
27 changes: 27 additions & 0 deletions command/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package command

import (
"github.com/nathenapse/telegram-commander/config"
tb "gopkg.in/tucnak/telebot.v2"
)

// Help run on help
type Help struct {
*ListCommands
}

// Exec to locate file and send
func (h *Help) Exec(b *tb.Bot, m *tb.Message, conf *config.Config) {
commands, descriptions := h.Setup()
message := `This Bot is used to do simple things to your unix based pc from the confort of telegram
like start downloading a file, run commands, get files, send files .. etc
Commands To use
`

for i := 0; i < len(commands); i++ {
message = message + "/" + commands[i] + " - " + descriptions[i] + "\n"
}

go b.Reply(m, message)
}
Loading

0 comments on commit 2b48a93

Please sign in to comment.