मार्केट सेंटीमेंट का विश्लेषण करने के लिए Node.js Twitter बॉट बनाना
![](https://post.nghiatu.com/assets/images/m/max/724/1*D2DD8qN92lSF2JSvlNQWbQ.png)
आइए जानें कि एक Node.js-आधारित ट्विटर बॉट कैसे बनाया जाता है जो ट्वीट्स को स्ट्रीम करके और फ़िल्टर लागू करके बाजार की भावना का विश्लेषण करता है!
परिचय
राय, समाचार और विचारों को साझा करने के लिए ट्विटर एक लोकप्रिय मंच रहा है। यह भावना विश्लेषण करने के लिए मूल्यवान डेटा का स्रोत भी हो सकता है, विशेष रूप से जब यह विभिन्न क्रिप्टोकरेंसी और वित्तीय साधनों के लिए बाजार की भावना को समझने की बात आती है।
इस ट्यूटोरियल में, हम आपको नोड.जेएस-आधारित ट्विटर बॉट बनाने की प्रक्रिया के बारे में बताएंगे जो ट्वीट्स को स्ट्रीम करता है और उन्हें हैशटैग और मुद्रा प्रतीकों (जैसे, #defi या $BTC) की सूची के आधार पर फ़िल्टर करता है। बॉट भावना विश्लेषण एपीआई का उपयोग करके ट्वीट्स की भावना का विश्लेषण करेगा।
आवश्यक शर्तें
- Twitter API कुंजियों तक पहुंच वाला एक Twitter डेवलपर खाता
- Node.js (>=14.x) स्थापित
चरण-दर-चरण मार्गदर्शिका
चरण 1: अपनी परियोजना की स्थापना
- अपने बॉट के लिए एक नई निर्देशिका बनाएँ:
mkdir twitter-market-sentiment-bot
cd twitter-market-sentiment-bot
npm init - yes
npm install axios
चरण 3: ट्विटर एपीआई की स्थापना
- अपनी प्रोजेक्ट निर्देशिका में, `twitter-api.js` नाम से एक फ़ाइल बनाएँ
2. फ़ाइल में निम्न कोड डालें:
const axios = require('axios');
const api_key = 'YOUR_API_KEY';
const api_secret_key = 'YOUR_SECRET_KEY';
async function getBearerToken() {
const headers = {
'Authorization': `Basic ${Buffer.from(`${api_key}:${api_secret_key}`).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
};
const response = await axios.post('https://api.twitter.com/oauth2/token', 'grant_type=client_credentials', { headers });
return response.data.access_token;
}
module.exports = {
getBearerToken,
};
- प्रोजेक्ट डायरेक्टरी में, `market-sentiment.js` नाम की एक फ़ाइल बनाएँ
2. फ़ाइल में निम्न कोड डालें:
const axios = require('axios');
async function analyzeSentiment(text) {
try {
// Replace the below URL with a sentiment analysis API of your choice
const sentimentApiUrl = 'https://sentim-api.sample.com/api/v2.0/';
const response = await axios.post(sentimentApiUrl, { text });
return response.data;
} catch (error) {
console.error(`Error analyzing sentiment: ${error}`);
}
}
module.exports = analyzeSentiment;
- प्रोजेक्ट डायरेक्टरी में, `index.js` नाम की एक फ़ाइल बनाएँ
2. फ़ाइल में निम्न कोड डालें: - बॉट शुरू करें (इस कमांड को अपने बैश टर्मिनल में चलाएं):
const { getBearerToken } = require('./twitter-api');
const analyzeSentiment = require('./market-sentiment');
const axios = require('axios');
const filters = [
'#defi',
'$BTC',
// Add more filters here
];
async function processStream(stream) {
for await (const chunk of stream) {
try {
const text = JSON.parse(chunk.toString()).data.text;
const sentiment = await analyzeSentiment(text);
console.log(`Text: ${text}\nSentiment: ${sentiment}\n\n`);
} catch (error) {
console.error(`Error processing stream data: ${error}`);
}
}
}
(async () => {
const token = await getBearerToken();
const url = 'https://api.twitter.com/2/tweets/search/stream';
const headers = { 'Authorization': `Bearer ${token}` };
// Use filtered-stream API
const rules = filters.map(filter => ({ "value": filter }));
await axios.post(`${url}/rules`, { "add": rules }, { headers });
// Start processing
const stream = await axios({ url, headers, responseType: 'stream' });
processStream(stream.data); })();
node index.js
साइडकिक टीम और डेवलपर्स से जुड़े रहने के लिए हमारे सोशल में शामिल हों!