Reputation: 75
Basically I'm trying to understand how this API works, by doing the following .I'm not able to get the message to my channel (I'm sending the /test command from my telegram account). Also, how I'm supposed to use JobQueue and send auto-msgs every 3 hours for example? The documentation explanation doesn't works for me.
def test(bot, update):
update.send_message(chat_id='@channelid', text='this is a test')
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater("457160310:AAFlxrH2uAaOMGrgO0suOXFM2gVKywsUL0E")
dp = updater.dispatcher
dp.add_handler(CommandHandler("test", test))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Upvotes: 3
Views: 11318
Reputation: 196
You can send messages directly to a channel or a conversation using the Telegram REST API by knowing its chat_id.
According to the Telegram documentation, methods such as webhooks require users to send a message to the bot first before the bot can reply. These methods essentially aim to obtain a chat_id, which is different for different recipients.
However, if you want to send messages to a specific channel without going through that process, you only need to acquire the fixed chat_id of the channel and the bot's token. Then, you can use curl to send the message:
// get
curl -i -X GET \
'https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text=hello'
// post
curl -i -X POST \
-H "Content-Type:application/json" \
-d \
'{
"chat_id": "{1}",
"text": "here is bot"
}' \
'https://api.telegram.org/bot{0}/sendMessage'
Please replace {0} with the bot's token and {1} with the fixed chat_id of the channel.
You can send the channel's link to the @username_to_id_bot, and it will provide you with the chat_id of the channel. Link: https://t.me/username_to_id_bot
Upvotes: 0
Reputation: 1700
For the job implementation to send messages on intervals you can read this page that is full of examples. Someone should be the very same thing you want to do.
About the command thing, I think that the library doesn’t get commands from channels so you may consider to:
send the command in private chat to the bot and the bot sends the reply to the channel (better thing in my opinion)
handle messages from channels (not commands) and checking if the text of the message contains the command
I would suggest the first solution so you can even keep clean the channel
Upvotes: 1