परिचय: C++ में चैटबॉट बनाना कोई पागलपन नहीं है
\n\nजब चैटबॉट बनाने की बात आती है, तो आमतौर पर Python, JavaScript या Node.js का ख्याल आता है। लेकिन क्या होगा अगर मैं आपसे कहूँ कि C++ इस काम के लिए उतना ही प्रभावी टूल हो सकता है? हाँ, वही C++ जो ड्राइवर, गेम और हाई-लोड सिस्टम से जुड़ा है।
\n\nअसल में, C++ में चैटबॉट एक बेहतरीन समाधान है जब:
\n- \n
- आपको अधिकतम परफॉरमेंस चाहिए (प्रति सेकंड हजारों रिक्वेस्ट प्रोसेस करना); \n
- आप एम्बेडेड सिस्टम या माइक्रोकंट्रोलर पर काम कर रहे हैं; \n
- आप मेमोरी खपत को कम करना चाहते हैं; \n
- आपको C++ पसंद है और कुछ नया आज़माना चाहते हैं। \n
इस व्यावहारिक गाइड में, हम शुरू से एक पूर्ण चैटबॉट बनाएंगे। हम वास्तविक लाइब्रेरी का उपयोग करेंगे, काम करने वाला कोड लिखेंगे, और ऐसी आर्किटेक्चर समझेंगे जो आपके बॉट को Telegram API के ज़रिए संवाद करने देगी। लेख के अंत तक, आपके पास सिर्फ थ्योरी नहीं, बल्कि एक तैयार प्रोजेक्ट होगा जिसे चलाया और विस्तारित किया जा सकता है।
\n\nहमें क्या चाहिए: टूलकिट
\n\nकोड लिखने से पहले, आइए टूल्स को समझें। C++ में चैटबॉट बनाने के लिए हमें चाहिए:
\n\nलाइब्रेरी
\n- \n
- libcurl — HTTP रिक्वेस्ट के लिए (Telegram API से डेटा भेजना और प्राप्त करना); \n
- nlohmann/json — JSON के साथ काम करने के लिए हल्की लाइब्रेरी (सर्वर से रिस्पॉन्स पार्स करना); \n
- Boost.Asio (वैकल्पिक) — नेटवर्क के साथ एसिंक्रोनस काम करने के लिए; \n
- Threading support (std::thread) — एक साथ कई रिक्वेस्ट प्रोसेस करने के लिए। \n
इंस्टॉलेशन (Linux / macOS)
\nsudo apt-get install libcurl4-openssl-dev # Linux के लिए\nbrew install curl # macOS के लिए\nnlohmann/json लाइब्रेरी को header-only के रूप में जोड़ा जा सकता है (बस json.hpp डाउनलोड करें और प्रोजेक्ट में रखें)।
प्रोजेक्ट संरचना
\ntelegram_bot/\n├── main.cpp\n├── bot.hpp\n├── bot.cpp\n├── json.hpp\n└── Makefile (या CMakeLists.txt)\n\nचरण 1: बॉट का ढांचा — Telegram API के साथ काम
\n\nTelegram Bot API एक RESTful API है। हमें बस इतना करना है कि https://api.telegram.org/bot<TOKEN>/METHOD पर HTTP रिक्वेस्ट भेजें। सबसे महत्वपूर्ण मेथड हैं getUpdates (नए मैसेज प्राप्त करना) और sendMessage (जवाब भेजना)।
चलिए एक TelegramBot क्लास बनाते हैं जो सभी संचार को संभालेगा।
bot.hpp — हेडर फ़ाइल
\n#ifndef BOT_HPP\n#define BOT_HPP\n\n#include <string>\n#include <vector>\n#include <nlohmann/json.hpp>\n\nusing json = nlohmann::json;\n\nclass TelegramBot {\npublic:\n explicit TelegramBot(const std::string& token);\n \n // नए मैसेज की सूची प्राप्त करें (long polling)\n std::vector<json> getUpdates(long offset = 0);\n \n // टेक्स्ट मैसेज भेजें\n bool sendMessage(long chat_id, const std::string& text);\n \n // एक इनकमिंग मैसेज प्रोसेस करें\n std::string handleMessage(const json& message);\n\nprivate:\n std::string api_url_;\n std::string httpGet(const std::string& url);\n std::string httpPost(const std::string& url, const std::string& post_data);\n};\n\n#endif // BOT_HPP\n\nbot.cpp — इम्प्लीमेंटेशन
\n#include "bot.hpp"\n#include <curl/curl.h>\n#include <sstream>\n\n// curl रिस्पॉन्स को स्ट्रिंग में लिखने के लिए कॉलबैक\nstatic size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {\n ((std::string*)userp)->append((char*)contents, size * nmemb);\n return size * nmemb;\n}\n\nTelegramBot::TelegramBot(const std::string& token) \n : api_url_("https://api.telegram.org/bot" + token + "/") {}\n\nstd::string TelegramBot::httpGet(const std::