【问题标题】:Telegram Bot don't work by putting into server and use of webhookTelegram Bot 不能通过放入服务器和使用 webhook 来工作
【发布时间】:2017-08-07 07:59:01
【问题描述】:

我从 Bot-Father 创建了一个机器人,并以 C# 的控制台应用程序格式编写了一个简单的电报机器人。我的机器人代码是:

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 中运行代码时,我的机器人工作正常。但我需要把我的机器人放在真正的服务器上。所以我将 C# 项目中的 Newtonsoft.json.dll 和 NetTelegramBotApi.dll 和 myBot.exe 放在我的服务器中,并使用 webhook 将我的机器人的令牌设置为主机地址,结果是:

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

但我的机器人不工作。

【问题讨论】:

    标签: c# webhooks telegram-bot


    【解决方案1】:

    您可以通过以下方法发现问题:

    检查getWebhookInfo方法,确保你的webhook URL是正确的,并且没有last_error_message字段。

    POST 类似的数据到你的服务器,here 是一些你可以在curl -d JSON 中使用的数据,只需复制它并在你自己的服务器上运行即可。

    顺便说一句,检查您的 CDN 配置(如果您已在该服务器上应用),暂时禁用泛洪或任何检查。

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 2016-04-10
      • 2020-09-22
      • 1970-01-01
      • 2018-03-12
      • 2017-08-03
      • 2017-01-11
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多