nyg*_*owl 2 python unicode sms twilio emoji
我在Python中构建了一个与Twilio API一起使用的测验游戏,因此我可以通过短信将游戏运行到我的Twilio号码.我正在试图弄清楚如何传递一个响应,它会显示标准文本和表情符号,当它被发送电话收到时.
我一直致力于在utf-8中理解unicode与ascii字符集以及编码和解码.我上次登陆的地方是以下代码,它只是像手机上的常规字符串一样打印出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))
Run Code Online (Sandbox Code Playgroud)
更新:部分问题是将sms方法应用于消息变量,将其转换为XML格式,但不保留unicode代码点.应用messages.create方法捕获并保留unicode代码点,以便移动设备可以解释它.
对于修复,我将Rob的建议主要应用于代码的最后两行,方法是将response.sms替换为client.messages.create并将消息变量传递给body参数.我还将u for unicode应用于分配给消息变量的所有字符串以及向消息添加了表情符号代码点/图像.这些使发短信的表情符号起作用.如果您想查看更新,请查看:https://github.com/nyghtowl/Twilio_Quiz
小智 5
关于如何使用Twilio Python模块发送表情符号的好问题.也喜欢战争游戏参考.想想我知道这里发生了什么.
发光星形符号的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")
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!
| 归档时间: |
|
| 查看次数: |
3606 次 |
| 最近记录: |