D
D
doublench212017-05-29 20:53:45
C++ / C#
doublench21, 2017-05-29 20:53:45

Can't send message with TelegramAPI?

Due to the specifics of strings and different formats, I can't figure out why I can't send a POST request. Tried it in two different ways:

spoiler
void SendMessage(SYSTEMTIME localTime) {
      CURL* curl;
      CURLcode res;

      std::wstringstream fmt;
      fmt << ((localTime.wDay < 10) ? (_T("0")) : (_T(""))) << localTime.wDay << _T(".")
          << ((localTime.wMonth < 10) ? (_T("0")) : (_T(""))) << localTime.wMonth << _T(".") << localTime.wYear << _T(" ")
          << localTime.wHour << _T(":") << localTime.wMinute << std::endl;

      TCHAR* fields = NULL;
      fmt >> fields;

      curl = curl_easy_init();

      if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL,
                         "https://api.telegram.org/bot{MyToken}/sendMessage");
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, _tcscat_s(_T("chat_id={MyChatId}&text="), 23, fields));
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
      }
    }

so
spoiler
void SendMessage(SYSTEMTIME localTime) {
  CURL* curl;
  CURLcode res;

  std::stringstream fmt;
  fmt << ((localTime.wDay < 10) ? ("0") : ("")) << localTime.wDay << "." << ((localTime.wMonth < 10) ? ("0") : (""))
      << localTime.wMonth << "." << localTime.wYear << " " << localTime.wHour << ":" << localTime.wMinute << std::endl;

  char* fields = nullptr;
  fmt >> fields;

  curl = curl_easy_init();

  if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL,
                     "https://api.telegram.org/bot{MyToken}/sendMessage");
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strcat("chat_id={MyChatId}&text=", fields));
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
}

Can you point out an error? I would be very grateful.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question