Ejemplo n.º 1
0
def reply(chat_id):
    chat = Chat.query.filter_by(id=chat_id).first_or_404()
    request_text = request.form['msg']
    request_msg = Message(chat_id=chat_id,
                          text=request_text,
                          author=Message.AUTHOR_USER,
                          order=chat.messages_count + 1)
    if app.config['NEURAL_LOGIC']:
        from app import sess, model, enc_vocab, rev_dec_vocab
        response_text = execute.decode_line(sess, model, enc_vocab,
                                            rev_dec_vocab, request_text)
    else:
        response_text = request.form['msg']

    response_msg = Message(chat_id=chat_id,
                           text=response_text,
                           author=Message.AUTHOR_BOT,
                           order=chat.messages_count + 2)
    for word in basic_tokenizer(request_text.encode()):
        db.session.add(UserWord(word=word))
    for word in basic_tokenizer(response_text.encode()):
        db.session.add(BotWord(word=word))

    chat.messages_count = chat.messages_count + 2

    db.session.add(request_msg)
    db.session.add(response_msg)
    db.session.commit()
    return jsonify(response_msg.as_dict())
Ejemplo n.º 2
0
def LoadMyEntry(ChatLog, EntryText,sess, model, enc_vocab, rev_dec_vocab):
    s = execute.decode_line(sess, model, enc_vocab, rev_dec_vocab, EntryText)
    if EntryText != '':
        ChatLog.config(state=NORMAL)
        if ChatLog.index('end') != None:
            LineNumber = float(ChatLog.index('end')) - 1.0
            ChatLog.insert(END, "You: " + EntryText)
            ChatLog.tag_add("You", LineNumber, LineNumber + 0.4)
            ChatLog.tag_config("You", foreground="#FF3300", font=("Arial", 12, "bold"))
            ChatLog.config(state=DISABLED)
            ChatLog.yview(END)
    LoadJkEntry(ChatLog, s)
Ejemplo n.º 3
0
def reply():

    req_msg = request.form['msg']
    req_msg = " ".join(jieba.cut(req_msg))
    print('***********', req_msg, '***********')
    res_msg = execute.decode_line(sess, model, enc_vocab, rev_dec_vocab,
                                  req_msg)

    res_msg = res_msg.replace('__UNK__', '!')
    res_msg = res_msg.strip()

    # 如果接受到的内容为空,则给出相应的回复
    if res_msg == ' ':
        res_msg = '请与我聊聊天吧'

    return jsonify({'text': res_msg})
Ejemplo n.º 4
0
def webhook():

    # endpoint for processing incoming messaging events

    data = request.get_json()
    #log(data)  # you may not want to log every incoming message in production, but it's good for testing

    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"]  # the facebook ID of the person sending you the message
                    recipient_id = messaging_event["recipient"][
                        "id"]  # the recipient's ID, which should be your page's facebook ID
                    message_text = messaging_event["message"][
                        "text"]  # the message's text
                    # Respond by decoding message_text
                    send_message(
                        sender_id,
                        execute.decode_line(sess, model, enc_vocab,
                                            rev_dec_vocab, message_text))

                if messaging_event.get("delivery"):  # delivery confirmation
                    pass

                if messaging_event.get("optin"):  # optin confirmation
                    pass

                if messaging_event.get(
                        "postback"
                ):  # user clicked/tapped "postback" button in earlier message
                    pass

    return "ok", 200
Ejemplo n.º 5
0
def tf_reply(message):
    message.reply(execute.decode_line(sess, model, enc_vocab, rev_dec_vocab, message.body['text']))
    message.reply("test reply. You said %s" % message.body['text'])
Ejemplo n.º 6
0
def answer(message):
    reply = normalize(execute.decode_line(sess, model, enc_vocab, rev_dec_vocab, message.text))
    reply += ' ' + normalize(execute.decode_line(sess, model, enc_vocab, rev_dec_vocab, reply))
    bot.send_message(message.chat.id, reply)