From 5e1f326b3f19678bcfc43368c2840b6d0ee5f1e6 Mon Sep 17 00:00:00 2001 From: Nicolas MASSE Date: Fri, 20 Dec 2019 11:10:11 +0100 Subject: [PATCH] Work in progress --- .gitignore | 3 ++- README.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++---- main.go | 28 ++++++++++++++-------- 3 files changed, 85 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 2a61605..b80c68b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.yaml \ No newline at end of file +*.yaml +photo-bot diff --git a/README.md b/README.md index 9a3af37..4cd68ec 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,20 @@ ## Compilation -``` +Fetch dependencies. + +```sh go get -u github.com/go-telegram-bot-api/telegram-bot-api go get -u github.com/spf13/viper go get -u gopkg.in/yaml.v2 ``` +Compile for Raspberry PI. + +```sh +GOOS=linux GOARCH=arm64 go build -o photo-bot +``` + ## Create a Bot Talk to [BotFather](https://core.telegram.org/bots#6-botfather) to create your bot. @@ -18,19 +26,71 @@ Talk to [BotFather](https://core.telegram.org/bots#6-botfather) to create your b Keep your bot token secure and safe! -## Create the configuration file +## Installation + +On your Raspberry PI. -Create a file named `photo-bot.yaml` in the current directory. +```sh +mkdir -p /opt/photo-bot/bin +mkdir -p /opt/photo-bot/etc +mkdir -p /srv/photo-bot +useradd -d /srv/photo-bot -s /bin/false -m -r bot +chown bot:bot /srv/photo-bot +``` + +```sh +scp photo-bot root@raspberry-pi.example.test:/opt/photo-bot/bin/ +``` + +Create a file named `photo-bot.yaml` in `/opt/photo-bot/etc/`. ```yaml TelegramToken: "bot.token.here" TelegramDebug: true -TargetDir: /srv/photos +TargetDir: /srv/photo-bot AuthorizedUsers: - john - jane ``` +```sh +chown bot:bot /opt/photo-bot/etc/photo-bot.yaml +chmod 600 /opt/photo-bot/etc/photo-bot.yaml +``` + +Start the bot manually. + +```sh +sudo -u bot /opt/photo-bot/bin/photo-bot +``` + +Create the startup script in `/etc/init.d/photo-bot`. + +```sh +#!/bin/sh /etc/rc.common +# photo-bot + +# Start late in the boot process +START=80 +STOP=20 + +start() { + cd /opt/photo-bot/etc/ && start-stop-daemon -c bot -u bot -x /opt/photo-bot/bin/photo-bot -b -S +} + +stop() { + start-stop-daemon -c bot -u bot -x /opt/photo-bot/bin/photo-bot -b -K +} +``` + +```sh +chmod 755 /etc/init.d/photo-bot +service photo-bot enable +service photo-bot start +``` + + + ## Documentation - https://core.telegram.org/bots/api diff --git a/main.go b/main.go index dcf36fc..18fdc5a 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ func main() { viper.SetDefault("MsgNoAlbum", "No album is currently open") viper.SetDefault("MsgAlbumClosed", "Album closed") viper.SetDefault("MsgDoNotUnderstand", "Unknown command") + viper.SetDefault("MsgNoUsername", "Sorry, you need to set your username") viper.SetConfigName("photo-bot") // name of config file (without extension) viper.AddConfigPath("/etc/photo-bot/") @@ -40,6 +41,17 @@ func main() { panic(fmt.Errorf("Cannot read config file: %s\n", err)) } + logFile := viper.GetString("LogFile") + var logHandle *os.File + if logFile != "" { + logHandle, err = os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) + if err != nil { + panic(fmt.Errorf("Cannot open log file '%s': %s\n", logFile, err)) + } + defer logHandle.Close() + log.SetOutput(logHandle) + } + target_dir := viper.GetString("TargetDir") if (target_dir == "") { panic("No target directory provided!") @@ -86,6 +98,7 @@ func main() { username := update.Message.From.UserName if (username == "") { + replyToCommandWithMessage(bot, update.Message, viper.GetString("MsgNoUsername")) continue } if (! authorized_users[username]) { @@ -94,7 +107,10 @@ func main() { continue } - updateChatDB(update.Message) + err := updateChatDB(update.Message) + if err != nil { + log.Printf("[%s] cannot update chat db: %s", username, err) + } if text != "" { if strings.HasPrefix(text, "/") { @@ -365,12 +381,6 @@ func getFile(bot *tgbotapi.BotAPI, message *tgbotapi.Message, fileId string) (st return fileId + extension, nil } -type AlbumMetadata struct { - Title string `yaml:"title"` - Date string `yaml:"date"` - Folder string `yaml:"folder"` -} - func closeAlbum() error { target_dir := viper.GetString("TargetDir") yamlData, err := ioutil.ReadFile(target_dir + "/data/.current/meta.yaml") @@ -378,13 +388,13 @@ func closeAlbum() error { return err } - var metadata AlbumMetadata + var metadata map[string]string = make(map[string]string) err = yaml.UnmarshalStrict(yamlData, &metadata) if err != nil { return err } - err = os.Rename(target_dir + "/data/.current/", target_dir + "/data/" + metadata.Folder) + err = os.Rename(target_dir + "/data/.current/", target_dir + "/data/" + metadata["folder"]) if err != nil { return err }