Browse Source

Work in progress

master
Nicolas Massé 6 years ago
parent
commit
5e1f326b3f
  1. 3
      .gitignore
  2. 68
      README.md
  3. 28
      main.go

3
.gitignore

@ -1 +1,2 @@
*.yaml
*.yaml
photo-bot

68
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

28
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
}

Loading…
Cancel
Save