示例#1
0
    def main(self):
        """Check the user option to send or search the message by letting them choose the key.

        If they key is 1, the user is authenticated, message is created and sent to the recipient 
        (This uses authorization,create_message and send_message modules)

        If they key is 2, the query to be searched is received from the user,the user is authenticated,
        and the message content is displayed to the user.
        (This uses authorization, list_matching_messages and get_msg modules)
        
        """
        if (self.key == '1'):
            while 1:
                try:
                    self.file = input("Please enter the file location:\n")
                    self.service = authorization(self.SCOPES)
                    self.message = create_message(self.sender, self.recipient,
                                                  self.subject, self.content,
                                                  self.file)
                    send_message(self.service, self.user_id, self.message)
                    break
                except:
                    print('File not found! Please enter the correct location')

        elif (self.key == '2'):
            self.query = input(
                "Please enter the query you would like to search: \n")
            self.service = authorization(self.SCOPES)
            list_matching_messages(self.service, self.user_id, self.query)

        else:
            print("Wrong input! Try again by choosing either 1 or 2")
示例#2
0
    def on_enter_state2(self, event):
        print("I'm entering state2")

        #print(event['message']['text'])
        #send_message(sender_id, event['message']['text'])
        global prices
        global items
        global urls
        global img_urls
        global describe

        #print(items)
        #print(prices)
        #print(urls)
        #for item in items:
        global state2_flag
        global searchTarget
        print('flaggggggggggggggggggggg' + str(state2_flag))
        if state2_flag == False:
            sender_id = event['sender']['id']
            searchTarget = event['message']['text']
            items, prices, urls, img_urls, describe = search(
                event['message']['text'])
            print('ffffffffffffffffffffff' + str(state2_flag))
            l = len(items)
            s = '共' + str(l) + '項商品'
            send_message(sender_id, s)
示例#3
0
    def on_enter_state6(self, event):
        print("I'm entering state6")

        global searchTarget

        sender_id = event['sender']['id']
        url = firstPage(searchTarget)
        send_message(sender_id, 'Demo')

        self.go_back_to_state2('back state2')
示例#4
0
    def on_enter_state4(self, event):
        print("I'm entering state4")

        global min_target
        global img_urls

        sender_id = event['sender']['id']
        if min_target != []:
            for every in min_target:
                send_imgurl(sender_id, img_urls[every])
        else:
            send_message(sender_id, '不好意思 目前尚未建立商品列表')
            send_message(sender_id, '請先輸入欲搜尋商品')

        self.go_back_to_state2('back state2')
示例#5
0
    def on_enter_state1(self, event):
        print("I'm entering state1")

        global state2_flag
        global prices
        global items
        global urls
        global img_urls
        global describe
        state2_flag = False
        prices = []
        items = []
        urls = []
        img_urls = []
        describe = []

        sender_id = event['sender']['id']
        send_message(sender_id, "請輸入商品名稱")
def start_application():
    print   ' Your details : - ' \
            '\n Name : ' + spy.name +\
            '\n Age : ' + str(spy.age) +\
            '\n Rating : ' + str(spy.rating) +\
            '\n Is online : ' + str(spy.is_online)
    raw = raw_input("Press enter to continue .........")
    status = True
    while status:
        print   '\n\nMenu  Select one of the following '\
                '\n1.Add a status update' \
                '\n2.Add a friend' \
                '\n3.Send a Secret message' \
                '\n4.Read a secret message' \
                '\n5.Read chats from user' \
                '\n6.Close Application'
        choice = raw_input('Enter your choice :- ')
        choice_validation = '^[1-6]$'
        if (re.match(choice_validation, choice) != None):
            choice = int(choice)
            if choice == 1:
                #add a status section
                spy_status()
            elif choice == 2:
                #add a friend
                add_friend()
            elif choice == 3:
                #Send a secret message
                send_message()
            elif choice == 4:
                #Read a secret message
                read_message()
            elif choice == 5:
                #Read chat form use
                read_all()
            elif choice == 6:
                #close the application

                status = False
        else:
            print Fore.RED + 'You have entered a wrong choice \n'
            raw = raw_input("Press any key to continue ..........")
示例#7
0
    def on_enter_state3(self, event):
        print("I'm entering state3")

        global prices
        global items
        global urls
        global describe
        global min_target

        print(prices)
        min_target = min_sort(prices)
        print(min_target)
        sender_id = event['sender']['id']
        for every in min_target:
            send_message(sender_id, items[every])
            send_message(sender_id, prices[every])
            send_message(sender_id, urls[every])
            send_message(sender_id, describe[every])
        self.go_back_to_state2('back state2')
示例#8
0
def webhook():

    # endpoint for processing incoming messaging events

    global thread_flag   #only run this thread one time
    global user_dict
    if not thread_flag :
        threading.Thread(target = check_user_status, args = (), name = 'check_thread').start()
        thread_flag = True


    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"]:
            if "messaging" in 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
                        if "text" in messaging_event["message"] :
                            message_text = messaging_event["message"]["text"]  # the message's text
                            message_text = message_text.encode('utf-8').lower()

                            # dorm internet workflow
                            if "quick_reply" in messaging_event["message"] :
                                payload = messaging_event["message"]["quick_reply"]["payload"]
                                if payload == 'GOT_IT' :
                                    send_message( sender_id, '很高興能為你幫上忙🙂' )
                                elif payload == 'ROLL_BACK' :
                                    faq = template_json.Template_json(sender_id,template_type=2,
                                          text="是否曾申請過帳號呢? (請用是/否按扭回答以便記錄)", payload_yes = "START_STATE_YES", payload_no = "START_STATE_NO" )
                                    send_template_message( faq )
                                else :
                                    reply = set_temp(payload, sender_id)
                                    send_template_message( reply )

                            else :
                                reply = handle_message( message_text, sender_id )

                                for key in user_dict.keys() :
                                    print(key)
                                    print(user_dict[key])

                                if not sender_id in user_dict : # not in time interval
                                    #暫時拿掉限制
                                    #if reply == '抱歉> < 我還無法處理這個問題,請您等待專人為您回答🙂 ' : user_dict[sender_id] = time.time() #使用者待專人回答, chatbot對該使用者暫停
                                    if type(reply) == str :
                                        send_message( sender_id, reply )
                                    else : #template
                                        send_template_message(reply)
                                pass

                    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
                        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["postback"]["payload"]  # the message's text
                        message_text = message_text.encode('utf-8').lower()
                        reply = handle_message( message_text, sender_id )
                        if not sender_id in user_dict : # not in time interval
                            user_dict[sender_id] = time.time()
                            send_message( sender_id, reply )

    return "ok", 200