Posting Supergroup Messages with Python-Telegram-Bot - python

I have been sending Telegram messages to a chat using the code below. It worked fine but the group has been upgraded to a "Supergoup" and it has stopped working. It just gives a "Chat cannot be found" error.
I have the new ID and found a suggestion that I needed to add the bot as admin for the group. Neither of these has fixed the problem.
Does anyone know what the issue is?
bot = telegram.Bot(token='xxxxxxxxxxxxxxxxxxxxxxxxxxx')
def post(message):
msg = dedent(f'''\
On subject {subject}: {message}
''')
bot.send_message('-1111111111', text=msg)

Related

Private chats in telegram. Telethon python

I started writing a bot for a private chat in Telegram. I ran into a problem.
url = "here I entered the link for the invitation"
channel = waiting for the client.get_entity(url)
Returned the error `ValueError: cannot find any object matching the 'link'
Nothing came of it.
As a result, I changed the chat type to public, created a link, inserted it into the url = ""
It worked, but how to receive and filter messages from a public chat.
Tried to get a link via
https://t.me/getmyid_bot

I keep getting same errors when trying to rerun Telethon script

so i`m trying to learn Telethon and wrote some script from tutorial. It works fine for the first time, when i have to confirm authorization.
from telethon import TelegramClient
api_id = 1234567
api_hash = ''
client = TelegramClient('anon', api_id, api_hash)
async def main():
me = await client.get_me()
print(me.username)
with client:
client.loop.run_until_complete(main())
but when i try to run it again with the same .session file i keep getting same error, which doesn`t help to understand the problem.
Server sent a very new message with ID 7144059125492611077, ignoring
Server sent a very new message with ID 7144059125964561409, ignoring
Also, program doesn`t stop running after this error.
After a couple of days searching for a fix, i just switched to pyrogram, which works perfectly fine. So if someone got the same problem I recommend just to change Telethon to something else.
Make sure your system time is correct. If the issue persists, it may be a bug you which you should report to https://github.com/LonamiWebs/Telethon/issues/new/choose. However recent versions of the library (v1.25.2) should not have this issue.

Telegram Bot Api / Python: Trying to send voice message via my 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.

Can't respond to messages in message request in fbchat

I'm trying to make a bot so that when someone sends it a message the bot sends back a joke but I've noticed this only works with people in my friends list but if random people sent to the bot then their message gets stuck in message request on messenger and even the fbchat library recognizes this and tells me in the console that there is a new pending message so is there a way to respond to those too?
Here is my code:
from fbchat import Client,ThreadLocation
class JokesBot(Client):
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.moveThread(ThreadLocation.PENDING,thread_id,thread_type)
self.markAsDelivered(thread_id, message_object.uid)
self.markAsRead(thread_id)
# If you're not the author, echo
if author_id != self.uid:
self.send(Message(text='wiso tefl hehe'), thread_id=thread_id, thread_type=thread_type)
client = JokesBot('email', 'password')
client.listen()
Bare in mind I still didn't implement the jokes so it's missing from the code
Thanks in advance
onMessage event called only when connected facebook friend sends you a message.
for unconnected facebook users(not friends) you should use another event onPendingMessage but unlike onMessage' in pending message things are getting more complex:
take a look at fbchat documentation
after the event called you should call the method: "fetchThreadList" in oreder to see the incoming message

How to send python output to telegram CHANNEL not to Group and gmail email group

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

Categories

Resources