Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dlion committed Feb 24, 2017
0 parents commit fd7f3d2
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test
vendor

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Opus2Audio

Be able to listen Whatsapp's voice audio files on Telegram

## What is it?

Opus2Audio is a Telegram's bot that allows you to be able to listen Whatsapp's voice audio files on Telegram

## How it work?

Opus2Audio use online-convert's APIs to converts OPUS files, so to use it you need an APIKEY from http://www.online-convert.com/.

Obviously you need an APIKEY from Telegram to use your own bot.

Pay attention: **The free account allows to you to convert 30files per day**.

## How to use it?

First of all you need to create your own bot on Telegram, follows these steps: https://core.telegram.org/bots

Then you have to create an account on online-convert to retrieve your APIKEY, follows this doc: http://apiv2.online-convert.com/

Then fill the constants in main.go file


I suggest to you to use glide as dependencies manager, so you can use it (`glide install`) or you can use `go get`.

That's all, now you can run or build it without any problems.

## License

MIT

## Author

Domenico Luciani aka DLion
https://domenicoluciani.com
8 changes: 8 additions & 0 deletions glide.lock

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

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package: github.com/dlion/opus2audio
import:
- package: github.com/go-telegram-bot-api/telegram-bot-api
205 changes: 205 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package main

import (
"encoding/json"
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)

const (
URL = "api2.online-convert.com/jobs"
BOTAPI = "TELEGRAM_BOT_API_KEY_HERE"
DEBUG = true
APICONVERTER = "ONLINE-CONVERTER_API_KEY_HERE"
)

func main() {
fmt.Printf("--- OPUS2AUDIO ---\n" +
"Author: Domenico (DLion) Luciani\n" +
"Site: https://domenicoluciani.com\n" +
"License: MIT\n")

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

if DEBUG {
log.Printf("Authorized on account %s", bot.Self.UserName)
}

u := tgbotapi.NewUpdate(0)
u.Timeout = 60

updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Panicln(err)
}

var mJob JsonResponsePost
mFiles := make(JsonResponseGet, 1)

for update := range updates {
//If no messages -> skip
if update.Message == nil {
continue
}
//Only Documet type accepted
if update.Message.Document == nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "I need to receive only Document files")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
continue
}

source, err := bot.GetFileDirectURL(update.Message.Document.FileID)
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error on getting file's direct URL")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

if DEBUG {
log.Println("Source: " + source)
}

payload := strings.NewReader("{\"input\":[{\"type\":\"remote\",\"source\":\"" + source + "\"}],\"conversion\":[{\"category\":\"audio\",\"target\":\"ogg\"}]}")

client := &http.Client{}
req, err := http.NewRequest("POST", "http://"+URL, payload)
req.Header.Add("x-oc-api-key", APICONVERTER)
req.Header.Add("content-type", "application/json")
req.Header.Add("cache-control", "no-cache")

if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error on the POST request to online-converter")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

res, err := client.Do(req)
defer res.Body.Close()

if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error doing the POST request")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error on the reading the body's response POST request")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

err = json.Unmarshal(body, &mJob)
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error decoding the POST response's json")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

//Warn the user
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Request taken, please wait...")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)

//Wait 5 seconds
time.Sleep(5000 * time.Millisecond)

req, err = http.NewRequest("GET", "http://"+string(URL)+"/"+string(mJob.ID)+"/output", nil)
req.Header.Add("x-oc-api-key", APICONVERTER)
req.Header.Add("content-type", "application/json")
req.Header.Add("cache-control", "no-cache")
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error on the GET request to online-converter")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

if DEBUG {
log.Println("URL GET: https://" + string(URL) + "/" + string(mJob.ID) + "/output")
}

res, err = client.Do(req)
defer res.Body.Close()
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error doing the GET request")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

body, err = ioutil.ReadAll(res.Body)
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error on the reading the GET response's body")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

err = json.Unmarshal(body, &mFiles)
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error decoding the GET response's json")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

if DEBUG {
log.Printf("URI: %s\nSIZE: %d\n", mFiles[0].URI, mFiles[0].Size)
}

out, err := os.Create("tmpFiles/" + mFiles[0].ID + ".ogg")
defer out.Close()
if err != nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error on create a new file on the local disk")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Panicln(err)
}

//Download file
resp, err := http.Get(mFiles[0].URI)
defer resp.Body.Close()
if err != nil {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error getting the result from the URI")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Println(err)
}

io.Copy(out, resp.Body)

//Send file
result := tgbotapi.NewAudioUpload(update.Message.Chat.ID, "tmpFiles/"+mFiles[0].ID+".ogg")
result.Duration = 20 //20 seconds
result.BaseFile.BaseChat.ReplyToMessageID = update.Message.MessageID
bot.Send(result)

//Remove tmpFile
if _, err := os.Stat("tmpFiles/" + mFiles[0].ID + ".ogg"); err == nil {
err = os.Remove("tmpFiles/" + mFiles[0].ID + ".ogg")
if err != nil && DEBUG {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Error getting the result from the URI")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
log.Panicln("Error on deleting tmpFile")
}
}
}
}
64 changes: 64 additions & 0 deletions responses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

//Structs taken from http://apiv2.online-convert.com/ doc

type JsonResponsePost struct {
ID string `json:"id"`
Token string `json:"token"`
Type string `json:"type"`
Status struct {
Code string `json:"code"`
Info string `json:"info"`
} `json:"status"`
Errors []interface{} `json:"errors"`
Process bool `json:"process"`
Conversion []struct {
ID string `json:"id"`
Target string `json:"target"`
Category string `json:"category"`
Options struct {
Frequency interface{} `json:"frequency"`
AudioBitrate interface{} `json:"audio_bitrate"`
DownloadPassword interface{} `json:"download_password"`
AllowMultipleOutputs bool `json:"allow_multiple_outputs"`
Channels interface{} `json:"channels"`
Normalize bool `json:"normalize"`
Start interface{} `json:"start"`
End interface{} `json:"end"`
} `json:"options"`
} `json:"conversion"`
Input []struct {
ID string `json:"id"`
Type string `json:"type"`
Source string `json:"source"`
Filename string `json:"filename"`
Size int `json:"size"`
Hash string `json:"hash"`
Checksum string `json:"checksum"`
ContentType string `json:"content_type"`
CreatedAt string `json:"created_at"`
ModifiedAt string `json:"modified_at"`
} `json:"input"`
Output []interface{} `json:"output"`
Callback string `json:"callback"`
NotifyStatus bool `json:"notify_status"`
Server string `json:"server"`
Spent int `json:"spent"`
CreatedAt string `json:"created_at"`
ModifiedAt string `json:"modified_at"`
}

type JsonResponseGet []struct {
ID string `json:"id"`
Source struct {
Conversion string `json:"conversion"`
Input []string `json:"input"`
} `json:"source"`
URI string `json:"uri"`
Size int `json:"size"`
Status string `json:"status"`
ContentType string `json:"content_type"`
DownloadsCounter int `json:"downloads_counter"`
Checksum string `json:"checksum"`
CreatedAt string `json:"created_at"`
}
Empty file added tmpFiles/.gitkeep
Empty file.

0 comments on commit fd7f3d2

Please sign in to comment.