2013-10-13 26 views
2

我在Python中構建了一個與Twilio API配合使用的問答遊戲,因此我可以通過短信向我的Twilio號碼運行遊戲。我試圖弄清楚如何通過一個響應來顯示標準文本和表情符號,當它通過發送電話收到。如何發送帶有表情符號輸出的Twilio短信響應

我一直在努力理解unicode與ascii字符集以及utf-8中的編碼和解碼。我最後登陸的地方是下面的代碼,它只是在手機上打印出一個普通字符串的unicode代碼點。差距似乎是如何隔離和傳遞手機可以解釋的代碼點。任何想法或指示如何做到這一點?或者有人會推薦一種不同的方法?

代碼的當前狀態是如下:

# -*- coding: utf-8 -*- 

from flask import render_template, flash, redirect, session, url_for, request, jsonify, g 
from flask import Response 
from app import app 
from config import TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN 
from twilio import twiml 
from twilio.rest import TwilioRestClient 
import re, random 

client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) 


def simplify_txt(submitted_txt): 
    response_letters = re.sub(r'\W+', '', submitted_txt) 
    return response_letters.lower() 

@app.route("/quiz_game") 
def quiz_game(): 

    response = twiml.Response() 

    from_number = str(request.values.get('From', None)) 
    body = request.values.get('Body', None) 
    simplify_body = simplify_txt(body) 

    questions = { 
      0: "What word is shorter when you add two letters to it?", 
      1: "If I drink, I die. If i eat, I am fine. What am I?", 
      2: "What kind of tree is carried in your hand?", 
      3: "Thanks for playing.", 
      4: "" 
    } 

    # Stripped down answers to compare to text in case multiple word answer 
    simplify_answers = { 
      1:"short", 
      2:"fire", 
      3:"palm", 
      4:"" 
      } 

    # Pretty print answers 
    print_answers = { 
      1:"short", 
      2:"fire", 
      3:"palm", 
      4:"" 
      } 

    # if from_number not in track_user: 
    if from_number not in session: 
     session[from_number] = 0 
     counter = session.get('counter', 0) 
     counter += 1 
     session['counter'] = counter 
     message = "Shall we play a game? %s " % questions[0] 
    else: 
     game_round = session['counter'] 

     if simplify_answers[game_round] == simplify_body: 
      session[from_number] += 10 
      score = session[from_number] 

      message = "Correct Answer. You have %d points out of 30. %s" % (score, questions[game_round]) 

      message += unicode('u1f31f',"unicode_escape").encode('utf-8') 

     else: 
      score = session[from_number] 
      message = "Wrong answer. We were looking for %s. Your score is %d out of 30. %s" % (print_answers[game_round], score, questions[game_round]) 

     session['counter'] += 1 


    if session['counter'] > 3: 
     session.pop(from_number, None) 
     session['counter'] = 0 


    response.sms(message) 

    return Response(str(response)) 

UPDATE:問題的一部分是將所述SMS方法的消息變量成了其中未保留Unicode代碼點XML格式。應用messages.create方法捕獲並保存unicode代碼點,以便移動設備可以解釋它。

對於修復,我將Rob的建議主要應用於代碼的最後兩行,將client.messages.create替換爲response.sms和return,並將消息變量傳遞給body參數。我還將u應用於unicode以分配給消息變量的所有字符串,並向消息添加表情符號代碼點/圖像。這些使發短信表情符號工作。如果你想看到更新,結帳:https://github.com/nyghtowl/Twilio_Quiz

回答

5

偉大的問題,如何使用Twilio Python模塊發送表情符號。也喜歡Wargames的參考。想想我知道這裏發生了什麼。

發光的恆星符號的unicode值是U + 1F31F,它表示爲一個Python unicode字面值爲u"\U0001F31F"。沒有加號,它實際上是一個不同的代碼點,而不是發光的明星表情符號。

所以在這個特殊的例子送,你會做這樣的事情:

from twilio.rest import TwilioRestClient 

# Set Twilio credentials 
TWILIO_ACCOUNT_SID = "ACxxxxxxxxxxxxx" 
TWILIO_AUTH_TOKEN = "yyyyyyyyyyyyyyyy" 
TWILIO_NUMBER = "+15558675309" 

# Instantiate Twilio REST client 
client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) 

# Send a glowing star emoji 
client.messages.create(from_=TWILIO_NUMBER, to="+15556667777", body=u"Here is your tasty emoji: \U0001F31F") 

希望幫助!

+0

完全有幫助。謝謝。 – nyghtowl