-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechTeleBot.go
More file actions
104 lines (94 loc) · 2.52 KB
/
speechTeleBot.go
File metadata and controls
104 lines (94 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// Package telebot provides a handy wrapper for interactions
// with Telegram bots.
//
// Here is an example of helloworld bot implementation:
//
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os/exec"
"time"
"github.com/tucnak/telebot"
)
func downloadFile(bot *telebot.Bot, fileID string) (ret string, err error) {
url, err := bot.GetFileDirectURL(fileID)
tmpFile, err := ioutil.TempFile("/home/ingemar/workspace/go/src/github.com/ingemar0720/speechTeleBot", "SpeechFile")
if err != nil {
fmt.Println("create temp file error")
return "", err
}
//defer os.Remove(tmpFile.Name())
var resp *http.Response
resp, err = http.Get(url)
fmt.Println(url)
if err == nil {
defer resp.Body.Close()
if _, err = io.Copy(tmpFile, resp.Body); err == nil {
fmt.Println("download file " + tmpFile.Name())
return tmpFile.Name(), err
} else {
fmt.Println("download file fail")
return "", nil
}
} else {
return "", nil
}
}
func transcodeToMP3(ogaFile string) (mp3File string) {
params := []string{"-i", ogaFile, "./Speech.mp3"}
cmd := exec.Command("ffmpeg", params...)
if _, err := cmd.CombinedOutput(); err != nil {
return ""
} else {
return "Speech.mp3"
}
}
func main() {
//bot, err := telebot.NewBot("SECRET_TOKEN")
bot, err := telebot.NewBot("370622763:AAHWWgh36fYrOheTNWPYLg6oqCZ3UwL77-g")
if err != nil {
return
}
messages := make(chan telebot.Message)
bot.Listen(messages, 1*time.Second)
for message := range messages {
if message.Text == "/hi" {
bot.SendMessage(message.Chat,
"Hello, "+message.Sender.FirstName+"!", nil)
/*} else if message.Text == "/photo" {
fmt.Println("here in photo")
photoObj := new(telebot.Photo)
photoObj.File, err = telebot.NewFile("/home/ingemar/workspace/go/src/github.com/ingemar0720/speechTeleBot/test.jpg")
if err == nil {
fmt.Println("here to send photo")
photoObj.Caption = "testPhoto"
err := bot.SendPhoto(message.Chat, photoObj, nil)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println("new file fail")
fmt.Println(err)
}
*/
} else if message.Voice.Exists() {
fmt.Println("voice file exist")
if ogaFile, err := downloadFile(bot, message.Voice.FileID); err != nil {
fmt.Println(err)
} else {
fmt.Println("transcode to mp3 file now")
var mp3file string
if mp3file = transcodeToMP3(ogaFile); mp3file != "" {
fmt.Println("transcode mp3 file success")
}
var c WITClient
c.PostSpeech(mp3file)
}
} else {
fmt.Println("receive unrecognized message")
}
}
}