Personal notifications in slack messages sent by Python bot are not highlighted - python

I created a small test bot connecting a local python script with Slack. The main task of the bot is to create a custom string, and send it to a channel in Slack. This is done with
from slack_sdk import WebClient
client = WebClient(token = slack_token)
slack_message = "Hello #demo_user" # #demo_user is an existing user, which can be mentioned in chat
client.chat_postMessage(channel = channel_ID, text = slack_message)
In this message I would like to explicitly mention certain groups or persons by using #<username>. This works fine if I type out the message directly in Slack, but if the bot itself sends the message, the groups are not messaged/pinged, even though the message contains the corresponding handle. What is happening here, and how can I ping those people/groups instead via Python?

I'm using the Slack API with NodeJS, but it might come from the link_names argument. Did you try to set it to true?
https://api.slack.com/methods/chat.postMessage#arg_link_names

Related

Using Discord's API to Register New Messages

What I'm trying to understand is how Discord.py creates and sends responses from their on_message function.
Take this as an example:
#client.event()
async def on_message(message):
print(message.content)
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Discord bots establish a websocket connection with Discord's servers, which is essentially a (secure) two-way tunnel. This means that once the connection is up, Discord can send events to you. Instead of you having to manually fetch every single channel, Discord tells you "Hey, a message was created", and the payload attached will give all the additional info about it.
The Discord (not discord.py) docs have detailed info about how everything works behind the scenes, to help the people creating the Bot libraries for you. For example, this section details which types of events Discord can send to you. To see how something is constructed, click on one of the event types and read up on the data that Discord provides.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
Not really, unless you do in fact send a GET for every single channel, which will get you ratelimited. There's really no reason to ever use only GET/POST requests (other than Webhooks, where you just send a POST with your info to send a message to a channel without a bot).
If you'd like to read up on Discord's API, the docs I linked contain a full spec of everything, so you could try to do whatever your heart desires (... and the API supports).

ConversationHandler state triggers only if the message starts with '/' in groups chat [duplicate]

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

How to use python-telegram-bot to send messages to a telegram channel

I created a telegram bot and added it to my telegram channel. Now, I want to use it to send messages to my channel, when something is happening in my python program. For example, I have a python program that checks the weather every 15 secs, and when there's a change in the weather, I want my bot to send the new weather information to my telegram channel.
So my question is, how can I do it? I'm stuck because python-telegram-bot requires a message from the user to get triggered, or a scheduled orders, while I can't schedule it because I don't know when the weather will change.
The easiest method to do this is to use the request method.
Telegram provides a cool API to send messages with your bot, you need to do that with a link for example :
https://api.telegram.org/bot<yourbottoken>/sendMessage?chat_id=<yourchatid>&text=Hello World!
What this does is that it will send the Hello World message to a certain chat id.
If you don't know how to get a chat id, you need to DM your bot and you can use this link :
https://api.telegram.org/bot<yourbottoken>/getUpdates
In the page, there will be quite a lot of JSON data, you need to use Control + F and search for your telegram username without the # and search for the chat id
If you want to do this in a python code, you need to use the requests module.
import requests
requests.post('https://api.telegram.org/bot<yourbottoken>/sendMessage?chat_id=<yourchatid>&text=Hello World!')

Updating Telegram Bot Commands In Realtime

Telegram allows the commands to be updated using setMyCommands. I can successfully update the commands in realtime based on the user input using a python API, pyTelegramBotAPI.
However, the problem is the user has to exit the chat with the bot and then come back to the chat again to see the new commands (by typing /).
Is there any way that I can have the bot update the list of commands in realtime with the user still in the chat?
Actually it is possible to do this real time if you set the scope to chat with specific user. There is parameter scope in set_my_commands function. By default set_my_commands affects all the chats of your bot. However, you can provide telegram.BotCommandScopeChat as a value of scope param. See the code below, it updates the command menu immediately without need to switch chats.
def _set_menu(commands: List[BotCommand], update: Update):
''' sets the chat commands menu '''
bot = Bot(os.getenv('TELEGRAM_TOKEN'))
bot.delete_my_commands()
if update:
bot.set_my_commands(
commands=commands,
scope=telegram.BotCommandScopeChat(
chat_id=update.effective_chat.id
))
else:
bot.set_my_commands(commands=commands)
So I tried it with telegram client (on linux and on android) and the commands do not change unless the user re-enters the chat. This I think is because the telegram-client only loads the commands when the user enters the chat.
But I also tried this with telegram web and found that the commands changed immidiately after I changed the command sets from BotFather. The webapp actually did load the command set without me leaving the chat, after sending a single message/command
So its definitely a problem with the telegram-client.

channel_not_found error while sending a message to myself

I'm using python3.6 and trying to send message to myself to start interacting in slack.
I've installed pip install slackclient and am using slackclient v2.2.1
channel ID is extracted from slack link of my account https://XXXXX.slack.com/messages/XXXXXXXXX
I would like to see Hi message in my slack account. any suggestions.
client = slack.WebClient("BOT_USER_TOKEN", timeout=30)
client.chat_postMessage(
channel='CHANNEL_ID',
text='Hi!')```
The bot user token is linked to a bot user that is created with your app. If you use the bot token you will only have access to channels that this bot user is a member of.
So to make your script work you need to do one of the following:
Use the access token instead of the bot token (that one is linked to the user that installed the Slack app)
Invite the bot user to the channel you are trying to send a message to
As you want to start with basics, I would recommend to use public channels first, which will always work.
Direct messages are a little bit more complicated. To send a direct message to a user (e.g. from your bot user to yourself) you need to first open a direct message channel with conversations.open, which will give you a new channel ID. And then use that channel ID for sending a message.

Categories