title: How to send a Twilio sms response with emoji output
body: <p>I built a quiz game in Python that works with the Twilio API so I can run the game through sms to my Twilio number. I'm trying to figure out how to pass a response that will show standard text and an emoji when its received by the sending phone. </p>
  
  <p>I've been working on understanding unicode vs. ascii character set and encoding and decoding in utf-8. Where I last landed is the following code which just prints out the unicode code point like a regular string on the mobile phone. The gap appears to be how to isolate and pass a code point that the phone can interpret. Any ideas or pointers on how to do this? Or is there a different approach someone would recommend? </p>
  
  <p>The current state of the code is as follows:</p>
  
  <pre><code># -*- 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'] &gt; 3:
          session.pop(from_number, None)
          session['counter'] = 0
  
  
      response.sms(message)
  
      return Response(str(response))
  </code></pre>
  
  <p>UPDATE: Part of the problem was applying the sms method to the message variable turned it into XML format which didn't preserve the unicode code point. Applying messages.create method captured and preserved the unicode code point so it could be interpreted by the mobile device. </p>
  
  <p>For the fix, I applied Rob's recommendation primarily to the last two lines of the code by replacing response.sms and the return with the client.messages.create and passing in the message variable to the body parameter. I also applied the u for unicode to all strings assigned to the message variable as well as added emoji code points/images to the messages. These made texting emoji work. If you want to see the update, checkout: <a href="https://github.com/nyghtowl/Twilio_Quiz" rel="nofollow">https://github.com/nyghtowl/Twilio_Quiz</a></p>