はじめに
現在は対話ロボットの研究に取り組んでいるが,対話システムを1から自分で実装できるようにするための勉強を始めようと思い,『Pythonでつくる対話システム』に取り組む.
環境
macOS: Big Sur(バージョン 11.6.7)
言語: Python 3.9.6
メッセンジャーアプリ: Telegram
第一章 対話システムを作るにあたって
『Pythonでつくる対話システム』ソースコード&データ配布とサポートページから必要なコードをダウンロードして動かす.
- Telegramのインストール
- Telegram上でBotの作成
- アクセストークンを取得
- telegram_bot.pyに取得したアクセストークンを貼り付け
- echo_system.py(おうむ返しをするプログラム)を起動
- エラーの修正
サンプルコードをダウンロードしたまま動かしたが,以下のエラーが発生.
No error handlers are registered, logging exception.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 557, in process_update
handler.handle_update(update, self, check, context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/telegram/ext/handler.py", line 199, in handle_update
return self.callback(update, context)
File "/Users/dsbook/telegram_bot.py", line 20, in message
input = {'utt': update.message.text, 'sessionId': str(update.message.from_user.id)}
AttributeError: 'CallbackContext' object has no attribute 'message'
telegram_bot.py中のコールバック関数において,引数の順番が間違っていたために発生している.
参考ページ:AttributeError: 'CallbackContext' object has no attribute 'message'
telegram_bot.py
def start(self, bot, update):
def message(self, bot, update):
正しくは以下のようにする.
telegram_bot.py
def start(self, update, bot):
def message(self, update, bot):
動作成功
おわりに
今回はチュートリアルとして,おうむ返しを行うTelegramのチャットボットを実際に動かしてみた.次回は第二章の状態遷移に基づくタスク指向型対話システムに取り組む.