i can't get anything from telegram using webhook
hi , i try telegram bot using "Telegram.bot" library & i want to use asp.net mvc for managing my bot. i use Echo example and it worked successfully , but i want to use windows hosting server with domain name and ssl certificate. now, getupdates method has working great , but when i set a webhook address, i can't get anything from telegram with webhook method. can any one help me?? here is my code in a controller in asp.net mvc
using System;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InlineQueryResults;
using System.IO;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.InputMessageContents;
using Telegram.Bot.Types.ReplyMarkups;
namespace telegramBot.Controllers
{
public class ExampleController : Controller
//public partial class Hook : System.Web.UI.Page
{
private static readonly TelegramBotClient Bot = new TelegramBotClient("API TOKEN");
protected void Page_Load(object sender, EventArgs e)
{
Bot.OnCallbackQuery += BotOnCallbackQueryReceived;
Bot.OnMessage += BotOnMessageReceived;
Bot.OnMessageEdited += BotOnMessageReceived;
Bot.OnInlineQuery += BotOnInlineQueryReceived;
Bot.OnInlineResultChosen += BotOnChosenInlineResultReceived;
Bot.OnReceiveError += BotOnReceiveError;
var me = Bot.GetMeAsync().Result;
Console.Title = me.Username;
Bot.StartReceiving();
Console.ReadLine();
Bot.StopReceiving();
}
private static void BotOnReceiveError(object sender, ReceiveErrorEventArgs receiveErrorEventArgs)
{
Debugger.Break();
}
private static void BotOnChosenInlineResultReceived(object sender, ChosenInlineResultEventArgs chosenInlineResultEventArgs)
{
Console.WriteLine($"Received choosen inline result: {chosenInlineResultEventArgs.ChosenInlineResult.ResultId}");
}
private static async void BotOnInlineQueryReceived(object sender, InlineQueryEventArgs inlineQueryEventArgs)
{
InlineQueryResult[] results = {
new InlineQueryResultLocation
{
Id = "1",
Latitude = 40.7058316f, // displayed result
Longitude = -74.2581888f,
Title = "New York",
InputMessageContent = new InputLocationMessageContent // message if result is selected
{
Latitude = 40.7058316f,
Longitude = -74.2581888f,
}
},
new InlineQueryResultLocation
{
Id = "2",
Longitude = 52.507629f, // displayed result
Latitude = 13.1449577f,
Title = "Berlin",
InputMessageContent = new InputLocationMessageContent // message if result is selected
{
Longitude = 52.507629f,
Latitude = 13.1449577f
}
}
};
await Bot.AnswerInlineQueryAsync(inlineQueryEventArgs.InlineQuery.Id, results, isPersonal: true, cacheTime: 0);
}
private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
{
var message = messageEventArgs.Message;
if (message == null || message.Type != MessageType.TextMessage) return;
if (message.Text.StartsWith("/inline")) // send inline keyboard
{
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);
var keyboard = new InlineKeyboardMarkup(new[]
{
new[] // first row
{
new InlineKeyboardButton("1.1"),
new InlineKeyboardButton("1.2"),
},
new[] // second row
{
new InlineKeyboardButton("2.1"),
new InlineKeyboardButton("2.2"),
}
});
await Task.Delay(500); // simulate longer running task
await Bot.SendTextMessageAsync(message.Chat.Id, "Choose",
replyMarkup: keyboard);
}
else if (message.Text.StartsWith("/keyboard")) // send custom keyboard
{
var keyboard = new ReplyKeyboardMarkup(new[]
{
new [] // first row
{
new KeyboardButton("1.1"),
new KeyboardButton("1.2"),
},
new [] // last row
{
new KeyboardButton("2.1"),
new KeyboardButton("2.2"),
}
});
await Bot.SendTextMessageAsync(message.Chat.Id, "Choose",
replyMarkup: keyboard);
}
else if (message.Text.StartsWith("/photo")) // send a photo
{
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.UploadPhoto);
const string file = @"<FilePath>";
var fileName = file.Split('\\').Last();
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var fts = new FileToSend(fileName, fileStream);
await Bot.SendPhotoAsync(message.Chat.Id, fts, "Nice Picture");
}
}
else if (message.Text.StartsWith("/request")) // request location or contact
{
var keyboard = new ReplyKeyboardMarkup(new[]
{
new KeyboardButton("Location")
{
RequestLocation = true
},
new KeyboardButton("Contact")
{
RequestContact = true
},
});
await Bot.SendTextMessageAsync(message.Chat.Id, "Who or Where are you?", replyMarkup: keyboard);
}
else
{
var usage = @"Usage:
/inline - send inline keyboard
/keyboard - send custom keyboard
/photo - send a photo
/request - request location or contact
";
await Bot.SendTextMessageAsync(message.Chat.Id, usage,
replyMarkup: new ReplyKeyboardHide());
}
}
private static async void BotOnCallbackQueryReceived(object sender, CallbackQueryEventArgs callbackQueryEventArgs)
{
await Bot.AnswerCallbackQueryAsync(callbackQueryEventArgs.CallbackQuery.Id,
$"Received {callbackQueryEventArgs.CallbackQuery.Data}");
}
}
}
Hi, you can not simply copy and paste echo code in your MCV code and anticipate that it works well. The webhook idea is different than Echo. Your program doesn't call telegram. telegram will call your webhook function. Please reed the documents carefully. webhook documents : https://core.telegram.org/bots/webhooks webhook Example: https://github.com/MrRoundRobin/telegram.bot.examples/tree/master/Telegram.Bot.Examples.WebHook
hi, i change my code but can't get any thing again! :( My site (that hosts the bot asp.net mvc project) has the SSL certificate working (I get the green lock in the address bar) I set up the webhook with:
https://api.telegram.org/bot<token>/setwebhook?url=https://www.example.com/home/index
And I got: {"ok":true,"result":true,"description":"Webhook was set"}
here is my code:
`using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Mvc;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using telegramBot.Models;
namespace telegramBot.Controllers
{
public class HomeController : Controller
{
static class Bot
{
public static readonly TelegramBotClient Api
= new TelegramBotClient("API TOKEN");
}
public async Task<ActionResult> Index(Update update)
{
var message = update.Message;
if (message.Type == MessageType.TextMessage)
{
await Bot.Api.SendTextMessageAsync(message.Chat.Id, message.Text);
}
return View(msg);
}
}
}`
I have a same problem
I have a same problem.
Me too :-(
I only use this project in net core apps so can't help you with that stack. If you guys found any clues/solutions please share here.
I have the same problem. But I got an update but it was null. The solution for me was to add this line to the ConfigureServices method in the .net core application: services.AddControllers() .AddNewtonsoftJson();