Telegram recurring message to one user - python

I need to send two specific recurring messages to a user in telegram .
The first message is followed by the second after some seconds .
It will be the same two messages every 10 mn ,everyday !
Example :
RecurringMessage1 :
I AM READY
then after some seconds comes the second one :
RecurringMessage2 :
YOU CAN SEND NOW
This would be repeated every 10 mn every day and so on .
The receiver is a bot on telegram that has a #username only .
( i dont have the skills to get his chat id , and restricted to include him in any group ) .
Any help is highly appreciated .
Tried using scheduled messages feature of telegram , it works but it is completely unpractical to use it due to the large number of recurring messages .
Thank you

You can try to use telethon module to get the chat id of the bot while it's reading all your current incoming messages.
from telethon import TelegramClient, events
Otherwise if you remove the '#' from the bot's name e.g. #MyBot, you should be able to use https://t.me/MyBot as an alternative.

Related

Can i get chat id with pyrogram?

I want to know, is there a way, to get a of a chat which i'm joining my bot to?(using pyrogram)
like, i created a chat manager bot, and when i'm joining him to chat, i want him to work in this specific chat.
But every function like get_chat_members() needs target id.
how do it get this?
i already found a solution
You can get in through a message form that chat.

How to automate message service

I have a message service , where i can set time frame to send message lets say between 9 AM to 10 AM , now i can send message using some Rest api services ( internal api ) , but once i send it will be in enqueue , basically i don't know when it will be sent , now i want to automate this validation part ( once i sent message my script should check status each time lets say every 5 min , if sent status success it should end test case if still status not update it should retry to check upto 5 times )
Can any one help me on this .. Thanks in Advance.

WhatsApp bot in python

I'm trying to make a simple WhatsApp bot using python. But the bot doesn't send the message after it writes the automated message.
This is the code I have written
import pywhatkit
pywhatkit.sendwhatmsg("+20100081978767","Whatsapp bot",5,24)
The reason the message is not sent, is because of the time you set. Have a look at the parameters -
Syntax: pywhatkit.sendmsg(“receiver mobile number”,”message”,hours,minutes)
Parameters:
Receiver mobile number: The Receiver’s mobile number must be in string format and the country code must be mentioned before the mobile number.
Message: Message to be sent(Must be in string format).
Hours: This module follows the 24 hrs time format.
Minutes: Mention minutes of the scheduled time for the message(00-59).
Check this
So, in your code, pywhatkit.sendwhatmsg("+20100081978767", "Whatsapp bot",5,24), 5 and 24 actually means that the message will be sent at 5:24 am tomorrow.
So, try changing time so that you can send the message today.

Get last message/s from Telegram channel with Python

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).

Using the Slack API, how can I direct message all users at the same time?

On Slack, I'm aware that using chat.postMessage allows me to message each user individually, but how would I go about direct messaging the entire team (400 members) at once?
There is no "bulk" variant of chat.postMessage. So you basically need to build your own bulk message sender, which you can easily do by iterating through the list of users and sending each of them a message.
You can get the list of all users with users.list. You then have two option for sending direct messages:
Slackbot channel: Send each of them a message with chat.postMessage using the ID of each user as channel.
App channel: Get the IM channel ID with im.open for each user
and then use that as channel for chat.postMessage. This only works though if you have a bot user and send the message from the bot user.
Keep in mind though, that there is a request limit of 1 message per second.
There also is a 3 seconds request time-out for many requests between Slack and your app. (e.g. for direct response to slash commands). So if your bot needs to send many messages you want to use an approach that allows you to send them asynchronously.
One solution to this problem that works very well for me is to use a messaging queue for all Slack messages sent from my bots.
Hello For that you need a channel with all 400 of them in it, cause currently you cannot send to 400 individual users. For sending message to a channel, you just need to add channel argument for postMessage method.
Check this :: https://api.slack.com/methods/chat.postMessage

Categories

Resources