-1

私は単にユーザー入力をエコーするfbボットを作成しました。 ユーザー入力を続けてエコーし、ユーザーが "bye"を入力すると停止します。 どうすればいいですか?助けてください。 コード:send_message(sender_id, reply)機能なしの復帰についてどう `` `輸入OS 輸入SYS 輸入JSONユーザーの入力を無限に繰り返すフェイスブックボットを作成する方法。ユーザーが特定のコマンド(たとえば、bye)を入力した場合にのみ停止します。

import requests 
import time 
from flask import Flask, request 

app = Flask(__name__) 

@app.route('/', methods=['GET']) 
def verify(): 
    if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"): 
     if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]: 
      return "Verification token mismatch", 403 
     return request.args["hub.challenge"], 200 

    return "Hello world", 200 


@app.route('/', methods=['POST']) 
def webhook(): 
    data = request.get_json() 
    if data["object"] == "page": 
     for entry in data["entry"]: 
      for messaging_event in entry["messaging"]: 
       if messaging_event.get("message"): # someone sent us a message 
        sender_id = messaging_event["sender"]["id"]   
        recipient_id = messaging_event["recipient"]["id"] 
        message_text = messaging_event["message"]["text"] 

        reply = "Received : " + message_text 

        if "bye" in message_text.lower(): 
         reply = "Good-bye" 
        send_message(sender_id, reply) 

    return "ok", 200 


def send_message(recipient_id, message_text): 

    log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text)) 

    params = { 
     "access_token": os.environ["PAGE_ACCESS_TOKEN"] 
    } 
    headers = { 
     "Content-Type": "application/json" 
    } 
    data = json.dumps({ 
     "recipient": { 
      "id": recipient_id 
     }, 
     "message": { 
      "text": message_text 
     } 
    }) 
    r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 
    if r.status_code != 200: 
     log(r.status_code) 
     log(r.text) 



if __name__ == '__main__': 
    app.run(debug=True) ``` 
+0

私はfbボットを作成してユーザーの入力を単純にエコーします。私はそれがユーザー入力を続けてエコーし、ユーザーが "bye"を入力すると停止するようにします。 これは、ユーザーの入力が無限にエコーバックされ、「バイ」が発生した場合に限り無限ループが中断することを意味します。 –

答えて

0

?この

if "bye" in message_text.lower(): 
    return "bye", 200 
+0

その場合、ユーザーは「さようなら」メッセージを見ることができません。 また、これを無限エコーにすると、無限ループを壊すことはありません。 byeが送信されるとすぐに無限ループを破ることが問題の目的です。 –

0

のようにこのコードは、私は彼/彼女が言う後にループは、ファイルやDB内のユーザーのFBIDを追加解除するには、クラスベースのビュー

を使用しているという事実を除いて、あなたに非常によく似ていますさよなら メッセージが投稿される度に、fbidをチェックして、ユーザーがすでに「bye」と言っていたかどうかを確認します。はいの場合は空の応答を返す

def post_facebook_message(fbid, recevied_message): 
    """ 
    takes a user fb id and posts the bot message as response 
    """ 
    post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token='+PAGE_ACCESS_TOKEN 
    response_msg = json.dumps({"recipient":{"id":fbid}, "message":{"text":recevied_message}}) 
    status = requests.post(post_message_url, headers={"Content-Type": "application/json"},data=response_msg) 
    print status.json() 

class FBBotView(generic.View): 
    #this is to verify your FB account with your program 
    #use ythe same verify token used while registering the bot 
    @method_decorator(csrf_exempt) 
    def get(self, request, *args, **kwargs): 
     if self.request.GET['hub.verify_token'] == VERIFY_TOKEN: 
      return HttpResponse(self.request.GET['hub.challenge']) 
     else: 
      return HttpResponse('Error, invalid token') 
    @method_decorator(csrf_exempt) 
    def dispatch(self, request, *args, **kwargs): 
     return generic.View.dispatch(self, request, *args, **kwargs) 

    # Post function to handle Facebook messages 
    def post(self, request, *args, **kwargs): 
     # Converts the text payload into a python dictionary 
     incoming_message = json.loads(self.request.body.decode('utf-8')) 
     # Facebook recommends going through every entry since they might send 
     # multiple messages in a single call during high load 
     for entry in incoming_message['entry']: 
      for message in entry['messaging']: 
       # Check to make sure the received call is a message call 
       # This might be delivery, optin, postback for other events 
       if 'message' in message: 
        if recieved_message.lower() == "bye": 
         #sends an empty response 
         message = "Good bye" 

        post_facebook_message(message['sender']['id'],message) 
     #this line is important because many times FB checks validity of webhook. 
     return HttpResponse() 
+0

その場合、ユーザーは「さようなら」メッセージを見ることができません。また、これを無限のエコーにすると、無限ループを壊すことはありません。 byeが送信されるとすぐに無限ループを打ち破ることは、質問の目的です –

+0

@KaustabhDattaChoudhuryループを壊すことによって何を意味するのですか、ユーザーがバイと言うと、ボットは何も返答しないはずですか? –

+0

その場合、これらのfbidをDBまたはファイルに追加します。 –

関連する問題