使用 TLSharp 库转发消息 (C#)
Forwarding message with TLSharp library (C#)
我正在使用这个 C# 库连接到 TelegramAPI:TLSharp
这个库的资源不够,我正在尝试转发消息到频道。
我不太确定如何获取频道 post peer
id(用于正向函数输入参数)
var store = new FileSessionStore();
var client = new TelegramClient(Properties.Settings.Default.apiID, Properties.Settings.Default.apiHash, store, "session");
await client.ConnectAsync();
var dialogs = (TLDialogs)await client.GetUserDialogsAsync();
var chat = dialogs.chats.lists
.OfType<TLChannel>()
.SingleOrDefault(a=>a.title=="test");
await client.SendRequestAsync<TLAbsUpdates>(
new TLRequestForwardMessage()
{
id = 2,
peer = new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value },
random_id = Helpers.GenerateRandomLong()
});
我收到此错误消息:
{"PEER_ID_INVALID"}
请帮助我,那个对等 ID 是什么,我如何访问和使用它来使用 TLSharp 转发消息。谢谢
你可以用这个有趣的技巧来做到这一点:代码如下:
new TLInputPeerChannel { channel_id = chat.id, access_hash = cha.access_hash.Value }, offset,
maxId, limit);
此代码可用于转发
**
await client.SendRequestAsync<TLAbsUpdates>( new TLRequestForwardMessage() { id = item2.Id, peer = new TLInputPeerChat() { chat_id = item.id }, random_id = Helpers.GenerateRandomLong(), });
---------------------------------------
**--------------------------------
这是我转发消息的代码:
/* e.g you can use TLInputPeerUser, TLInputPeerChat, TLInputPeerChannel here as an SourcePeer */
// a Person
var sourcePeer = new TLInputPeerUser { UserId = <<<USER.ID>>>, AccessHash = <<<USER.AccessHash>>> };
// normal Group
//var sourcePeer = new TLInputPeerChat { ChatId = <<<USER.ID>>> };
// SuperGroup or Channel
//var sourcePeer = new TLInputPeerChannel { ChatId = <<<USER.ID>>> , AccessHash = <<<USER.AccessHash>>> };
/* e.g you can use TLInputPeerUser, TLInputPeerChat, TLInputPeerChannel here as an SourcePeer */
// a Person
//var targetPeer = new TLInputPeerUser { UserId = <<<USER.ID>>>, AccessHash = <<<USER.AccessHash>>> };
// normal Group
var targetPeer = new TLInputPeerChat { ChatId = <<<USER.ID>>> };
// SuperGroup or Channel
//var targetPeer = new TLInputPeerChannel { ChatId = <<<USER.ID>>> , AccessHash = <<<USER.AccessHash>>> };
// random Ids to prevent bombinggg
var randomIds = new TLVector<long>
{
TLSharp.Core.Utils.Helpers.GenerateRandomLong()
};
// source messages
var sourceMessageIds = new TLVector<int>
{
messageIdInSourceContactToForward // this ID should be in the SourcePeer's Messages
};
var forwardRequest = new TLRequestForwardMessages()
{
FromPeer = sourcePeer,
Id = sourceMessageIds,
ToPeer = targetPeer,
RandomId = randomIds,
//Silent = false
};
var result = await myTelegramClient.SendRequestAsync<TLUpdates>(forwardRequest);
我不会用this method, and have to use this one
我正在使用这个 C# 库连接到 TelegramAPI:TLSharp
这个库的资源不够,我正在尝试转发消息到频道。
我不太确定如何获取频道 post peer
id(用于正向函数输入参数)
var store = new FileSessionStore();
var client = new TelegramClient(Properties.Settings.Default.apiID, Properties.Settings.Default.apiHash, store, "session");
await client.ConnectAsync();
var dialogs = (TLDialogs)await client.GetUserDialogsAsync();
var chat = dialogs.chats.lists
.OfType<TLChannel>()
.SingleOrDefault(a=>a.title=="test");
await client.SendRequestAsync<TLAbsUpdates>(
new TLRequestForwardMessage()
{
id = 2,
peer = new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value },
random_id = Helpers.GenerateRandomLong()
});
我收到此错误消息:
{"PEER_ID_INVALID"}
请帮助我,那个对等 ID 是什么,我如何访问和使用它来使用 TLSharp 转发消息。谢谢
你可以用这个有趣的技巧来做到这一点:代码如下:
new TLInputPeerChannel { channel_id = chat.id, access_hash = cha.access_hash.Value }, offset,
maxId, limit);
此代码可用于转发
**
await client.SendRequestAsync<TLAbsUpdates>( new TLRequestForwardMessage() { id = item2.Id, peer = new TLInputPeerChat() { chat_id = item.id }, random_id = Helpers.GenerateRandomLong(), });
---------------------------------------
**--------------------------------
这是我转发消息的代码:
/* e.g you can use TLInputPeerUser, TLInputPeerChat, TLInputPeerChannel here as an SourcePeer */
// a Person
var sourcePeer = new TLInputPeerUser { UserId = <<<USER.ID>>>, AccessHash = <<<USER.AccessHash>>> };
// normal Group
//var sourcePeer = new TLInputPeerChat { ChatId = <<<USER.ID>>> };
// SuperGroup or Channel
//var sourcePeer = new TLInputPeerChannel { ChatId = <<<USER.ID>>> , AccessHash = <<<USER.AccessHash>>> };
/* e.g you can use TLInputPeerUser, TLInputPeerChat, TLInputPeerChannel here as an SourcePeer */
// a Person
//var targetPeer = new TLInputPeerUser { UserId = <<<USER.ID>>>, AccessHash = <<<USER.AccessHash>>> };
// normal Group
var targetPeer = new TLInputPeerChat { ChatId = <<<USER.ID>>> };
// SuperGroup or Channel
//var targetPeer = new TLInputPeerChannel { ChatId = <<<USER.ID>>> , AccessHash = <<<USER.AccessHash>>> };
// random Ids to prevent bombinggg
var randomIds = new TLVector<long>
{
TLSharp.Core.Utils.Helpers.GenerateRandomLong()
};
// source messages
var sourceMessageIds = new TLVector<int>
{
messageIdInSourceContactToForward // this ID should be in the SourcePeer's Messages
};
var forwardRequest = new TLRequestForwardMessages()
{
FromPeer = sourcePeer,
Id = sourceMessageIds,
ToPeer = targetPeer,
RandomId = randomIds,
//Silent = false
};
var result = await myTelegramClient.SendRequestAsync<TLUpdates>(forwardRequest);
我不会用this method, and have to use this one