This code is supposed to take up prices from 2 indexes from yahoo finance nd send them to the user when a text is sent in the chat:
import requests
import telegram
from bs4 import BeautifulSoup
def get_price(symbol):
url = f"https://finance.yahoo.com/quote/{symbol}"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
price = soup.find("span", {"data-reactid": "14"}).get_text()
return price
def check_price(update, context):
symbols = ["^DJI", "^GSPC"]
prices = [get_price(symbol) for symbol in symbols]
message = "DJI: " + prices[0] + "\n" + "GSPC: " + prices[1]
context.bot.send_message(chat_id=update.message.chat_id, text=message)
token = "TOKEN NAME"
bot = telegram.Bot(token)
updater = telegram.Updater(token, use_context=True)
dispatcher = updater.dispatcher
price_handler = CommandHandler("price", check_price)
dispatcher.add_handler(price_handler)
updater.start_polling()
`
However, it gives an error:
\stocks.py", line 21, in <module>
updater = telegram.Updater(token, use_context=True)
AttributeError: module 'telegram' has no attribute 'Updater'. Did you mean: 'Update'?
I have tried updating the telegram libruary and installing Updater, but it didn't work.
I have also tried other variations of the code:
import requests
import telegram
from telegram import Updater, InlineKeyboardButton, InlineKeyboardMarkup
def get_price(symbol):
url = f"https://finance.yahoo.com/quote/{symbol}"
page = requests.get(url)
text = page.text
start = text.find("data-reactid=\"50\"") + len("data-reactid=\"50\"") + 1
end = text.find("</span>", start)
price = text[start:end].strip()
return price
def main():
token = "BOT_TOKEN_GOES_HERE"
updater = Updater(token)
dp = updater.dispatcher
def handle_text_message(update, context):
chat_id = update.message.chat_id
message = update.message.text
dji_price = get_price("%5EDJI")
sp_price = get_price("%5EGSPC")
bot.send_message(chat_id=chat_id, text=f"DJI Price: {dji_price}\nSP Price: {sp_price}")
dp.add_handler(MessageHandler(Filters.text, handle_text_message))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
But it gives a similar error due to Updater:
stocks.py", line 3, in <module>
from telegram import Updater, InlineKeyboardButton, InlineKeyboardMarkup
ImportError: cannot import name 'Updater' from 'telegram' (C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\__init__.py)
There is no class Updater in the telegram package. There is telegram.Update and there is telegram.ext.Updater both of which have very litte to do with each other. The tutorial explains this in more detail.
Please also note that you're using a version <= 13.15 of the python-telegram-bot libray. That version is no longer being supported. If you're new to python-telegram-bot, I recommend to get started with v20.
Disclaimer: I'm currently the maintainer of python-telegram-bot
Related
I want to create a button that when I click on it will send the file to me in the telegram.
from config import TOKEN
from telegram import MessageEntity, ReplyKeyboardMarkup, Update
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater
from telegram.ext import CallbackContext
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
from telegram.ext import CallbackQueryHandler
import logging
import urllib.request , json
updater = Updater(token= TOKEN, use_context= True)
dispathcer = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
download_url_link = 'https://saavn.me/songs?link='
home_page = 'https://saavn.me/home'
def readjson(url):
req = urllib.request.urlopen(url)
return json.loads(req.read().decode())
def start(update: Update, context: CallbackContext):
text= "Welcome #{username}".format(username = update.effective_user.username)
update.message.reply_text(text)
def download(update: Update, context: CallbackContext):
x = update.message.parse_entities(types = MessageEntity.URL)
msg = update.message.reply_text("Working on it...")
for i in x:
try:
rjson = read_json(end_point_link + x[i])
name = rjson["results"]['name']
download_link = rjson["results"]["downloadUrl"][4]["link"]
quality = rjson["results"]["downloadUrl"][4]["quality"]
msg.delete()
dl = update.message.reply_document(download_link, caption="Here is {} and quality is {}".format(name, quality))
keyboard = [
[
InlineKeyboardButton('128KB', callback_data=dl),
InlineKeyboardButton('320kb', callback_data=dl),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("Please choose:",
reply_markup=reply_markup)
except:
continue
if 'error' in rjson:
continue
return
msg.edit_text("I can't fetch from that url. try again!")
dont work inlinekeyboard and just send for me link download and dont show my buttons
start_handler = CommandHandler('start', start)
download_hundler = MessageHandler(Filters.entity(MessageEntity.URL), download)
button_handler = CallbackQueryHandler(button)
dispathcer.add_handler(start_handler)
dispathcer.add_handler(download_hundler)
dispathcer.add_handler(button_handler)
updater.start_polling()
I run the program and the file is sent to me but it does not ask me 128 or 320 and it automatically sends the file why?
In the keyboard section, you should have a unique callback data for each button. for example:
keyboard = [
[
InlineKeyboardButton('128KB', callback_data='dl-128'),
InlineKeyboardButton('320kb', callback_data='dl-320'),
]
]
Then you should create a function to handle callback query:
def button(update: Update, context: CallbackContext):
data = update.callback_query.data
quality = int(data.split('-')[1])
if quality == 128:
# send 128kb file
elif quality == 320:
# send 320kb file
after that, you should create a callback handler with your preferred pattern. For this example it would be:
button_handler = CallbackQueryHandler(button, pattern='dl-')
dispathcer.add_handler(button_handler)
i am searching for a solution. I hava a bot which post by /start command some stuff i need. But id like that the bot do this in a defined time, maybe every 6h.
Hope someone could help me.
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
from tracker import get_prices
telegram_bot_token = "xxx-xxx"
updater = Updater(token=telegram_bot_token, use_context=True)
dispatcher = updater.dispatcher
def start(update, context):
chat_id = update.effective_chat.id
message = ""
crypto_data = get_prices()
for i in crypto_data:
coin = crypto_data[i]["coin"]
price = crypto_data[i]["price"]
change_day = crypto_data[i]["change_day"]
change_hour = crypto_data[i]["change_hour"]
message += f"Coin: {coin}\nPrice: ${price:,.2f}\nHour Change: {change_hour:.3f}%\nDay Change: {change_day:.3f}%\n\n"
context.bot.send_message(chat_id=chat_id, text=message)
dispatcher.add_handler(CommandHandler("start", start))
updater.start_polling()
I was testing something an could add an timer, but is there a way to loop the programm with while True: ?
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
from tracker import get_prices
import threading
import time
telegram_bot_token = "xxx:xxx"
updater = Updater(token=telegram_bot_token, use_context=True)
dispatcher = updater.dispatcher
def message_timer(update, context):
time.sleep(10)
chat_id = update.effective_chat.id
message = ""
crypto_data = get_prices()
for i in crypto_data:
coin = crypto_data[i]["coin"]
price = crypto_data[i]["price"]
change_day = crypto_data[i]["change_day"]
change_hour = crypto_data[i]["change_hour"]
message += f"Coin: {coin}\nPrice: ${price:,.2f}\nHour Change: {change_hour:.3f}%\nDay Change: {change_day:.3f}%\n\n"
context.bot.send_message(chat_id=chat_id, text=message)
dispatcher.add_handler(CommandHandler("start", message_timer))
updater.start_polling()
So i found this piece of code somewhere on stackoverflow:
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
from telethon.tl.functions.channels import InviteToChannelRequest
import sys
import csv
import traceback
import time
from datetime import datetime
api_id = 123456789 #Enter Your 7 Digit Telegram API ID.
api_hash = '123456789' #Enter Yor 32 Character API Hash.
phone = '123456789'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code recieved to your Telegram messenger: '))
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
for chat in chats:
try:
if chat.megagroup== True:
groups.append(chat)
except:
continue
print('Choose a group to scrape members from:')
i=0
for g in groups:
print(str(i) + '- ' + g.title)
i+=1
g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]
print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)
print('Saving In file...')
with open("Scraped.csv","w",encoding='UTF-8') as f:
writer = csv.writer(f,delimiter=",",lineterminator="\n")
writer.writerow(['username','user id', 'access hash','name','group', 'group id','last seen'])
for user in all_participants:
accept=True
try:
lastDate=user.status.was_online
num_months = (datetime.now().year - lastDate.year) * 12 + (datetime.now().month - lastDate.month)
if(num_months>1):
accept=False
except:
continue
if (accept) :
if user.username:
username= user.username
else:
username= ""
if user.first_name:
first_name= user.first_name
else:
first_name= ""
if user.last_name:
last_name= user.last_name
else:
last_name= ""
name= (first_name + ' ' + last_name).strip()
writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id,user.status])
print('Members scraped successfully.')
And this pretty much scrapes online & recently active members, how would i change this to ONLY scrape online members? I tried looking into the telethon docs but i don't seem to understand...
I'm not sure where else to ask for help regarding this issue so here i am...
Any sort of help is highly appreciated!
Thank you.
Scraping ONLY online members is not possible.
Try something like this:
import telethon
from telethon.sync import TelegramClient, events
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import InputChannel
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.functions.channels import GetFullChannelRequest
api_id = 123567
api_hash = "dsaopdas2131"
client = TelegramClient("RDJR", api_id, api_hash)
#client.on(events.NewMessage())
async def handler(event):
chat_id = event.message.peer_id.channel_id
offset = 0
limit = 200
my_filter = ChannelParticipantsSearch('')
channel = await client(GetFullChannelRequest(chat_id))
participants = await client(GetParticipantsRequest(channel=chat_id, filter=my_filter, offset=offset, limit=limit, hash=0))
for x in participants.users:
print(x.status)
with client as client:
print("USER_BOT ONLINE!")
client.run_until_disconnected()
You need to get all of the participants of a group/channel, then iterate them to print the status.
The output would be something like this:
User online:
UserStatusOnline(expires=datetime.datetime(2021, 7, 6, 21, 6, 22, tzinfo=datetime.timezone.utc))
Or:
None
User offline:
UserStatusOffline(was_online=datetime.datetime(2021, 7, 6, 18, 19, 35, tzinfo=datetime.timezone.utc))
I have some code:
master = InlineKeyboardMarkup()
master.add(InlineKeyboardButton(text='7(800)555-35-35', url='tel:+78005553535'),
InlineKeyboardButton(text='8(800)555-35-35', url='tel:+88005553535'))
But when I try to summon this keyboard I have an error:
aiogram.utils.exceptions.BadRequest: Wrong http url
You need to write the code as follows:
main.py
from aiogram
import Bot, Dispatcher, executor, types
import keyboards as kb
bot = Bot(token = 'BOT_TOKEN')
dp = Dispatcher(bot)
#dp.message_handler(commands = ['inline'])
async def show_items(message: types.Message):
await message.answer('It is buttons', reply_markup = kb.start_keyboard)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates = True)
keybords.py
from aiogram.types
import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
inline_keyboard = InlineKeyboardButton(text='7(800)555-35-35', url = 'tel:+78005553535'),
InlineKeyboardButton(text='8(800)555-35-35', url = 'tel:+88005553535')
start_keyboard = ReplyKeyboardMarkup(resize_keyboard = True).add(inline_keyboard)
How can I get last posts and messages from all channel and groups in my Telegram account using "telethon".
At first you should get your "api_id" and "api_hash" from Telegram API.
then you should use following code to connect to your Telegram account.
from telethon.sync import TelegramClient
from telethon.errors import SessionPasswordNeededError
# Create the client and connect
client = TelegramClient(you_username, api_id, api_hash)
client.start()
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
client.send_code_request(your_phone)
try:
client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
client.sign_in(password=input('Password: '))
In the next step we define a def to get last message of an entity (like channel or group):
from telethon.tl.functions.messages import GetHistoryRequest
def get_entity_data(entity_id, limit):
entity = client.get_entity(entity_id)
today = datetime.datetime.today()
# y = today - datetime.timedelta(days=1)
posts = client(GetHistoryRequest(
peer=entity,
limit=limit,
offset_date=None,
offset_id=0,
max_id=0,
min_id=0,
add_offset=0,
hash=0))
messages = []
for message in posts.messages:
messages.append(message.message)
return messages
This def gets id of a channel or group and limit on number of messages that we want to get from any channels and groups and returns the messages.
Then you should get all groups and channels using this code:
from telethon.tl.functions.messages import GetDialogsRequest
result = client(GetDialogsRequest(
offset_date=None,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=100,
hash=0))
entities = result.chats
And in last step you should iterate on entities and get last messages(for example 10 in following code)
for entity in entities:
title = entity.title
messages = get_entity_data(entity.id, 10)
print(title + ' :')
print(messages)
print('#######')