Skip to content

Commit

Permalink
Open source Soteria.
Browse files Browse the repository at this point in the history
  • Loading branch information
issmirnov committed Jun 7, 2019
0 parents commit 7519244
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# binaries
soteria
tgfu
out/*
.idea/*
75 changes: 75 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
PACKAGE = github.com/issmirnov/soteria
TARGET = soteria

SHELL := /bin/bash

# figure out current platform for deps.
UNAME := $(shell uname | tr A-Z a-z )

LDFLAGS = -s -w

.DEFAULT_GOAL := all

.PHONY: \
all \
build \
deps \
debug \
init

## Default action: debug.
all: debug

## Compile and run with sample file.
debug:
go build -o "$(TARGET)" .
# Contributors: provde the TOKEN and CHATID here if you wish. Just make sure not to commit and push!
./$(TARGET) -f tests/hello_world.txt

## Compile and compress binary.
build:
mkdir -p out
GOOS=linux go build -ldflags "$(LDFLAGS)" -o "out/$(TARGET)-linux" .
upx --brute out/$(TARGET)-linux
GOOS=darwin go build -ldflags "$(LDFLAGS)" -o "out/$(TARGET)-darwin" .
upx --brute out/$(TARGET)-darwin

## "go get" deps
deps:
go get ./...

## Install system deps
init: deps
ifeq ($(UNAME), linux)
sudo apt install upx-ucl
endif
ifeq ($(UNAME), darwin)
brew install upx
endif


# Fancy help message
# Source: https://gist.github.com/prwhite/8168133
# COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM=20

## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Soteria

A simple bot that sends files to a user.


## Usage

1. Create a bot by talking to https://telegram.me/BotFather. Copy down the TOKEN.
2. compile and run this binary, passing the token as a flag (`--token` or environment variable (`TOKEN=xxx`)).
This will start up the bot in echo mode. Start a chat with your bot and make note of the chatID printed in the logs.
3. Now you have everything you need. Call the bot with the token and chatID set, and a path to a file: `./soteria --token=xxx --chatID=123 --file=tests/hello_world`

```
Usage of ./soteria:
--chatid int [env: `CHATID`] Telegram chatID for your bot + user.
-f, --file string [env: `FILE`] path for file to send
-t, --token string [env: `TOKEN`] Telegram bot token.
```


## What's in a name?

[Soteria](https://en.wikipedia.org/wiki/Soteria_(mythology)) is the Greek Godess of Deliverance. Naming this bot TGFU
(TeleGram File Uploader) just didn't have the same ring to it. Hence, Soteria lives again in the modern age.

## Contributing

Clone this repo. Run `make init` to get all the tools and deps. `make build` will create a [upx](https://upx.github.io/) minified binary.
99 changes: 99 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"fmt"
"log"
"os"

"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/telegram-bot-api.v4"
)

func initFlags() {
pflag.StringP("token", "t", "", "Telegram bot token.")
viper.BindEnv("TOKEN")

pflag.Int64("chatid", 0, "Telegram chatID for your bot + user.")
viper.BindEnv("CHATID")

pflag.StringP("file", "f", "", "path for file to send")
viper.BindEnv("FILE")

pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}

func main() {
initFlags()

token := viper.GetString("token")
if token == "" {
fmt.Printf("Error: please provide a token set as an enviroment variable `TOKEN`\n")
fmt.Printf("You can create a bot by talking to https://telegram.me/BotFather\n")
os.Exit(1)
}

bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}

b, err := bot.GetMe()
if err != nil {
fmt.Printf("Fatal error: unexpected issue when trying to get bot info. Is the token correct?")
fmt.Printf(err.Error())
os.Exit(2)
}
fmt.Printf("Bot created with username: %s\n", b.UserName)

chatID := viper.GetInt64("chatid")
if chatID == 0 {
fmt.Printf("No `CHATID` env var or `--chatid` flag provided. Starting up in echo mode.\n")
fmt.Printf("Please start a chat with https://telegram.me/%s and make note of the chatID, then restart this bot.\n", b.UserName)
startEcho(bot)
os.Exit(3)
}

filePath := viper.GetString("file")
if filePath == "" {
fmt.Println("Error: Please provide a file path with the '-f' flag or via the `FILE` env var")
os.Exit(4)
}

if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Printf("Error: File '%s' does not exist\n", filePath)
os.Exit(5)
}

fmt.Printf("Sending %s...\n", filePath)

msg := tgbotapi.NewDocumentUpload(chatID, filePath)
_, err = bot.Send(msg)
if err != nil {
fmt.Printf("Error sending message: %s", err)
} else {
fmt.Println("Success!")
}
}

// startEcho is a very simple bot that simply prints out the username and chatID of all messages that arrive.
// This is used to get the `CHATID` value for the main bot.
func startEcho(bot *tgbotapi.BotAPI) {
updates, err := bot.GetUpdatesChan(tgbotapi.UpdateConfig{
Offset: 0,
Timeout: 60,
Limit: 0,
})
if err != nil {
fmt.Printf("error opening updates channel: %s\n", err.Error())
}

fmt.Printf("Waiting for messages...\n")
for update := range updates {
resp := fmt.Sprintf(" - User: %s, chatID: %d", update.Message.From.UserName, update.Message.Chat.ID)
fmt.Println(resp)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, resp)
bot.Send(msg)
}
}
1 change: 1 addition & 0 deletions tests/hello_world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hai

0 comments on commit 7519244

Please sign in to comment.