1

I made a telegram bot. Now I want to send a share button for posts into channel.

$btn =array('inline_keyboard'=>
    array(
        array(
            array(
                'text'=> 'share', 
                'switch_inline_query'=>"1111"
            )
        )   
    )
);
$btn = json_encode($btn);
$content = array
    (
        'chat_id' => $id,
        'reply_markup' => $btn, 
        'text' => $textt,
    );
$bot->sendMessage($content);

Now if I set a user_telegram_id of any user in instead of $id, this code works! But if I set the channel_telegram_id (in which the robot is admin) instead of $id this doesn't work! What should I do?

( Also if I use url and do not use switch_inline_query, then this code works for bot channel_telegram_id and user_telegram_id and I have no problem in this case )

1 Answers1

-1

If you want to send the message to the public channel, you can put the username in place of the ID. For example: @channelname

$content = array
(
    'chat_id' => @channelname,
    'reply_markup' => $btn, 
    'text' => $textt,
);

but if you want send the message to the private channel, You can follow these steps:

  1. You should convert it to the public with some @channelName and Send the message to this channel through Bot API https://api.telegram.org/bot111:222/sendMessage?chat_id=@channelName&text=123
  2. As the response, you will get info with chat_id of your channel.{ "ok" : true, "result" : { "chat" : { "id" : -1001005582487, "title" : "Test Private Channel", "type" : "channel" }, "date" : 1448245538, "message_id" : 7, "text" : "123ds" } }
  3. Now you can convert Channel back to private (by deleting channel's link) and send message directly to this chat_id "-1001005582487" https://api.telegram.org/bot111:222/sendMessage?chat_id=-1001005582487&text=123

If you need more detailed explanation, read this page

Alihossein shahabi
  • 4,034
  • 2
  • 33
  • 53
  • You didn't understand what I said!!! I could send message to channel with **id** of channel or **username** of channel. The problem is this: **switch_inline_query** does not work for channel via bot. When I make inline keyboard with **switch_inline_query** it is not possible to send it into a channel with bot itself, so I have to make inline keyboard with bot and get the post that is made by bot and then share it in channel! Please read my question with more ... – Hamid Shafie Asl Apr 29 '18 at 06:03