我是golang的新手,我正在尝试编写一个函数,该函数将带有发布请求的文件上传到电报中以供我编写的机器人使用。
我已经尝试过使用此代码,但是我从电报中收到的错误是
2018-07-08T16:21:10.427313+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2018-07-08T16:21:11.167238+00:00 heroku[worker.1]: State changed from starting to up
2018-07-08T16:21:15.159855+00:00 heroku[worker.1]: State changed from up to crashed
2018-07-08T16:21:15.056356+00:00 heroku[worker.1]: Process exited with status 0
2018-07-08T16:56:32.097321+00:00 heroku[worker.1]: State changed from crashed to starting
2018-07-08T16:56:36.064795+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2018-07-08T16:56:36.789264+00:00 heroku[worker.1]: State changed from starting to up
2018-07-08T16:56:40.212853+00:00 heroku[worker.1]: State changed from up to crashed
2018-07-08T16:56:40.170153+00:00 heroku[worker.1]: Process exited with status 0
2018-07-08T17:08:08.000000+00:00 app[api]: Build started by user myemailaddress
2018-07-08T17:08:23.727988+00:00 app[api]: Deploy 852c8541 by user myemailaddress
2018-07-08T17:08:23.727988+00:00 app[api]: Release v17 created by user myemailaddress
2018-07-08T17:08:24.186005+00:00 heroku[worker.1]: State changed from crashed to starting
2018-07-08T17:08:24.000000+00:00 app[api]: Build succeeded
2018-07-08T17:08:26.514939+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2018-07-08T17:08:27.156637+00:00 heroku[worker.1]: State changed from starting to up
2018-07-08T17:08:28.631772+00:00 heroku[worker.1]: Process exited with status 0
2018-07-08T17:08:28.723695+00:00 heroku[worker.1]: State changed from up to crashed
2018-07-08T17:08:28.725787+00:00 heroku[worker.1]: State changed from crashed to starting
2018-07-08T17:08:31.479079+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2018-07-08T17:08:32.358515+00:00 heroku[worker.1]: State changed from starting to up
2018-07-08T17:08:33.922886+00:00 heroku[worker.1]: State changed from up to crashed
2018-07-08T17:08:33.878013+00:00 heroku[worker.1]: Process exited with status 0
2018-07-08T17:12:36.000000+00:00 app[api]: Build started by user myemailaddress
2018-07-08T17:12:48.895075+00:00 app[api]: Release v18 created by user myemailaddress
2018-07-08T17:12:48.895075+00:00 app[api]: Deploy 1fbb896c by user myemailaddress
2018-07-08T17:12:49.246800+00:00 heroku[worker.1]: State changed from crashed to starting
2018-07-08T17:12:49.000000+00:00 app[api]: Build succeeded
2018-07-08T17:12:53.635812+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2018-07-08T17:12:54.327615+00:00 heroku[worker.1]: State changed from starting to up
2018-07-08T17:12:57.874384+00:00 heroku[worker.1]: State changed from up to crashed
2018-07-08T17:12:57.852555+00:00 heroku[worker.1]: Process exited with status 0
2018-07-08T17:35:48.475654+00:00 heroku[worker.1]: State changed from crashed to starting
2018-07-08T17:35:52.257156+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2018-07-08T17:35:52.995975+00:00 heroku[worker.1]: State changed from starting to up
2018-07-08T17:35:56.888137+00:00 heroku[worker.1]: State changed from up to crashed
2018-07-08T17:35:56.871199+00:00 heroku[worker.1]: Process exited with status 0
。
我已经在网上搜索了如何执行此操作,但发现没有任何帮助我解决了该问题。
const twit = require('twit');
const getVideo = require('./youtube');
const { getLyrics } = require('./lyricadder/lyrics');
const { randomNumber } = require('./helpers');
const config = {
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token: process.env.access_token,
access_token_secret: process.env.access_token_secret
};
const Twitter = new twit(config);
const lyrics = getLyrics();
const tweetLyric = () => {
const random = randomNumber(lyrics.length);
const lyric = lyrics[random];
getVideo(lyric.song).then(response => {
const songHash = lyric.song.replace(/\s/g, '');
const tweet = {
status: `${lyric.lyric}\n${response} #ToriAmos #${songHash}`
};
Twitter.post('statuses/update', tweet, (err, data, response) => {
if (err) {
console.log(err);
}
});
});
};
tweetLyric();
我从中调用SendPostRequest的函数是
Bad Request: there is no photo in the request
编辑: 我在代码中使用的Telegram机器人api的链接为https://core.telegram.org/bots/api
答案 0 :(得分:2)
经过一番挖掘,我发现了这一点
func SendPostRequest (url string, filename string, filetype string) []byte {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(filetype, filepath.Base(file.Name()))
if err != nil {
log.Fatal(err)
}
io.Copy(part, file)
writer.Close()
request, err := http.NewRequest("POST", url, body)
if err != nil {
log.Fatal(err)
}
request.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
return content
}