Search code examples
c#asp.netbotstelegramapplication-start

Telegram bot in asp.net Application_Start


I write a telegrom bot with c# and put it in my asp.net web application . but does not work

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Telegram.Bot;

namespace MyFirstBot
{
public class Global : System.Web.HttpApplication
{
    TelegramBotClient bot = new TelegramBotClient("My Token");

    TweetTestEntities context = new TweetTestEntities();

    protected void Application_Start(object sender, EventArgs e)
    {
        bot.OnMessage += Bot_OnMessage;
        bot.StartReceiving();
    }

    private void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
    {
        //Some Code
    }
}
}

My application is on a host with no problem


Solution

  • You Can Use below Code to Initialize your Bot and WebHook

    Public Shared Sub init()
        Client = New TelegramBotClient(tocken)
        Dim ms As New MemoryStream(Encoding.ASCII.GetBytes(<Your Self-Signed SSL Key>))
        Dim cert As New FileToSend("SSL.pem", ms)
        Client.SetWebhookAsync("https://Your/WebHook", cert)
    
        With Client.GetMeAsync.Result
            BotID = .Id
            BotUserName = .Username
            Try
                BotPhotoID = Client.GetUserProfilePhotosAsync(.Id).Result.Photos.First.First.FileId
            Catch ex As Exception
            End Try
        End With
    
    End Sub