2017-08-07 33 views
0

我從Bot-Father創建一個bot,並在C#的控制檯應用程序格式中編寫一個簡單的電報bot。我的機器人代碼:Telegram Bot不能通過放入服務器和使用webhook工作

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using NetTelegramBotApi; 
using NetTelegramBotApi.Requests; 
using NetTelegramBotApi.Types; 

namespace vinehTest 
{ 
    class Program 
    { 
    private static string token = " "; ---> a token from Bot-Father 
    private static ReplyKeyboardMarkup mainMenue; 
    private static ReplyKeyboardMarkup phoneMenue; 

    static void Main(string[] args) 
    { 
     mainMenue = new ReplyKeyboardMarkup 
     { 
      Keyboard = new KeyboardButton[][] {new KeyboardButton[] { new KeyboardButton("درباره ی ما"), new KeyboardButton("شماره تلفن ها ") }, 

                     new KeyboardButton[] { new KeyboardButton("آخرین اخبار") } 

               } 
     }; 

     phoneMenue = new ReplyKeyboardMarkup 
     { 
      Keyboard = new KeyboardButton[][] { new KeyboardButton[] { new KeyboardButton("ریاست"), new KeyboardButton("بازاریاب "), new KeyboardButton("طراح") } , 
               new KeyboardButton[] { new KeyboardButton("بازگشت به منوی اصلی") } 
               } 
     }; 

     Task.Run(() => RunBot()); 
     Console.ReadLine(); 
    } 

    public static async Task RunBot() 
    { 

     var bot = new TelegramBot(token); 
     var me = await bot.MakeRequestAsync(new GetMe()); 
     Console.WriteLine(me.Username); 
     long offset = 0; 
     int whileCount = 0; 

     while (true) 
     { 
      Console.WriteLine("While is {0}", whileCount); 
      whileCount += 1; 
      var updates = await bot.MakeRequestAsync(new GetUpdates() { Offset = offset }); 
      Console.WriteLine("update count is {0}", updates.Count()); 
      Console.WriteLine("-----------------------------------------"); 
      try 
      { 
       foreach (var update in updates) 
       { 
        offset = update.UpdateId + 1; 
        var text = update.Message.Text; 
        if (text == "/start") 
        { 
         var req = new SendMessage(update.Message.Chat.Id, "گزینه مورد نظر را وارد کنید") { ReplyMarkup = mainMenue }; 
         await bot.MakeRequestAsync(req); 
         continue; 
        } 
        else if (text != null && text.Contains("درباره ی ما")) 
        { 
         var req = new SendMessage(update.Message.Chat.Id, "این شرکت در حوزه های تبلیغاتی فعال است") { ReplyMarkup = mainMenue }; 
         await bot.MakeRequestAsync(req); 
         continue; 
        } 
        else if (text != null && text.Contains("شماره تلفن ها")) 
        { 
         var req = new SendMessage(update.Message.Chat.Id, "یکی ارز گزینه ها را انتخاب کنید") { ReplyMarkup = phoneMenue }; 
         await bot.MakeRequestAsync(req); 
         continue; 
        } 
        else if (text != null && text.Contains("ریاست")) 
        { 
         using(var stream = System.IO.File.Open("C://Users//sahar//Desktop//Pic//1.jpg",System.IO.FileMode.Open)) 
         { 
          var req = new SendPhoto(update.Message.Chat.Id, new FileToSend(stream,"1.jpg")); 
          await bot.MakeRequestAsync(req); 
          continue; 
         } 
        } 
        else if (text != null && text.Contains("بازاریاب")) 
        { 
         var req = new SendMessage(update.Message.Chat.Id, "لطفاً کمی صبر کنید") { ReplyMarkup = phoneMenue }; 
         await bot.MakeRequestAsync(req); 
         using (var stream = System.IO.File.Open("C://Users//sahar//Desktop//video//2.mp4", System.IO.FileMode.Open)) 
         { 
          var req2 = new SendVideo(update.Message.Chat.Id, new FileToSend(stream, "2.mp4")); 
          await bot.MakeRequestAsync(req2); 
          continue; 
         } 
        } 
        else if (text != null && text.Contains("بازگشت به منوی اصلی")) 
        { 
         var req = new SendMessage(update.Message.Chat.Id, "امیدواریم توانسته باشیم شما را یاری کنیم") { ReplyMarkup = mainMenue }; 
         await bot.MakeRequestAsync(req); 
         continue; 
        } 
        else 
        { 
         var req = new SendMessage(update.Message.Chat.Id, "دستوری که فرستادید مفهوم نبود") { ReplyMarkup = mainMenue }; 
         await bot.MakeRequestAsync(req); 
         continue; 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       throw e; 
      } 
     } 
    } 
} 
} 

當我運行在Visual Studio 2012的代碼,我的機器人做工精細。但我需要把我的機器人放在真正的服務器上。所以我把Newtonsoft.json.dll和NetTelegramBotApi.dll和myBot.exe從C#項目在我的服務器,並使用網絡掛接設置我的機器人的令牌主機地址,結果是:

{"ok":true,"result":true,"description":"Webhook was set"} 

但我不要博特沒有工作。

回答

0

您可以通過下面的方法找出問題:

檢查getWebhookInfo方法,確保您的網絡掛接網址是正確的,並沒有last_error_message領域。

將類似數據發佈到您的服務器,here是您可以在curl -d JSON中使用的一些數據,只需將其複製並在您自己的服務器上運行即可。

順便說一句,檢查您的CDN配置(如果您已經應用在該服務器上),臨時禁用氾濫或任何檢查。