Add the org-mode entry

This commit is contained in:
Sameer Rahmani 2022-04-16 11:56:00 +01:00
parent 284d99fa16
commit a62f59a73d
3 changed files with 50 additions and 26 deletions

View File

@ -8,11 +8,13 @@ In order to use it, first you need to setup the project which you can just call
following command: following command:
```bash ```bash
./builder run -owner TELEGRAM_ID -model PATH_TO_MODEL -scorer PATH_TO_SCORER ./builder run -owner TELEGRAM_ID -org-file ORG_FILE_PATH -model PATH_TO_MODEL -scorer PATH_TO_SCORER
``` ```
* **TELEGRAM_ID**: Is your telegram id. It will only reply to you and tell others * **TELEGRAM_ID**: Is your telegram id. It will only reply to you and tell others
to F off. to F off.
* **ORG_FILE_PATH**: Path to an `org-mode` file that **HAS TO EXIST**. Orion will append entries
to this file.
* **PATH_TO_MODEL(optoinal)**: Is the path to the model file you want to use for speech recognition. * **PATH_TO_MODEL(optoinal)**: Is the path to the model file you want to use for speech recognition.
the default points to the default model in the models directory. the default points to the default model in the models directory.

View File

@ -33,6 +33,7 @@ var model = flag.String("model", "models/default.tflite", "Path to the model (pr
var scorer = flag.String("scorer", "models/default.scorer", "Path to the external scorer") var scorer = flag.String("scorer", "models/default.scorer", "Path to the external scorer")
var owner = flag.String("owner", "", "Telegram user id that is allowed to use this bot") var owner = flag.String("owner", "", "Telegram user id that is allowed to use this bot")
var storage = flag.String("voice-storage", "", "Where to store the voices") var storage = flag.String("voice-storage", "", "Where to store the voices")
var orgfile = flag.String("org-file", "", "The target Orgfile")
func main() { func main() {
flag.Parse() flag.Parse()
@ -44,7 +45,7 @@ func main() {
return return
} }
if *model == "" || *scorer == "" { if *model == "" || *scorer == "" || *orgfile == "" {
// In case of error print error and print usage // In case of error print error and print usage
// This can also be done by passing -h or --help flags // This can also be done by passing -h or --help flags
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
@ -61,7 +62,7 @@ func main() {
*storage = filepath.Join(dir, ".orion", "storage") *storage = filepath.Join(dir, ".orion", "storage")
} }
bot, err := core.CreateBot(storage) bot, err := core.CreateBot(orgfile, storage)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return

View File

@ -18,10 +18,10 @@ package core
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time" "time"
@ -31,23 +31,25 @@ import (
) )
type Bot struct { type Bot struct {
Owner string Owner string
StoragePath string StoragePath string
VoiceStorage string VoiceStorage string
Token string Token string
Model *asticoqui.Model Model *asticoqui.Model
ExtendedMetaData bool ExtendedMetaData bool
MaxResults uint MaxResults uint
db *DB OrgFile string
Format string
db *DB
} }
type Entry struct { type Entry struct {
FilePath string FilePath string
Transcript string Transcript string
Visited bool Visited bool
} }
func CreateBot(storage *string) (*Bot,error) { func CreateBot(orgfile *string, storage *string) (*Bot, error) {
voiceStorage := filepath.Join(*storage, "voices") voiceStorage := filepath.Join(*storage, "voices")
dbpath := filepath.Join(*storage, "db") dbpath := filepath.Join(*storage, "db")
err := os.MkdirAll(voiceStorage, 0750) err := os.MkdirAll(voiceStorage, 0750)
@ -63,12 +65,14 @@ func CreateBot(storage *string) (*Bot,error) {
} }
return &Bot{ return &Bot{
Token: os.Getenv("BOT_TOKEN"), Token: os.Getenv("BOT_TOKEN"),
MaxResults: 5, MaxResults: 5,
ExtendedMetaData: false, ExtendedMetaData: false,
StoragePath: *storage, StoragePath: *storage,
VoiceStorage: voiceStorage, VoiceStorage: voiceStorage,
db: db, OrgFile: *orgfile,
db: db,
Format: "* %s\n:FILE:\npath:%s\nsize:%s\n:END:\n",
}, nil }, nil
} }
@ -81,7 +85,7 @@ func (bot *Bot) ShutDown() error {
func (bot *Bot) StartBot() { func (bot *Bot) StartBot() {
pref := tele.Settings{ pref := tele.Settings{
Token: bot.Token, Token: bot.Token,
// TODO: Move this to the config // TODO: Move this to the config
Poller: &tele.LongPoller{Timeout: 10 * time.Second}, Poller: &tele.LongPoller{Timeout: 10 * time.Second},
} }
@ -125,8 +129,27 @@ func (bot *Bot) StartBot() {
b.Start() b.Start()
} }
func (bot *Bot) SaveTranscript(filepath *string, text *string) error { func (bot *Bot) SaveTranscript(voicefilepath *string, text *string) error {
return ioutil.WriteFile(*filepath + ".txt", []byte(*text), 0600) fi, err := os.Stat(*voicefilepath)
if err != nil {
return err
}
entry := fmt.Sprintf(bot.Format,
*text, *voicefilepath,
strconv.FormatInt(fi.Size(), 10))
f, err := os.OpenFile(bot.OrgFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(entry); err != nil {
return err
}
return nil
} }
func (bot *Bot) isOwner(c tele.Context) bool { func (bot *Bot) isOwner(c tele.Context) bool {
@ -144,8 +167,8 @@ func (bot *Bot) StoreVoice(c tele.Context) (*string, error) {
v := c.Message().Voice.File v := c.Message().Voice.File
time := c.Message().Time().Format("2006-01-02_15:04:05") time := c.Message().Time().Format("2006-01-02_15:04:05")
path := filepath.Join(bot.VoiceStorage, time + ".ogg") path := filepath.Join(bot.VoiceStorage, time+".ogg")
wavePath := filepath.Join(bot.VoiceStorage, time + ".wav") wavePath := filepath.Join(bot.VoiceStorage, time+".wav")
if err := c.Bot().Download(&v, path); err != nil { if err := c.Bot().Download(&v, path); err != nil {
return nil, err return nil, err
@ -162,7 +185,6 @@ func (bot *Bot) StoreVoice(c tele.Context) (*string, error) {
return &wavePath, nil return &wavePath, nil
} }
func (bot *Bot) ConvertToText(voice *string) (*[]string, error) { func (bot *Bot) ConvertToText(voice *string) (*[]string, error) {
// Stat audio // Stat audio
i, err := os.Stat(*voice) i, err := os.Stat(*voice)
@ -222,7 +244,6 @@ func (bot *Bot) ConvertToText(voice *string) (*[]string, error) {
return &results, nil return &results, nil
} }
func metadataToStrings(m *asticoqui.Metadata) []string { func metadataToStrings(m *asticoqui.Metadata) []string {
results := make([]string, 0, m.NumTranscripts()) results := make([]string, 0, m.NumTranscripts())
for _, tr := range m.Transcripts() { for _, tr := range m.Transcripts() {