簡體   English   中英

如何發送帶有表情符號輸出的Twilio SMS響應

[英]How to send a Twilio sms response with emoji output

我使用Python構建了一個可以與Twilio API一起使用的測驗游戲,因此可以通過短信將游戲運行到我的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))

更新:問題的一部分是將sms方法應用於消息變量,從而將其轉換為XML格式,該格式未保留unicode代碼點。 應用messages.create方法可捕獲並保留Unicode代碼點,以便移動設備可以對其進行解釋。

對於此修復程序,我通過將response.sms和return替換為client.messages.create並將message變量傳遞給body參數,主要將Rob的建議應用於代碼的最后兩行。 我還將unicode的u應用於分配給message變量的所有字符串,以及為消息添加了emoji代碼點/圖像。 這些使發短信表情符號起作用。 如果要查看更新,請簽出: https : //github.com/nyghtowl/Twilio_Quiz

有關如何使用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")

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM