Ejemplo n.º 1
0
    def post(self):
        str_to_reply = ''
        urlfetch.set_default_fetch_deadline(60)
        body = json.loads(self.request.body)
        request_body = body
        logging.info('request body:')
        logging.info(body)
        self.response.write(json.dumps(body))

        update_id = body['update_id']
        try:
            message = body['message']
        except:
            message = body['edited_message']
        message_id = message.get('message_id')
        date = message.get('date')
        text = message.get('text')
        fr = message.get('from')
        name = fr.get('first_name')
        chat = message['chat']
        chat_id = chat['id']
        logging.info(chat_id)

        if not text:
            logging.info('no text')
            return

        def reply(msg=None, img=None):
            logging.info(msg)
            if msg:
                resp = urllib2.urlopen(
                    BASE_URL + 'sendMessage',
                    urllib.urlencode({
                        'chat_id': str(chat_id),
                        'text': msg.encode('utf-8'),
                        'disable_web_page_preview': 'true',
                        'reply_to_message_id': str(message_id),
                    })).read()
            elif img:
                resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                    ('chat_id', str(chat_id)),
                    ('reply_to_message_id', str(message_id)),
                ], [
                    ('photo', 'image.jpg', img),
                ])
            else:
                logging.error('no msg or img specified')
                resp = None

            logging.info('send response:')
            logging.info(resp)

        cmd, params = myutils.split_text(text)
        params_encoded = [x.encode('utf-8') for x in params]

        replies = myutils.handle_message(commander, cmd, request_body, params)

        for r in replies:
            reply(r)
Ejemplo n.º 2
0
 def test_handle_message_specific(self):
     cmndr = Commander()
     coffeestore.registerCoffeeCommands(cmndr)
     cmd, params = myutils.split_text(u'/coffeeupd 2')
     replies = myutils.handle_message(
         cmndr, cmd,
         {'message': {
             'date': 1542830400,
             'from': {
                 'first_name': 'igor'
             }
         }}, params)
     assert replies[0] == 'igor, the coffee amount was updated.'
Ejemplo n.º 3
0
 def test_handle_message_other(self):
     cmndr = Commander()
     coffeestore.registerCoffeeCommands(cmndr)
     cmd, params = myutils.split_text(u'\u2615\ufe0f \u2615\ufe0f')
     replies = myutils.handle_message(
         cmndr, cmd,
         {'message': {
             'date': 1542830400,
             'from': {
                 'first_name': 'igor'
             }
         }}, params)
     assert 'igor drank 2 coffee out of 3' in replies[0]
Ejemplo n.º 4
0
 def test_split_text(self):
     text = "/hello  igor     basko"
     cmd, params = myutils.split_text(text)
     assert cmd == '/hello'
     assert params == ['igor', 'basko']
Ejemplo n.º 5
0
 def test_split_text_emoji(self):
     text = u'/addfood \U0001f33d 100'
     cmd, params = myutils.split_text(text)
     assert cmd == u'/addfood'
     assert params == [u'\U0001f33d', u'100']