0

Доброе время суток, Хотел создать Бота для телеграмма, но подключение к боту не происходит. Вот сообственно код.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telegram.Bot;
using Telegram.Bot.Args;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        static ITelegramBotClient botClient;
        public Form1()
        {
            InitializeComponent();
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            
            botClient = new TelegramBotClient("1482817782:AAHrDRmSl9U8ck7OXHKQPSF636bgsLXv-YU");
            var me = botClient.GetMeAsync().Result;
            Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
            botClient.OnMessage += Bot_OnMessage;
            botClient.StartReceiving();

        }

        static async void Bot_OnMessage(object sender, MessageEventArgs e)
        {
            if (e.Message.Text != null)
            {
                Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}.");

                await botClient.SendTextMessageAsync(
                  chatId: e.Message.Chat,
                  text: "You said:\n" + e.Message.Text
                );
            }

        }

       
    }
}


Output

7
  • Вас не смущает соседство winforms и Console.WriteLine в одном проекте?
    – A K
    6 ноя 2020 в 19:15
  • Нет, ну это для дебага чтоб получить ид бота 6 ноя 2020 в 19:20
  • Можете помочь почему у меня не получается запустить бота? 6 ноя 2020 в 19:21
  • Могу. Оберните в try...catch и посмотрите что за ошибку выдаёт.
    – A K
    6 ноя 2020 в 19:30
  • Для GUI приложений есть Debug WriteLine(), попробуйте.
    – aepot
    7 ноя 2020 в 0:02

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.