2017-01-10 12 views
0

私はslackbotを構築しており、slackからsmsメッセージを受信しようとしています。33行目の構文に問題があります

File "twiliobot.py", line 33 response_message = username " in " channel " says: " text

:ここに私のpythonコード

import os 
from flask import Flask, request, Response 
from slackclient import SlackClient 
from twilio import twiml 
from twilio.rest import TwilioRestClient 

SLACK_WEBHOOK_SECRET = os.environ.get('SLACK_WEBHOOK_SECRET', None) 
WILIO_NUMBER = os.environ.get('TWILIO_NUMBER', None) 
USER_NUMBER = os.environ.get('USER_NUMBER', None) 

app = Flask(__name__) 
slack_client = SlackClient(os.environ.get('SLACK_TOKEN', None)) 
twilio_client = TwilioRestClient() 


@app.route('/twilio', methods=['POST']) 
def twilio_post(): 
    response = twiml.Response() 
    if request.form['From'] == USER_NUMBER: 
     message = request.form['Body'] 
     slack_client.api_call("chat.postMessage", channel="#general", 
       text=message, username='twiliobot', 
       icon_emoji=':robot_face:') 
    return Response(response.toxml(), mimetype="text/xml"), 200 


@app.route('/slack', methods=['POST']) 
def slack_post(): 
    if request.form['token'] == SLACK_WEBHOOK_SECRET: 
     channel = request.form['channel_name'] 
     username = request.form['user_name'] 
     text = request.form['text'] 
     response_message = username " in " channel " says: " text 
     twilio_client.messages.create(to=USER_NUMBER, from_=TWILIO_NUMBER, 
            body=response_message) 
return Response(), 200 


@app.route('/', methods=['GET']) 
def test(): 
    return Response('It works!') 


if __name__ == '__main__': 
    app.run(debug=True) 
それはアプリの名前であるので、私は、「Pythonのtwiliobot.py」でこのコードを実行してみてください

enter image description here

があり、それがこのエラーを返します。

enter image description here 私はここで間違っていますか?私の構文が間違っていますか?ここで

+0

あなたは、文字列に参加しようとしています

response_message = username + " in " + channel + " says: " + text 

それともjoin

response_message = ' '.join([username, "in", channel, "says:",text] 

または+演算子を使用しますか?それらの間に '+ '記号を追加してください。 – wim

+1

実際のエラーメッセージが表示されませんでした。 –

+0

また、コードの一部が間違ってインデントされています。 ( 'slack_post'のreturn文) –

答えて

3

は、私はあなたがテキストを連結しようとしていると仮定し、質問

response_message = username " in " channel " says: " text 

の行です。 format

response_message = '{} in {} says: {}'.format(username, channel, text) 
+0

実際、これは完全に機能します。コードが与えられたので、何が起こっているのか完全にはわからなかった。スーパーシンプル、私はそれを逃したと信じることはできません。 –

関連する問題