I already tried search this in Google. But I couldn't find the answer. I want to bot send user's location. Here is the final version(It doesn't work):
markup_request = ReplyKeyboardMarkup(resize_keyboard=True).add(
KeyboardButton('Send your contact ☎️', request_contact=True)
).add(
KeyboardButton('Send your location 🗺️', request_location=True)
)
#dp.message_handler(commands=['location'])
async def message_for_start(message: types.Message):
await message.reply('Location', reply_markup=markup_request)
Firstly, check your imports.
Secondly, use other methods, instead of try <send_message> or . Reason is updating of aiogram library.
Related
I'm trying to code a python anti-advertising telegram bot and basically want to check when a user mentions another user in their message it will check rather the mentioned # is a user or a group.
I want the members to still be able to mention other usernames like ex: #username, but when they mention a group #testinggroup, it will be flagged and removed, here's all I have right now.
from telegram import *
from telegram.ext import *
from requests import *
import re
updater = Updater(token="bot_token", use_context=True)
dispatcher = updater.dispatcher
username = re.compile(r'^#[A-Za-z][A-Za-z0-9_]{4,30}$', re.IGNORECASE)
def messageHandler(update: Update, context: CallbackContext):
#chat_member = context.bot.get_chat_member(update.effective_chat.id, update.message.from_user.id)
#if chat_member.status == 'creator' or chat_member.status == 'administrator' or chat_member.user.is_bot:
# return
if username.search(update.message.text):
return #NEED HELP HERE
dispatcher.add_handler(MessageHandler(Filters.text, messageHandler))
updater.start_polling()
updater.idle()
I've tried everything,
tried removing messages with # in them, but I want users to still be able to mention each other.
tried using regex to detect usernames, but once again want users to be able to mention each other.
Checking if a message contains a mention
Note that you can use Filters.entity("mention") to filter for messages that contain a mention in the form #username - that way you don't have to rely on regex.
Moreover, you can retrieve the usernames via update.effective_message.parse_entities(["mention"]).
Note that MessageEntity.user is not available for entities of type "mention".
Checking if a username links to a private chat or something else
As implied by Lorenzo, you can use get_chat for this. Note that get_chat should return the Chat object in case the username links to a public group or channel. However, for private chats, get_chat only works with the user id and not the username, so get_chat("#privateusername") will raise a "Chat not found" exception rather than returning a Chat object.
Note: The links point to the PTB v13.15 docs. The current stable version is v20.0, which contains significant breaking changes compared to v13.x.
Disclaimer: I'm currently the maintainer of python-telegram-bot.
so im playing a little bit around with my telegram bot, now im already able to send normal messages and pictures. But now i want to be able to send pre recorded voice messages as an answer. In the documentation it says to use send_voice for this matter and it recommends using the file_id of a file thats already on the telegram servers. So thats what i did i send a voice message to the #RawDataBot and he returned to me the file_id of the voice message.
Problem is: When i try to trigger it i get the following error: telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong file identifier/HTTP URL specified
Any ideas here on what i might be doing wrong? Here is the relevant code:
import telebot
API_KEY = <MY_API_KEY>
bot = telebot.TeleBot(API_KEY)
#start
#bot.message_handler(commands=['start'])
def start (message):
bot.send_message(message.chat.id,Textstart)
#bot.message_handler(commands=['pic'])
def start (photo):
bot.send_photo(photo.chat.id, "https://de.wikipedia.org/wiki/Zeus#/media/Datei:Zeus_Otricoli_Pio-Clementino_Inv257.jpg")
#here is the part where he is supposed to send the voice message if someone types in /audio
#bot.message_handler(commands=['audio'])
def start (voice):
bot.send_voice(voice.chat.id,"AwACAgIAAxkBAAEWjl5i5bjyudWAM9IISKWhE1Gjs5ntQgACLx8AApcNKEv97pVasPhBoCkE",)
bot.polling()
There are multiple reasons, but I think it is because of these:
file_id is unique for each individual bot and can't be transferred from one bot to another.
file_id uniquely identifies a file, but a file can have different valid file_ids even for the same bot.
See here.
#dp.message_handler(content_types=types.ContentType.DOCUMENT)
async def scan_message(file: types.File):
print("downloading document")
file_path = file.file_path
destination = r"C:\users\aleks\PycharmProjects\pythonProject\file.pdf"
destination_file = bot.download_file(file_path, destination)
print("success")
I would like to be able to download a file (in this case it is pdf) that was sent to my bot by the user. But the problem is that the bot doesn't even recognize a file (when I send a file, it doesn't even print "downloading document")
TL;DR
I hope you're using aiogram v2.x.
Make sure that's the only handler for types.ContentType.DOCUMENT without filters and your bot can get needed updates, then:
#dp.message_handler(content_types=types.ContentType.DOCUMENT)
async def scan_message(message: types.Message):
print("downloading document")
destination = r"C:\users\aleks\PycharmProjects\pythonProject\file.pdf"
await message.document.download(destination)
print("success")
Detailed
Everything described below applies to stable aiogram v2.x.
aiogram.Dispatcher class parses raw telegram Update and sends it parsed and unpacked to corresponding handler, e.g. dp.message_handler will receive aiogram.types.Message for updates containing message, dp.callback_query_handler will receive aiogram.types.CallbackQuery for updates containing callback_query and so on. In your case you are expecting aiogram.types.File, which is wrong. Then dispatcher checks filters and calls corresponding handlers in registration order and stops dispatching if any handler was called.
Consider the following example:
# Some code omitted in favor of brievity
#dp.message_handler()
async def handle1(msg: types.Message):
print("handled 1")
#dp.message_handler()
async def handle2(msg: types.Message):
print("handled 2")
You send any text message then look at bot's console. There will only "handled 1" be printed because it was the first matching handler and only one handler is called.
Bots have so-called "Privacy mode" in group chats, so not every message comes to your bot in a group. This is not the case in private (direct) messages, so it's better to test your bot in private. You can read more about Privacy mode in official Bot API docs: https://core.telegram.org/bots#privacy-mode.
It's better and more readable to use shortcuts which are contained in each class, e.g. you can .reply() or .answer() to a aiogram.types.Message, which is a shortcut for aiogram.Bot.send_message(). Same with downloading file, you can just use .download() on aiogram.types.Document, aiogram.types.PhotoSize and so on. You can find more downloadable types by looking for classes that implement aiogram.types.mixins.Downloadable.
All .download() methods return destination where the file was saved. If you pass your own optional destination as the first argument to the method, it'll be useless to get it back as you already know it.
So, after these modifications, you'll get the code as in TL;DR section.
You did not specify version of aiogram. I guess it is 2.x.
All asynchronous function registered, as message handler must take the first positional parameter received message. If you like type hints you must to specify your function as
async def scan_message(message: types.Message):
for download your document you need just:
destination_file = await message.document.download(destination)
so correct handler for download document is:
async def scan_message(message: types.Message):
print("downloading document")
destination = r"C:\users\aleks\PycharmProjects\pythonProject\file.pdf"
destination_file = await message.document.download(destination)
print("success")
Hi I have a program in python that generates results every one hour. The result can be of anything.This program will run in local machine or in the virtual private network.
I have two requirements
1. Send this python generated result to one telegram group [Group name "ourworld"](created by me) automatically without user intervention . (I have desktop telegram client running or web.telegram.org running in the same system)
Send this result to gmail group email ID.
what are the methods available to achieve this requirement .Is there any working code available to do this job .Please share the info and details.
Edit:
The Issue that i am facing :
1.created a Bot using BotFather.
2.Adding this Bot to my Group ,Here i get an error could not add an member So added the Bot as admin in the group
3.Token of the BOT noted down.
4. Trying to get ChatId
in this forum (https://web.telegram.org/#/im?p=g154513121) someone says number after p=g is the chartid ,In my case there is no
number it shows #testingname like this.
Using this approach trying to get the Chat ID https://api.telegram.org/bot738909732:AAE-2l7xAAlYHBXprK_a_bex09g9DMZbbDI/getme
so here 738909732 become a chat Id (in this case why we need seperate
call for the chart id)
here it is true as response coming! Here the chat id is the ID of the my "testingname" no chart id generated for the group.
6.Now packing the URL to see the response i am getting this error.
https://api.telegram.org/bot738909732:AAE-2l7xAAlYHBXprK_a_bex09g9DMZbbDI/sendMessage?chat_id=738909732&text=testing
the output if i run this in browser
{"ok":false,"error_code":400,"description":"Bad Request: chat not
found"} {"ok":false,"error_code":403,"description":"Forbidden: bot
can't send messages to bots"}
How to resolve this issue and make the BOT working .why i am not able to add BOT to my group that says error "Cant Add user"
How to make this telegram group working.
Note : Using BotFather BOT created
In case for sending message to telegram group the above method provided by bipin_s works where the chat_id = -xxxxxx is used.This is correct id followed by - symbol to be used.
For Posting message in the "telegram channel " a minor change needs to be done in the URL.The URL must be framed as below .
url = "https://api.telegram.org/botTokenID/sendMessage?chat_id=#yourChannelChatID&text=message"
Replace the TokenID with your BOT tokenID replace the yourChannelChatID with your channel chart id.Please note that the channel id is not negative id.
the code will look like this
import request
url = "https://api.telegram.org/botXyz:wwwwDFSJSJSAX/sendMessage?chat_id=#telechanneltesting&text=message"
requests.post(url)
Here the "message" as in the URL it will be posted in telegram channel.
How to get channel id ?
Go to https://web.telegram.org/#/im in browser ,after login now search your "channel".Now in the browser address bar you will one link like https://web.telegram.org/#/im?p=#llliopppppsssssr
p=#llliopppppsssssr after the equal symbol what comes is channel chat ID.
to send message to the telegram "group" without any user intervention , you require a telegram bot. create one using telegram bot-father. take a look at this link. also note the token while creating the bot. This method will work only for telegram Group .Telegram channel another method to be followed which MarxBabu answered below in his answers post.
import requests
# telegram url
url = "https://api.telegram.org/bot<Token>"
def send_mess(text):
params = {'chat_id':-xxxxxxx, 'text': text}
response = requests.post(url + 'sendMessage', data=params)
return response
send_mess(result_you_want_to_send)
to get the chat_id follow the steps mentioned here. note: group chat id's always starts with '-' . e.g. of group chat_id -356163873. A token and chat_id is only what you require to send message to telegram group.
for sending group emails you have to search more as i do not know much
I have it running on a Raspberry pi. You must look for botfather to get your telegram token.
import telepot
from telepot.loop import MessageLoop
telegram_token = 'xxxx:xxxxxx'
user = 4444444
bot = telepot.Bot(telegram_token)
bot.sendMessage(user, 'Hey!')
For configuring gmail I don't have something here right now...
You can send emails in python through SMTP or using the Mailgun Api
I personally prefer using Mailgun as it is easier to setup and sending an email is as easy as sending a post request to mailgun.
You can get the free version of the api and add your group email id to the sandbox (to avoid spam) and then use requests to post an email with the given api token
I'm trying to automate interaction with a website that uses websockets. In theory all I need to do is get authenticated with steam and then send 1 piece of data, but I'm having trouble getting authenticated.
I manage to get the connection set up. But after I send my steam token (replicating what I see happens in the devtools of my browser), it replies that I am not logged in.
Below is the code I currently have. Locally it doesn't work at all. But on repl.it (an online editor) it has the effect it's supposed to have but the websocket still replies that I am not logged in.
The code:
import asyncio
import websockets
import json
import time
import sys
token = "YOUR_STEAM_TOKEN_HERE"
adress = 'wss://eu.pug.champ.gg/socket.io/?EIO=3&transport=websocket'
authtemplate = '42["authenticate",{{"token":{0}}}]'
rolesupdatetemplate = '42["updateAvailability",{0}]'
async def add(roles):
async with websockets.connect(adress) as websocket:
await websocket.send(authtemplate.format(token))
roles_string = json.dumps(roles)
await websocket.send(rolesupdatetemplate.format(roles_string))
asyncio.get_event_loop().run_until_complete(add({'roles': ['roamer', 'pocket'], 'captain': False})
)
Some extra info:
When on the site normally, the websocket I'm connected to according to the developer tools has an extra argument compared to the one I use in my code. The extra argument being: sid=....
However, adding this manually gives me error code 400.
I've tried looking up how to do this but I can't find the answer anywhere. Do I need to send cookies?
Do I need to get the websocket address in another way rather than manually?
What am I missing?