TLSharp向群组发送消息

时间:2018-10-01 19:37:42

标签: c# telegram

//get user dialogs
var dialogs = await client.GetUserDialogsAsync() as TLDialogs;

//find channel by title
var chat = dialogs.Chats
                  .Where(c => c.GetType() == typeof(TLChat))
                  .Cast<TLChat>()
                  .FirstOrDefault(c => c.Title == "zgzxbhsrbhdrbh");

//send message
await client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = chat.Id }, 
                                                         "OUR_MESSAGE");

我正在尝试此代码,但它返回了我InvalidOperationException:CHANNEL_INVALID。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

你说得很对。

//Get dialogs
 var dialogs = await client.GetUserDialogsAsync();

//get user chats 
   var chats = ((TeleSharp.TL.Messages.TLDialogsSlice)dialogs).Chats;

//find channel by title
   var tlChannel = chats.Where(_ => _.GetType() == typeof(TLChannel))
                        .Select(_=>(TLChannel)_)
                        .Where(_=>_.Title.Contains("<Channel-Name>"))
                        .FirstOrDefault();
//send message
    await client.SendMessageAsync(new TLInputPeerChannel()
                    { ChannelId = tlChannel.Id, AccessHash =(long)tlChannel.AccessHash },
                    "OUR_MESSAGE");

答案 1 :(得分:0)

发送到群组和发送到频道是有区别的 在你的代码中,在它的最后,你调用了一个命令来发送到一个通道,在第一个中,你将类型指定为一个组。这是最新的例外。你可以试试这个代码,上帝保佑,问题就解决了。

//get user dialogs
        var dialogs = (TLDialogsSlice)await client.GetUserDialogsAsync();
        //find channel by title
        var chat = dialogs.Chats
          .Where(c => c.GetType() == typeof(TLChat))
          .Cast<TLChat>()
          .FirstOrDefault(c => c.Title == "zgzxbhsrbhdrbh");

        //send message
        await client.SendMessageAsync(new TLInputPeerChat() { ChatId = chat.Id }, "OUR_MESSAGE");