Forwarding message with TLSharp library (C#)
Asked Answered
F

3

6

I'm connecting to Telegram API with this C# library: TLSharp

The resources for this library is not enough, I'm trying to Forward a message to channel .

I'm not quiet sure how to get channel post peer id (for forward function input parameters)

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()
                               });

I get this error message :

{"PEER_ID_INVALID"}

Please help me, what is that peer id and how can i access and use that for forwarding a message using TLSharp. Thanks.

Freehanded answered 11/3, 2017 at 13:24 Comment(2)
Are you the owner of this channel?Slaughterhouse
@CharlesOkwuagwu Yeah sure we have that channel and groupCarlyncarlynn
F
2

This code can be used for forward

**

await client.SendRequestAsync<TLAbsUpdates>( new TLRequestForwardMessage() { id = item2.Id, peer = new TLInputPeerChat() { chat_id = item.id }, random_id = Helpers.GenerateRandomLong(), });
---------------------------------------

**---------------------------------

Freehanded answered 31/3, 2017 at 8:36 Comment(2)
excuse me, what type are item and item2?(TLMessage or TLChannel or TLChat)Rolo
@purTahan Hi! for sendmeesage to channel you must use metoth SendMessagesAsync metoth SendMessageAsync Does not work for send to channelFreehanded
I
3

You can do that with this nice trick which is hilarious: this code is below:

new TLInputPeerChannel { channel_id = chat.id, access_hash = cha.access_hash.Value }, offset,
                maxId, limit);
Incompliant answered 31/3, 2017 at 8:30 Comment(0)
F
2

This code can be used for forward

**

await client.SendRequestAsync<TLAbsUpdates>( new TLRequestForwardMessage() { id = item2.Id, peer = new TLInputPeerChat() { chat_id = item.id }, random_id = Helpers.GenerateRandomLong(), });
---------------------------------------

**---------------------------------

Freehanded answered 31/3, 2017 at 8:36 Comment(2)
excuse me, what type are item and item2?(TLMessage or TLChannel or TLChat)Rolo
@purTahan Hi! for sendmeesage to channel you must use metoth SendMessagesAsync metoth SendMessageAsync Does not work for send to channelFreehanded
M
0

And this is my code to Forward a message:

/* 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);

I can't use this method, and have to use this one

Machismo answered 4/7, 2020 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.