Telegram bot creating survey (poll) - python

how do i create automatic polls on telegram with python? I need to create a poll every 3 days and I need to automate this task. Is it possible to do this with a telegram robot or via script? I can't create a vote with my robots.
I know it is possible to send messages by the robot with the following code:
import telegram
bot = telegram.Bot(token='104014195************dew')
bot.sendMessage(-3251xx743, 'hello')
or by post method:
https://core.telegram.org/bots/api#message
but I can't do it that way.
My question is how to make a poll with post method or by python
I need an example code as there is not much information on the internet

As you has tag python-telegram-bot, I guess you are using this library:
https://github.com/python-telegram-bot/python-telegram-bot
In this case you should use this method to send a poll:
https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html#telegram.Bot.sendPoll
Next step is to start your script regularly: you can do it by using crontab, for instance.

Related

How to get telegram messages without any delay from any server using the API or is there a get request that I can use?

So I want to get all the messages from the telegram servers I'm in and want to display the messages in the terminal.
I used the telethon python library but the delay is too much takes like 2 to 3 seconds to fetch the message after it has already appeared in the browser.
Thanks.
The best way I found is to use the pyrogram library, it displays messages faster than the Telegram App itself which is what I wanted.

How to use telebot module to create an inline button in bot window, which if clicked will work very similar to 'Create poll' button found in groups?

I am relatively new to python and I am trying to create a telegram bot using telebot, which will create a quiz-like game, and each user can create their own quiz-like game also. During the step by step process of creating this quiz by the user, at one point, I need them to send me a poll, just like how they create a new poll in a group. But there is no create poll button inside telegram bot as these buttons are generally found in groups and not in one-one chats.
So I need to create an inline keyboard button, which upon clicking will let the users create a poll and send it to the bot. I have gone through the documentation in github and couldn't find anything useful for this.
This similar thing is implemented by telegram's own "Quizbot". I'll attach the screenshots from that bot for clarity. Please help me identify how to implement it in my bot.
If 'Create question" button is clicked:
I am a noob to python and coding, so please help me with this problem.
edit: I can send a poll to the bot if I am using telegram desktop and not from the phone. I want to know how to enable to use it in the phone.
By using telebot we can do this. This gives us the opportunity to create poll from phone apps also.
import telebot
from telebot.types import ReplyKeyboardMarkup,KeyboardButton,
KeyboardButtonPollType,ReplyKeyboardRemove
bot=telebot.Telebot(token='your bot token')
poll_markup=ReplyKeyboardMarkup(one_time_keyboard=True)
poll_markup.add(KeyboardButton('send me a poll',
request_poll=KeyboardButtonPollType(type='quiz')))
#from my experience, only quiz type and regular type polls can be send.
remove_board=ReplyKeyboardRemove()
bot.send_message(chat_id,text,reply_markup=poll_markup)
#some other code here
#this can be used to remove the replykeyboard when you no longer need it.
bot.send_message(chat_id,text,reply_markup=remove_board)

Send a message to all users when the bot wake up

I've started tinkering with python recently and I'm on my way to create my very first telegram bot mainly for managing my Raspberry Pi and a few things connected to it. The bot is done but I would like to send a message to all the users that have already interacted with the bot when it starts, basically saying something like "I'm ready!", but I haven't been able to find any information about it.
Is there any specific method in the API already done to do this? Or should I create another file to store the chat_id from all the users and read it with python?
Thank you all for your help!! Regards!
You should have a Database that stores Chat-id(s) of Clients that interact with your bot.
If you're able to put them into a list, then the job is half done.
All you need to do is to create a "for loop", and send message to all the ID(s) in the list.
Example:
message = "Good morning"
chats =[1234447488,3748838477,4748848578,7463638488]
for chat in chats:
bot.send_message(chat_id=chat,text= "message")
In case of of clients that have deleted your bot, you may experience an error. You can catch that with try and except.
Is there any specific method in the API already done to do this?
No, there's no such method.
Or should I create another file to store the chat_id from all the users and read it with python?
Yes, you should handle it yourself. Usually user ids and another information about User <-> Bot interaction is stored in a database on a server, because it's easier for maintaining and searching.
So if you want to send a plain text message to your users in a file, you should call SendMessage method of telegram bot API for every user in your file.
You should save users in database or file.After that use for to send_message one by one to all users that you have in database or file.
Yes you should create another file or use a database to store the chat_id of all the users you interact with. You can check Telegram Bots API to see all the available commands. if there is no such command then there probably won't be any command in the wrapper you are using for python either.

How to generate information from telegram using a Python based Telegram bot and it’s API like it’s done with GetUpdate in addressbars in web browsers

Hi everyone so I’m new to developing bots for Telegram with Python and I need help being able to generate the basic get updates information. I usually can when I use the web browser, you know when you drop the telegram link with the bot token key and finish it up with the GetUpdate function in the address bar then the browser just generates a page with all the info such as chat_id, user_id, firstname,last name, etc. Yeah I want to know how to do that with Python so I can achieve the same results in the python terminal. I’m working with the telegram bot for python package so I think it’s called telegram.ext. I’m using the basic guide which involves me using the Dispatcher and Updater, I’m still learning from the guide but for now I need to know how to get that system/session info using my bot in the python terminal. Feel free to ask any questions to clarify this question, thanks.
So far I the most I can do is print out messages I’ve sent to the bot but that’s about it, it’s pretty much filtered and I don’t want that I want the raw and unorganized information like browser usually provides.
The easiest way is to use requests library:
import requests
r = requests.get('https://api.telegram.org/bot<BOT_TOKEN>/getupdates')
print(r.json())

Receive a web request with IFTTT

I am currently using IFTTT to create some automated software with Amazon Echo (Alexa). I want to use the IFTTT's Maker channel to do so.
Here's what I want the end result to be:
Command Amazon Echo (Alexa) to run a program.
Run a Python program on my computer.
I have had success in using the Trigger function of the Maker channel using a JSON request. However, there seems to be little documentation on the Action function, where IFTTT can make a web request to a URL. I have heard that webhooks may be needed to use this, again I am not sure where to get started with this.
The image below is what the Action function asks for. I know I'll need a server on my local machine or a program reading any requests sent to a public website.
If there are any libraries that would make this much easier, I would happily take recommendations, as this has been on my mind for a while..
Thank you!

Categories

Resources