在c#bot中按用户ID发送消息

时间:2017-06-17 13:21:12

标签: c# telegram-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");


                }




            }

3 个答案:

答案 0 :(得分:0)

您无法通过用户向用户发送消息Name.telegram bot API仅接受用户ID。 (渠道除外)。

答案 1 :(得分:0)

当您尝试向用户发送消息时,您必须知道他们的chatID (chatID是一个长号码,对于电报中的每个用户都是唯一的,并且在用户名可以更改时永远不会更改)或者您必须拥有将他们的chatID存储在数据库或文件中......或者

这意味着用户必须先向您的机器人发送至少一条消息,然后再包含 / start命令 。然后您的机器人可以找到他们的chatID并使用该chatID,您可以向该用户发送您想要的任何内容,除非他/她在尝试删除之间的对话时按删除并停止按钮阻止您的机器人他/她和你的机器人。

答案 2 :(得分:0)

您可以但不能传递@符号,如果用户名是 @me ,则将"me"传递给SendTextMessageAsync而不传递@。

相关问题