【发布时间】: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