简体   繁体   English

第33行的语法有问题

[英]Having trouble with the syntax in line 33

I am building a slackbot, and am trying to receive sms messages from slack. 我正在构建一个slackbot,并试图从slack接收短信。 Here is my python code 这是我的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)

在此处输入图片说明

When I try running this code with "python twiliobot.py", because it is the name of the app, it returns this error: 当我尝试使用“ python twiliobot.py”运行此代码时,因为它是应用程序的名称,它将返回以下错误:

File "twiliobot.py", line 33 response_message = username " in " channel " says: " text 文件“ twiliobot.py”,第33行response_message = username在“ channel”中说:“ text

在此处输入图片说明 What am I doing wrong here? 我在这里做错了什么? What is wrong with my syntax? 我的语法有什么问题?

Here's the line in question 这是有问题的线

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

I assume you're trying to concatenate text. 我认为您正在尝试连接文本。 Use the + operator 使用+运算符

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

Or join join

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

or format format

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM