Reputation: 21
I'm just starting to discover how to build a bot with python. I'm trying to send a message at certain time. Seems that the callback_minute function won't accept more than one arguments.I read a lot of example, I read the documentation regarding run_repeating function but I can't fix this issue...
my target is to get the chat_id dinamically. Someone can help me?
import logging
import sys
import requests
import random
import datetime
import telegram.ext
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import Updater, Filters, CommandHandler, MessageHandler, CallbackQueryHandler, ConversationHandler, CallbackContext
from lib import utils
class MonoBot:
def __init__(self):
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
# define a command callback function
def start(self, update, context):
#a = utils.search(self, update.message.from_user)
#context.bot.send_message(chat_id=update.message.chat_id, text=update.message.text + a)
update.message.reply_text("write /lista top know what u can do...")
def echo(self, update, context):
context.bot.send_message(chat_id=update.message.chat_id, text=update.message.text + " stuff")
def option(self, update, context):
button = [
[InlineKeyboardButton("text 1", callback_data="1")],
[InlineKeyboardButton("text 2", callback_data="2")],
[InlineKeyboardButton("text 3", callback_data="3")],
]
reply_markup = InlineKeyboardMarkup(button)
context.bot.send_message(chat_id=update.message.chat_id, text="scegli..", reply_markup=reply_markup)
def button(self, update, context):
query = update.callback_query
# context.bot.send_message(chat_id=query.message.chat_id, text="vitto " + query.data)
# use this below in order to hide option
context.bot.edit_message_text(chat_id=query.message.chat_id, text="vitto " + query.data,
message_id=query.message.message_id)
def get_location(self, update, context):
button = [[KeyboardButton("share location", request_location=True)]]
reply_markup = ReplyKeyboardMarkup(button)
context.bot.send_message(chat_id=update.message.chat_id, text="share location?", reply_markup=reply_markup)
def location(self, update, context):
lat = update.message.location.latitude
lon = update.message.location.longitude
context.bot.send_message(chat_id=update.message.chat_id, text="lat:" + str(lat) + " lon:" + str(lon),
reply_markup=ReplyKeyboardRemove())
#conversazione
def cancel(self, update: Update, context: CallbackContext) -> int:
print(update.message.from_user)
update.message.reply_text("by by :)", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def cerca(self, update: Update, context: CallbackContext) -> int:
print("-"*40)
print(update.message.text)
word = update.message.text.split(" ")[1]
print("-"*40)
update.message.reply_text('https://www.google.com/search?q='+ word)
#return ConversationHandler.END
def lista(self, update: Update, context: CallbackContext) -> int:
update.message.reply_text("qua metto l\'elenco dei comandi che espongo...tipo\n- /cercaGOOGLE\n- /altrocomando")
print("-" * 40)
print(update.message)
print("-" * 40)
return CERCA
def reply(self, update: Update, context: CallbackContext) -> int:
user_input = update.message.text
update.message.reply_text(utils.search(self, user_input))
#return ConversationHandler.END
# i'm trying to send a message at certain time...
def callback_minute(self, update, context):
#-1001198785547
context.bot.send_message(chat_id=update.effective_chat.id, text="tempoooooo")
if __name__ == "__main__":
bot = MonoBot()
# first of all take token in order to authenticate
# check new messages --> polling
updater = Updater(token=sys.argv[1])
# allows to register handler --> command, text, video, audio, ...
dispatcher = updater.dispatcher
# CORE <<-----------
# create a command headler and command heandler to dispatcher ###ORDER LIST IS IMPORTANT!!
updater.dispatcher.add_handler(CommandHandler('start', bot.start))
updater.dispatcher.add_handler(CommandHandler('option', bot.option))
updater.dispatcher.add_handler(CommandHandler('location', bot.get_location))
updater.dispatcher.add_handler(MessageHandler(Filters.location, bot.location))
updater.dispatcher.add_handler(CallbackQueryHandler(bot.button))
#updater.dispatcher.add_handler(MessageHandler(Filters.text, bot.reply))
# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
word, cmd, LOCATION, CERCA = range(4)
conv_handler = ConversationHandler(
entry_points=[CommandHandler('lista', bot.lista)],
states={
CERCA: [MessageHandler(filters=Filters.text, callback=bot.reply)],
},
fallbacks=[CommandHandler('exit', bot.cancel)],
)
dispatcher.add_handler(conv_handler)
# from here time issue..
j = updater.job_queue
j.run_repeating(bot.callback_minute, interval=60, first=10)
#t = datetime.time(6, 15, 00, 000000)
#j.run_daily(bot.timer, t, days=(0, 1, 2, 3, 4, 5, 6), context=None, name=None)
#updater.dispatcher.add_handler(MessageHandler(Filters.text, bot.echo))
# start polling
updater.start_polling()
#updater.idle() # ???
I get below error:
2021-02-15 22:21:04,527 - telegram.ext.dispatcher - DEBUG - Setting singleton dispatcher as <telegram.ext.dispatcher.Dispatcher object at 0x7f8719922040>
Traceback (most recent call last):
File "/home/vitto/PycharmProjects/monopattino/mono_bot.py", line 118, in <module>
j.run_repeating(bot.callback_minute, interval=60, first=10)
File "/home/vitto/PycharmProjects/monopattino/mono_env/lib/python3.8/site-packages/telegram/ext/jobqueue.py", line 291, in run_repeating
j = self.scheduler.add_job(
File "/home/vitto/PycharmProjects/monopattino/mono_env/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 434, in add_job
job = Job(self, **job_kwargs)
File "/home/vitto/PycharmProjects/monopattino/mono_env/lib/python3.8/site-packages/apscheduler/job.py", line 49, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "/home/vitto/PycharmProjects/monopattino/mono_env/lib/python3.8/site-packages/apscheduler/job.py", line 180, in _modify
check_callable_args(func, args, kwargs)
File "/home/vitto/PycharmProjects/monopattino/mono_env/lib/python3.8/site-packages/apscheduler/util.py", line 401, in check_callable_args
raise ValueError('The following arguments have not been supplied: %s' %
ValueError: The following arguments have not been supplied: context
Upvotes: 2
Views: 1362
Reputation: 75
Try to remove update
from callback_minute()
like this:
def callback_minute(self, context):
#-1001198785547
context.bot.send_message(
chat_id=update.effective_chat.id,
text='tempoooooo'
)
There is no update
delivered when executing a job.
Upvotes: 1