I'm developing a telegram bot using telepot, I'm trying to ask the user a question for example: What's your name?
Then wait for the user to type the name, and save it in a variable, but this is not a library method, I think.
So I decided to create my own method.
When asking the question "What is your name?" I will go to the telegram api and I will change the text field to empty so in the next function I will use a loop that does not break until the user's last message is different from empty, understand?
I access: https://api.telegram.org/bot/(enter image description heretoken)getUpdates
and I want to change this session:
(Change a message send by user)enter image description here
"message":{"message_id":1133,"from":{"id":5296812825,"is_bot":false,"first_name":"Vinicius","language_code":"pt-br"},"chat" :{"id":5296812825,"first_name":"Vinicius","type":"private"},"date":1652800679,"text":"Mensagem from User"}}] }
for
"message":{"message_id":1133,"from":{"id":5296812825,"is_bot":false,"first_name":"Vinicius","language_code":"pt-br"},"chat" :{"id":5296812825,"first_name":"Vinicius","type":"private"},"date":1652800679,"text":"EMPTY"}}] }
No; Bot's can't alter messages send by an user
A Telegram bot can only alter/delete his own messages.
Luckily, otherwise every message you send could be altered by some random bot, this would lead to insecure behaviour for sure.
Related
I've created a slack bot that sends out a private message via the client.chat_postMessage method to different users in my workspace.
They've received the message (they showed me screenshots), however, I cannot see their private conversation via Slack it seems. Is there another way to monitor that the message has arrived and looks the way it's supposed to be (the format, attachments, etc. sent together)?
I tried this method:
response = client.conversations_info(
channel=,
include_num_members=1
)
but when I input the user's channel it returns that the channel doesn't exist.
Here's what my bot's message looks like in Slack according to the receiver. I cannot view it anywhere it seems.
If you're using your bot token to send a message to a user, the message will show up in a DM between your bot and that particular user. Your bot is its own user, separate from your user. For that reason, if you login to Slack, you won't be able to view that conversation as you yourself are not a part of the conversation.
The only way to "view" a message that has been sent to a user within a DM is to call the conversations.history method. Use the channel ID of the DM between the bot and your user, which will start with "D".
If you want to confirm how a message will look like within Slack, send the message to yourself beforehand. Additionally, adding logging within your code will help for any troubleshooting that you may need in the future.
I'm using the python-telegram-bot library to write a bot in Python that sends URLs into a channel where the bot is administrator.
Now, I would like to have the bot reading, let's say, the last 5 messages (I don't really care about the number as I just need to read the message on the chat) and store them into a list in the code for further elaborations.
I already have my bot working with:
bot = telegram.Bot(token='mytoken')
bot.sendMessage(chat_id='#mychatid', text=entry.link)
But I can't find a bot.getLastMessage or bot.getMessage kind of class into the python-telegram-bot library.
In case there's already no written class that does that, how can I implement it via the Telegram API as I'm a bit of a beginner when it comes to API implementation?
Thanks.
That's not possible in Bots unfortunately.
Here you can find all available methods (that python-telegram-bot invokes behind the scenes) and there's no such method available to fetch messages on demand.
The closest you can get through the api is getChat (which would return the pinned_message in that chat).
What you can do in this case is, store the messages the bot sends as well as the message updates the bot receives (by setting up a handler) in some storage (database) and fetch from there later on.
Have you tried the other type of Telegram API, Telegram [client] API and TDLib?
Using telethon library makes it easy to read channels history (see telethon docs).
For this, we need an api_id and an api_hash.
To get these parameters, we need to log in to our Telegram core
and go to the API development tools area.
There is a simple form that needs to be filled out, after which, we can receive our api_id and api_hash. See Telegram's help documentation about how to get your API credentials.
Here is an example code that gets the last 5 messages of targetChannelId:
from telethon import TelegramClient
API_ID = 123456 # See above for how to get it
API_HASH = '123abc' # See above for how to get it
client = TelegramClient('my-client', API_ID, API_HASH)
async def main():
async for message in client.iter_messages('targetChannelId', limit=5):
print(message.id, message.text)
with client:
client.loop.run_until_complete(main())
The first time you run this code it asks your phone number or bot token. Enter your phone number in the format +9912345... where 99 is your country code and the rest is your phone number.
It then may send a login code to your Telegram app; enter it in the console.
Note: Bots cannot see channel history messages (at least in telethon) but users (phone numbers) can. Bots can only listen for channel updates only if they are one of its administrators.
The client.iter_messages() accepts other parameters like min_id which can be used so we get messages only after a specific message (for example, we can save the last message id that we have processed and next time pass that id as min_id so only messages after that message are returned).
I'm trying to create moderation commands for the telegram bot.
Currently to ban a user I reply to his message with a command, obtaining the user's id from the message.
This is all inconvenient, I'd prefer the command to be structured like this:
/warn <username>.
I couldn't find any way to get the user id from the username.
Is anyone able to point me in the right direction?
You can't do it using bot api - you shall use tdlib or account api except (with real telegram account with phone number).
But I see better option - as your bot exists in a group and you need only to ban users within the group, you can just store pairs username-id all the time from message updates you got by bot api. So then you check the username in your database and that's it
I want to send multiple, delayed responses from the webhook written in python, once an intent is been triggered by the user. First response I want immediately after triggering the intent and another response I want after some processing to be performed on top of the user utterance.
For example:
User : I want my account balance.
BOT : Please tell your account number for details.
User : my account number is 218497234.
BOT : Hold-on a bit we are fetching your details.
BOT : Welcome John, your account balance is $70000.
In the above example, this is a bank-bot, which replies to user queries. Currently fetching-up account balance for a user supplying account number. The last two responses from the BOT are from the webhook when say "account_balance_check" intent is been triggered. First response is immediate, asking the user to be patient and wait for the account details , while the second response is after fetching the account details from a db using account number.
Another way could be to trigger response from the bot, without utterance from the user. In the above case, is there anyway, bot itself can send response to user after telling him to wait? Please note that after telling user to wait, we don't want user utterance to trigger second response.
Unfortunately, you cannot do that in Dialogflow because once you send the response then your agent will not be able to send the response without any user input or event call.
Here is an answer if your process does not take a long time. Once you get the user request, send them the waiting message with the "OK" suggestion. Once the user clicks on the suggestion, you can show the response. Also, process the request with some API and save your data in a common file that you can access through both API and agent and then show the response to the user from the file.
In my own library I'm trying to send a message using "sendMessage" API method from Telegram Bot API. I've create a channel titled, say, "my_channel123" and my user name is "my_username123". So I'm an admin of the channel and the only user.
When I'm trying to send a message to the whole channel or myself from a bot, I get an error Bad request 400. Here's how I'm trying to do that:
my_bot.send_message(chat_id="#my_channel123", text="tetfdsfd")
# or
my_bot.send_message(chat_id="#my_channel123my_username123", text= "tetfdsfd")
# or
my_bot.send_message(chat_id="#my_username123", text="tetfdsfd")
I believe the error is somewhere in the format of the ids of the channel or user name or both. Are all these 3 calls correct, meaning is the format I use for chat_id correct?
Note that most likely the probable case is the id of the chat or user_name (or rather, the format) or something else because other post and get methods in my library work properly.
You have to add the bot to be an administrator of the channel before it can send messages to the channel. After you do that, your first line should work:
my_bot.send_message(chat_id="#my_channel123", text="tetfdsfd")
Also remember that whatever follows the # should be the channel username, not the title.
when sending a message to a channel, i think its easiest to send it with the channel id...
there are a few ways to get the channel id
if the bot is a admin in the channel (go to "add admin..." and search for your bot to add it), then any message sent in the channel, the bot would be able to see it, and in every message there is an id of the sender, if its in a channel, its the channel id...
use this bot #ChannelIdBot... though the bot will still need to be in the channel as an admin, in order to send messages...