I am quite confused on Slack bot using Python. I want to be able to receive messages sent to by bot:
client = SlackClient(API_TOKEN)
if client.rtm_connect():
while True:
print client.rtm_read()
time.sleep(1)
else:
print "Connection Failed, invalid token?"
Then I send a message to my bot in slack. But I don't get the message. How do I start sending message to my user?
Also, is it possible to include the bot in all channels? So if I'm in any channel, I want to be able to say #myBot args 123
Slack only sends events to bots who are joined to a channel and by default they are not joined to any channels and receive events only for private messages. If you invite your bot to the channel then Slack should start routing events from the channel to it.
As for the second part of your question, mentions (e.g., #bot) are not special events and you must check each incoming event to see if it is message that is directed at your bot (see this answer. One gotcha is you need to know the ID of your bot as the event will contain something like <#YOURBOTID> args 123. Finding the ID of your bot and other useful tips for building a python slack bot can be found in this article.
There are many types of events in slack, if they have been subscribed by your BOT, then your BOT will definitely get related traffic on your request URL.
So, if you want to receive any message that has been directly sent to your BOT, you have to subscribe direct message event.
And if you want your BOT triggered by #bot .. kind of messages, then you need to subscribe app_mention event. Your BOT would receive traffic if it has been mentioned in any group but it can post a message to the only group in which it is allowed to.
Related
My telegram bot receives messages sent by user to my bot in private chats but not receives messages sent by users in group chats. Any options/api for getting group chat messages also,.
Talk to #botfather and disable the privacy mode.
Sequence within a BotFather chat:
You: /setprivacy
BotFather: Choose a bot to change group messages settings.
You: #your_name_bot
BotFather: 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.
'Disable' - your bot will receive all messages that people send to groups.
Current status is: ENABLED
You: Disable
BotFather: Success! The new status is: DISABLED. /help
By default A Bot will receive only messages addressed to it by any user directly via posting by /command#YourBot any message you send.
After that it vill be available via getUpdates API call.
In browser it will be:
https://api.telegram.org/botToken/getupdates
Find the related message in output JSON and grab chatId. It will allow you to answer back with:
https://api.telegram.org/botToken/sendmessage?chat_id=123456788&text=My Answer
Make your bot by admin in group.
You can access all avaliable settings from all of your bots by sending /mybots to Botfather. Choose the bot, then Bot Settings and Group Privacy. If its disable (default), you can tap on Turn off.
Now its possible to receive the chat history using GetUpdates. This can be done via HTTP API or the frameworks. For example, in C# (.NET Core) like this:
var bot = new TelegramBotClient(ApiToken);
var updates = bot.GetUpdatesAsync().Result;
foreach(var update in updates) {
Console.WriteLine($"{update.ChannelPost.Date} {update.ChannelPost.Text}");
}
But keep in mind that this feature has some kind of perfect forward secrecy implemented. So you only get messages that were send after group privacy is disabled. As a result, the GetUpdates result is empty until some post was made.
if you added your bot before disable the privacy mode, you should remove the bot from the group and add it again
Can I use the discord API in python to harvest messages from a server (not my own server)? Assuming you have an invite link.
Thanks
Yes, it would be possible. If you look under the TextChannel section of the Discord Models section of the discord.py API Reference, there is the TextChannel history() method. This can be used to parse all the messages in a channel. If the limit argument of the method is set to None, then all the messages in the channel will be returned.
Since you say that the bot already has an invite, that implies they have access to a good part of the server. The method I mentioned requires the following permissions for your bot:
See Text Channels
Read Message History
Well, if your using a discord bot, you need them to invite your bot to their server.
Other than that you could theoretically listen with a bot on your own account but that would be against the discord TOS.
You can achieve this through a discord bot. Given that bots respond to events that fire within servers they reside in, you can listen in to incoming messages.
An example of such event can be found in discord-py documentation.
I want to make a telegram bot that people can send the bot a message if they first join a specific channel that the bot says
For example, if a user wants to send a message to the bot, the bot will tell the user that you have to join this channel then you can send me this massage
How can I write its code in python?
I've read related questions in this site but none of them answer my question clearly
thanks for your answers...
If you add the bot to the channel as admin, you can use getChatMember with the channel ID and the user ID.
i have a telegram -bot in a group which sends messages like current weather, stock prices , news and so on. at the end of the day i want to clean up the messages sent by bot. however i unable to get the message id of the messages sent by bot using the api 'https://api.telegram.org/botxxxx:xxxxxxxxxxx/getUpdates' and hence i am unable to delete message created by bot. any updates on this issue will be appreciated
you should store the message IDs when invoking the send method (from the message object returned) and delete those messages while emptying out your message id storage later on.
This is efficient than your approach for couple of reasons. For instance, with your approach you may have to filter out other messages/updates on that channel that weren't send by the bot (if your bot isn't the only admin who posts content).
I am writing a Telegram bot, to simplify the issue let's say you can ask the bot questions in a channel and it replies to you in private. When the bot receives the command it takes the chat_id of the user from: update.message.from_user.id
But when I do this I get an error:
Unauthorized: Forbidden: bot can't initiate conversation with a user
This sounds quite logical, as I assume this prevents from writing spam bots. The question is how do I overcome it? How can I let the user enable private messages from the bot?
Note: The bot does succeed sending messages to me but not to other users.
Telegram Bots can't initiate a conversation with a user.
Only an user can initiate it by clicking on the start button. There is no way to around this.
In your case, you can ask the user to start the bot before asking questions in the channel, for example by attaching a button containing the bot URL (https://t.me/bot_username).
message.from_user.id
try instead
message.chat.id