前提・実現したいこと
PythonでDjangoとHerokuを使ってLINEbotを製作しています。
こちらを参考に似たものを作っているのですが、実際に最後まで作って見たところcallbackの処理が上手くいっていないようです。原因がいまいち分からないので良ければ教えてください。
発生している問題・エラーメッセージ
以下がhttps://{アプリ名}/bot/callbackに表示されます。
ValueError at /bot/callback No JSON object could be decoded Request Method: GET Request URL: https://dry-dusk-55498.herokuapp.com/bot/callback Django Version: 1.10.3 Exception Type: ValueError Exception Value: No JSON object could be decoded Exception Location: /app/.heroku/python/lib/python2.7/json/decoder.py in raw_decode, line 382 Python Executable: /app/.heroku/python/bin/python Python Version: 2.7.15 Python Path: ['/app/.heroku/python/bin', '/app', '/app/.heroku/python/lib/python27.zip', '/app/.heroku/python/lib/python2.7', '/app/.heroku/python/lib/python2.7/plat-linux2', '/app/.heroku/python/lib/python2.7/lib-tk', '/app/.heroku/python/lib/python2.7/lib-old', '/app/.heroku/python/lib/python2.7/lib-dynload', '/app/.heroku/python/lib/python2.7/site-packages'] Server time: Thu, 7 Jun 2018 17:50:49 +0000
該当のソースコード
python
1from django.shortcuts import render 2from django.http import HttpResponse 3import json 4import requests 5import re 6import urllib 7 8REPLY_ENDPOINT = 'https://api.line.me/v2/bot/message/reply' 9ACCESS_TOKEN = "AjVgISkuYRmpS+DKpT6pqs7wp6Lzb0qT482VpiWjXTDEUK06KWSjk477LwVO6qp/z1fNdwmuxyCj3yBvipfWMFVptvBdcT0Annc0M9F2ym5wsoDdsfMQPclamBgbIu7BkJHhTuAoqDGuayZiEHdx1AdB04t89/1O/w1cDnyilFU=" 10HEADER = { 11 "Content-Type": "application/json", 12 "Authorization": "Bearer " + ACCESS_TOKEN 13} 14 15def index(request): 16 return HttpResponse("This is bot api.") 17 18def callback(request): 19 reply = "" 20 request_json = json.loads(request.body.decode('utf-8')) 21 for e in request_json['events']: 22 reply_token = e['replyToken'] 23 message_type = e['message']['type'] 24 25 if message_type == 'text': 26 text = e['message']['text'] 27 reply += reply_text(reply_token, text) 28 return HttpResponse(reply) 29 30def reply_text(reply_token, text): 31 reply = word_search(text) 32 payload = { 33 "replyToken":reply_token, 34 "messages":[ 35 { 36 "type":"text", 37 "text": reply 38 } 39 ] 40 } 41 42 requests.post(REPLY_ENDPOINT, headers=HEADER, data=json.dumps(payload)) 43 return reply 44 45def word_search(text): 46 err = False 47 x = text 48 url = 'https://ejje.weblio.jp/content/' + x.replace(' ','+') 49 response = urllib.request.urlopen(url).read().decode('utf-8') 50 51 repro = re.search(r'<span class=phoneticEjjeDesc>(.*?)<',response) 52 if repro != None: 53 pro = repro.group(1) 54 else: 55 pro = '' 56 57 remean = re.search(r'<meta name="twitter:description" content="(.*?)"',response) 58 if remean == None: 59 err = True 60 mean = remean.group(1) 61 62 url = 'https://dictionary.cambridge.org/dictionary/learner-english/' + x.replace(' ','-') 63 response = urllib.request.urlopen(url).read().decode('utf-8') 64 reenmean = re.search(r'<meta name="description" content="(.*?)Learn more."',response) 65 if reenmean == None: 66 err = True 67 enmean = reenmean.group(1) 68 69 return "{}\n{}\n{}\n{}".format(x, pro, mean, enmean)
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー