1
1

More than 3 years have passed since last update.

Pythonのエラーコードを投げると解説してくれるSlackBotを作った話

Last updated at Posted at 2021-08-12

はじめに

今回はSlackにPyhtonのエラーコードを投げると画像付きでエラーを解説してくれるSlackBotを作りました。(Web版もあるよ!)

スクリーンショット 2021-08-11 14.57.00.png
このような感じで、エラー文をBotに投げるとエラーの詳細とエラー例が画像で返されます。

構造

今回はGoogleAppScriptでSlackBotを作成しました。SlashCommandに反応して投げられたエラーコードを正規表現でエラーを分けて該当エラーの画像とともに返すものです。

使ったもの

・GoogleAppScript
・Github Pages(画像置き場)

コード

main.gs
function doPost(e) {
  var verificationToken = e.parameter.token;
  if (verificationToken != 'YOUR Token!') { 
    throw new Error('Invalid token');
  }

var command = e.parameter.text;
if ( command.match(/SyntaxError/)) {
  if ( command.match(/function definition/)) {

  command="構文エラー(引数) 引数が重複している可能性があります。"
  urls="http://ituyama.com/errorsearch/imgs/syntax-3.png"

   } else if ( command.match(/literal/)) {

    command="構文エラー(クオーテーション忘れ)クオーテーションやダブルクオーテーションを忘れている可能性があります。"
    urls="http://ituyama.com/errorsearch/imgs/syntax-1.png"

    } else if ( command.match(/parentheses/)) {

    command="構文エラー(かっこ忘れ)かっこを忘れている可能性があります。"
    urls="http://ituyama.com/errorsearch/imgs/syntax-2.png"

    }else if ( command.match(/invalid/)) {
    command="構文エラー コロン(:)が抜けている可能性があります。"
    urls="http://ituyama.com/errorsearch/e1s.png"
  }else{
    command="構文エラー コロン(:)やかっこが抜けている可能性があります。"
    urls="http://ituyama.com/errorsearch/e1s.png"

  }



}
else if ( command.match(/IndentationError/)) {
    if ( command.match(/unexpected/)) {

  command="インデントエラー(不要なインデント) 不要なインデントがある可能性があります"
  urls="http://ituyama.com/errorsearch/imgs/indent-1.png"

   }else  if ( command.match(/indentation/)) {

  command="インデントエラー(インデントの不足) インデントが不足しています。"
  urls="http://ituyama.com/errorsearch/imgs/indent.png"

   }else{
     command="インデントエラー インデントがどこか抜けている可能性があります。"
      urls="http://ituyama.com/errorsearch/imgs/indent.png"
   }


}
else if ( command.match(/NameError/)) {

command="ネームエラー 定義していない変数を使用している可能性があります。"
urls="http://ituyama.com/errorsearch/imgs/name.png"
}
else if ( command.match(/TypeError/)) {
  if ( command.match(/arguments/)) {

  command="型エラー(引数) 引数の数が多かったり少なかったりする可能性があります。"
  urls="http://ituyama.com/errorsearch/imgs/type-1.png"
  }else{
  command="型エラー 型を確認してみてください。(int型とstr型を足そうとしていませんか?)"
  urls="http://ituyama.com/errorsearch/imgs/type.png"
  }

}
else if ( command.match(/ValueError/)) {

command="値エラー 定められた型に対して不適切な値を渡している可能性があります。(int型の変数に文字列を渡そうとしていませんか?)"
urls="http://ituyama.com/errorsearch/imgs/value.png"
}
else if ( command.match(/IndexError/)) {

command="インデックスエラー 定義したリストに無い値を参照している可能性があります。"
urls="http://ituyama.com/errorsearch/imgs/index.png"
}
else if ( command.match(/KeyError/)) {

command="キーエラー 辞書に登録されてないキーを参照している可能性があります。(スペル間違っていませんか?)"
urls="http://ituyama.com/errorsearch/imgs/key.png"
}
else if ( command.match(/ModuleNotFoundError/)) {

command="モジュールエラー インストールしていないモジュールをimportしている可能性があります。"
urls="http://ituyama.com/errorsearch/imgs/module.png"
}
else if ( command.match(/FileNotFoundError/)) {

command="ファイルが見つからないエラー 存在しないファイルを参照している可能性があります。(ファイルのパスを確認しましょう)"
urls="http://ituyama.com/errorsearch/imgs/file.png"
}
//else if ( command.match(/UnicodeEncodeError/)) {

//command="文字コード間違ってるよエラー(エンコード) 文字コードが間違っている可能性があります。(SIFT-JSをUTF-8で読み込んでませんか?)"
//urls="http://ituyama.com/errorsearch/e1s.png"
//}
//else if ( command.match(/UnicodeDecodeError/)) {

//command="文字コード間違ってるよエラー(デコード) 文字コードが間違っている可能性があります。(SIFT-JSをUTF-8で読み込んでませんか?)"
//urls="http://ituyama.com/errorsearch/e1s.png"
//}
else{
  command="該当するものがありません。\n解決したらGithubに送ってくれると嬉しいです"
  urls="http://ituyama.com/errorsearch/imgs/sorry.png"
}
  var response = {  response_type :"in_channel",
    text: command,
    attachments : [{

      "image_url": urls
    }]

  };

  return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON);
}

あとがき

今回が初めての投稿でした。今までもいくつかプロダクトは作っていたけどもQiitaにあげられていないので、時間ができたら順次投稿していきたいです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1