2017-01-03 2 views
1

telegraf bot framework을 사용하여 매우 간단한 전보 봇을 작성합니다. 지금까지 .hears.on 메서드를 사용하여 간단한 명령에 응답했지만 지금까지는 문제가 없습니다.시도/캐치가 충돌을 막지 않습니다

이제 문자열 Miez을 기다리는 또 다른 .hears 메서드를 구현했습니다. 이 문자열을 "듣게"되면 .replyWithDocumentcat-api URL이 포함되어 있습니다. cat-api에 따른 URL은 모든 호출에서 임의의 cat-gif를 제공합니다. 지금까지 내 코드 : 당신은 내가 try/catch 블록에서 .replyWithDocument wraped 볼 수 있습니다 그래서

const Telegraf = require('telegraf') 

const app = new Telegraf('<MY_TOKEN>') 

// Connect/Express.js integration 
const express = require('express') 
const expressApp = express() 

expressApp.set('port', (process.env.PORT || 5000)); 


app.command('start', (ctx) => { 
    console.log('start', ctx.from) 
    ctx.reply('Welcome!') 
}) 

app.hears('Hi', (ctx) => ctx.reply('Hallo!')) 
app.hears('Marco', (ctx) => ctx.reply('Polo')) 
app.on('sticker', (ctx) => ctx.reply('❤')) 

app.hears('Miez',(ctx) => { 
    try{ 
      return ctx.replyWithDocument({ 
      url: 'http://thecatapi.com/api/images/get?format=src&type=gif', 
      filename: 'cat.gif' 
      }) 

     }catch(error){ 
      return ctx.reply("Miau"); 
     } 
}) 

. 주어진 url이 항상 gif를 제공하지는 않기 때문에이 작업을 수행했습니다. 경우에 따라 Server not found 메시지가 표시되는 경우도 있습니다. 나는 해당 오류 여기 로그를하는 Heroku가에 봇을 호스팅하고이 후

Failed to process updates. { FetchError: request to http://30.media.tumblr.com/tumblr_lu65p0QXgW1r4xjo2o1_r1_500.gif failed, reason: getaddrinfo ENOTFOUND 30.media.tumblr.com 30.media.tumblr.com:80 
    at ClientRequest.<anonymous> (/app/node_modules/telegraf/node_modules/node-fetch/index.js:133:11) 
    at emitOne (events.js:96:13) 
    at ClientRequest.emit (events.js:188:7) 
    at Socket.socketErrorListener (_http_client.js:310:9) 
    at emitOne (events.js:96:13) 
    at Socket.emit (events.js:188:7) 
    at connectErrorNT (net.js:1022:8) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 
    message: 'request to http://30.media.tumblr.com/tumblr_lu65p0QXgW1r4xjo2o1_r1_500.gif failed, reason: getaddrinfo ENOTFOUND 30.media.tumblr.com 30.media.tumblr.com:80', 
    name: 'FetchError', 
    errno: 'ENOTFOUND', 
    type: 'system', 
    code: 'ENOTFOUND' } 

이 봇은 잠시 동안 Heroku가 작업을 중지 발생 후 15 분 뭔가 후 다시 등장한다, 나는 그것이 활동의 ​​약간의 시간 이후에 다시 시작된다고 생각한다.

글쎄, 나는 결론을 내리지 못한다. 때로는 실패 할 수도있다. 내가 이해하지 못하는 이유는 내 try/catch 블록이이 동작을 잡지 못하는 이유입니다. 내 통나무 해석이 정확한 경우.

편집 : 가능한 이유는 내 머리에 들었습니다. .replyWithDocument은 비동기 호출입니다. 따라서 HTTP 요청은 성공적이며 시도도 마찬가지입니다. 그러나 일단 응답이 Server not found이면 메소드 호출은 여전히 ​​실패합니다. 이것이 이유 일 수 있다면 어떻게 처리할까요?

+0

) (.catch 사용합니다. 콜백을 기반으로하는 경우 콜백에서 error 매개 변수를 처리하십시오. –

답변

4

Telegraf 사용자 정의 오류 처리 방법을 사용해야합니다!.

By default Telegraf will print all errors to stderr and rethrow error. To perform custom error-handling logic use following snippet:

const app = new Telegraf(process.env.BOT_TOKEN) 
app.catch((err) => { 
    console.log('Ooops', err) 
}) 

은 'ENOTFOUND'오류를 잡기 위해 위의 코드를 사용해보십시오.

출처 : replyWithDocument 기반의 약속 인 경우 http://telegraf.js.org/introduction.html#error-handling

관련 문제