WhatsApp bot in python - 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.

Related

program that send wtsp message

I'm trying to create a python program that checks a condition for example if the room temperature is above 20 degrees and informs the user by sending a WhatsApp message. the program will be on the user's computer and it's not necessary to have the wtsp account on it. I found a lot of sites that do this thing like nexmo but they are paid. I wanna know how to send the wtsp message but free.

Telegram recurring message to one user

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.

Scheduled messages are sent at difference of 5 hours 30 minutes in Twilio and Sendgrid api

I am working on a Django application where I have used twilio to send sms and whatsapp messages and the sendgrid api for sending the emails. The problem occurs in scheduled messages in all three. For example, if I have schedule an email to be sent at 06:24 PM(scheduled time is 06:24 PM), then I am receiving the email at 11:54 PM, hence the difference is of 5 hours and 30 minutes. The same problem also arises in sms and whatsapp.
I think the problem might be due to the timezone. I am scheduling the messages from India and twilio account is in USA. And, the time difference of 5 hours 30 mins is due to the UTC format as India is 5 hours 30 mins ahead of UTC. But I am not getting any solution for this.
Also the time is specified as the UNIX time. I am getting a datetime object whose value is like: "2022-09-13 11:44:00". I am converting it to unix time with the "time module" of python. Hence the code looks like:
message = MAIL(...)
finalScheduleTime = int(time.mktime(scheduleTime.timetupple()))
message.send_at = finalScheduleTime
In the above code, scheduleTime is the datetime object.
So, is there any specific way to solve this problem ?
As per the answer of philnash, the sendgrid api and twilio is scheduling the messages in UTC and we are generating it in IST without timezone attached. Hence first of all we need to attach the timezone and then convert it in the UNIX Timestamp. Refer to the following code:
scheduleTime = scheduleTime.astimezone()
finalScheduleTime = int(time.mktime(scheduleTime.timetupple()))
message.send_at = finalScheduleTime
Hence, this will solve the problem.
The issue is that Twilio and SendGrid are scheduling the messages in UTC and you are creating your time in IST without a time zone attached, so the time is being interpreted as UTC and thus sent 5 hours 30 minutes out.
I'm not a python expert, so I may be wrong here. But, I think using mktime is incorrect here and you should be using gmtime.
message = MAIL(...)
finalScheduleTime = int(time.gmtime(scheduleTime.timetuple()))
message.send_at = finalScheduleTime
Alternatively, you don't have to convert the datetime to a time first. You can change the datetime's timezone from your local one to UTC:
import pytz
utcdatetime = scheduleTime.astimezone(pytz.UTC)
Then turn that into a timestamp:
message.send_at = int(utcdatetime.timestamp())

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

Sending a message with PRAW but the receiving account does not ever get the message

I'm trying for the first time using APIs with Python. I'm using PRAW. I've wrote a stupid easy script trying to send a message from one account to another. I'm prompted to login and enter the captcha, but then the receiving account does not ever get the message. What am I doing wrong? I need to get the foundations down to be able to progress onto a more complicated program that implements some logic and other methods.
import praw
r = praw.Reddit('PRAW related-question monitor by u/testpurposes v 1.0.')
r.login()
user = 'krumpqueen'
msg = 'Hello'
r.user.send_message(user, msg)
Your code appears to be correct. The r.user.send_message() takes two arguments. The first is the user to receive the messages and the second is the actual message. So, your code is accurate. However, you shouldn't be receiving a captcha all the time. Try entering your reddit login info in the r.login() method. It should look something like r.login('username', 'password'). You might also see how much karma the account has. Your script may not be making it all the way through because of low karma.
You're sending the message to yourself with subject 'krumpqueen'.
Try:
r.send_message(user, 'SOME SUBJECT', msg)

Categories

Resources