簡體   English   中英

在c#bot中按用戶ID發送消息

[英]sending message by user id in c# bot

我正在嘗試向People用戶名發送消息,例如我的朋友用戶名是@ ...但是沒有事情發生沒有錯誤,沒有發送我可以使用這個庫嗎? 或者只是用tlsharp? 我如何為我的機器人找到apiid和hashid?

使用Telegram.Bot;

 public partial class Form1 : Form
    {
       Telegram.Bot. TelegramBotClient bot = new TelegramBotClient("token");

        Thread a;
             public Form1()
        {
            InitializeComponent();
        }


        public void GetUpdates()
        {
            int offset = 0;
            while (true)
            {

                Telegram.Bot.Types.Update[] updates = 
                 bot.GetUpdatesAsync(offset).Result;

                foreach (var update in updates)
                {

                    offset = update.Id + 1;
                    if (update.Message == null)
                        continue;

                    var from = update.Message.From.FirstName;
                    var text = update.Message.Text;
                   string  chatid = update.Message.Chat.Id;
                    string username = update.Message.From.Username;
                    // label1.BeginInvoke(delegate { label1.Text = label1.BeginInvoke(delegate { label1.Text = string.Format("sender:{0}\ntext:{1}\ncatid:{2}", from, text, chatid); }); });
                  // lblshow.Text = string.Format("sender:{0}\ntext:{1}\ncatid:{2}", from, text, chatid);
                  //label1.Text = string.Format("sender:{0}\ntext:{1}\ncatid:{2}", from, text, chatid);
                    this.BeginInvoke((System.Windows.Forms.MethodInvoker)delegate () {textBox1.Text = string.Format("sender:{0}\ntext:{1}\ncusername:{2}", from, text, username); });
                  bot.SendTextMessageAsync( chatid, "سلام بر شما");
                    //is this correct for send to people? 
                    bot.SendTextMessageAsync("@Hoda.....", "hi");


                }




            }

您無法通過用戶Name.telegram bot API向用戶發送消息只接受用戶ID。 (渠道除外)。

當您嘗試向用戶發送消息時,您必須知道他們的chatID (chatID是一個長號,對於電報中的每個用戶都是唯一的,並且在用戶名可以更改時永遠不會更改),或者您必須將他們的chatID存儲在數據庫或文件中或...

這意味着用戶必須包含/ start命令之前向您的機器人發送至少一條消息 然后您的機器人可以找到他們的chatID並使用該chatID,您可以向該用戶發送您想要的任何內容,除非他/她在嘗試刪除他/她與您的機器人之間的對話時按下刪除和停止按鈕來阻止您的機器人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM