Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <tgbot/tgbot.h>
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <curl/curl.h>
  8. #include <exception>
  9. #include <signal.h>
  10. using namespace std;
  11. using namespace TgBot;
  12. bool sigintGot = false;
  13. static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
  14. {
  15.   size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  16.   return written;
  17. }
  18. void image(string link)//link converter
  19. {
  20. CURL *curl;
  21. CURLcode res;
  22. static const char *pagefilename = "example.mp4";
  23.   FILE *pagefile;
  24. curl_global_init(CURL_GLOBAL_DEFAULT);
  25. curl = curl_easy_init();
  26. if(curl)
  27. {
  28.   curl_easy_setopt(curl, CURLOPT_URL, link.c_str());
  29. #ifdef SKIP_PEER_VERIFICATION
  30.   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  31. #endif
  32. #ifdef SKIP_HOSTNAME_VERIFICATION
  33.   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  34. #endif
  35. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  36. pagefile = fopen(pagefilename, "wb");
  37.     if(pagefile)
  38.       {
  39.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);
  40.         curl_easy_perform(curl);
  41.         fclose(pagefile);
  42.       }
  43.   }
  44. curl_global_cleanup();
  45. }
  46. int main() {
  47.   bool i=false;
  48.   string test;
  49.   string mp4="mp4";
  50.   const string videoFilePath = "example.mp4";
  51.     const string videoMimeType = "video/mp4";
  52.    TgBot::Bot bot("INSERT HERE YOUR TOKEN");
  53.    bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
  54.           bot.getApi().sendMessage(message->chat->id, "I'm a bot!");
  55.    });
  56.     bot.getEvents().onAnyMessage([&bot, &i, &videoFilePath, &videoMimeType,&test,&mp4](TgBot::Message::Ptr message) {
  57.         printf("User wrote %s\n", message->text.c_str());
  58.         if (StringTools::startsWith(message->text, "/start")) {
  59.             return;
  60.         }
  61.         if(i==true)
  62.         {
  63.         test=message->text;
  64.         if(test.find(mp4)!=std::string::npos)
  65.         {
  66.         image(test);
  67.         bot.getApi().sendVideo(message->chat->id, InputFile::fromFile(videoFilePath, videoMimeType));
  68.         }else
  69.           {
  70.             bot.getApi().sendMessage(message->chat->id, "Error mp4 file does not exist");
  71.           }
  72.           i=false;
  73.         }
  74.         if(message->text=="/convert")
  75.         {
  76.           bot.getApi().sendMessage(message->chat->id, "Write here the link to convert:");
  77.           i=true;
  78.         }
  79.         cout<<message->chat->id<<"\n";
  80.        
  81.         });
  82.     remove("example.mp4");
  83.     signal(SIGINT, [](int s) {
  84.         printf("SIGINT got");
  85.         sigintGot = true;
  86.     });
  87.     try {
  88.         printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
  89.         TgLongPoll longPoll(bot);
  90.         while (!sigintGot) {
  91.             printf("Long poll started\n");
  92.             longPoll.start();
  93.         }
  94.     } catch (exception& e) {
  95.         printf("error: %s\n", e.what());
  96.     }
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement