/** * Dependencies: * Dotenv, Express, BodyParser, Slack Web Client, Slack Events API */ require('dotenv').config(); const express = require('express'); const bodyParser = require('body-parser'); const createSlackEventAdapter = require('@slack/events-api').createSlackEventAdapter; //a new export, ErrorCode, is a dictionary of known error types const { WebClient } = require('@slack/client'); const twilio = require('twilio'); const firebase = require('firebase'); // Creates our express app const app = express(); // The channel we'll send TalkBot messages to const channel = 'CBY5883L3'; // The port we'll be using for our Express server const PORT = 3000; // Use BodyParser for app app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); /** * Tokens: * Slack, Firebase, Twilio */ //Retrieve bot token from dotenv file const bot_token = process.env.SLACK_BOT_TOKEN || ''; // Authorization token const auth_token = process.env.SLACK_AUTH_TOKEN || ''; // Verification token for Events Adapter const slackEvents = createSlackEventAdapter(process.env.SLACK_VERIFICATION_TOKEN); //Slack web client const web = new WebClient(auth_token); const bot = new WebClient(bot_token); //Twilio tokens const twilio_sid = process.env.TWILIO_SID || ''; const twilio_auth = process.env.TWILIO_AUTH_TOKEN || ''; // Initialize Twilio client using our Twilio SID and Authorization token const twilioClient = twilio(twilio_sid, twilio_auth); app.listen(PORT, function (err) { if (err) { throw err } console.log('Server started on port 3000') }) // Handles incoming SMS to Twilio number app.post('/sms', function (req, res) { const body = req.body.Body const num = req.body.From // Sends message to Slack - in format from { // `res` contains information about the posted message console.log('Message sent: ', res.ts); }) .catch(console.error); }
答案 0 :(得分:0)
您收到此错误
错误-11200 HTTP检索失败
因为Twilio在向您的/sms
端点发出POST请求时会收到响应。
响应必须是有效的TwiML
(XML)。
更改代码:
// Handles incoming SMS to Twilio number
app.post('/sms', function (req, res) {
const body = req.body.Body
const num = req.body.From
// Sends message to Slack - in format from {
// `res` contains information about the posted message
console.log('Message sent: ', res.ts);
})
.catch(console.error);
}
对此:
// Handles incoming SMS to Twilio number
app.post('/sms', function (req, res) {
const body = req.body.Body
const num = req.body.From
// Sends message to Slack - in format from {
// `res` contains information about the posted message
const twiml = new twilio.twiml.MessagingResponse();
twiml.message('Your SMS was forwarded to Slack...');
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(twiml.toString());
});
如果不想回复发件人,则只需注释掉此行
// twiml.message('Your SMS was forwarded to Slack...');
我希望这会有所帮助。