LoginSignup
UDs
@UDs

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

python TypeError: 'ChannelParticipants' object is not subscriptable

解決したいこと

pythonのtelegram APIを利用できるTelethonというライブラリを使用ている時に起きたエラーです。

メッセージ送信を実行すると5人ほど送信ができたのちにエラーが発生しました。
解決方法を教えて下さい。

main.py(self,data_set,group_key):
を呼び出してgroup_key=str型の変数(引数)を使用しているが、for文を利用するとなぜか途中でエラーが発生します。

発生している問題・エラー

TypeError: 'ChannelParticipants' object is not subscriptable

該当するソースコード


def SetData(self):
        uni_id = self.df_member["user id"].unique()
        selectKey = uni_id.tolist()
        uni_id = self.df_member[self.df_member["user id"].isin(selectKey)]

        groupSet = uni_id["group名"].unique()
        for groupKey in groupSet:
            print(f"グループ名{groupKey}に送信します。")
            dataSet = uni_id[uni_id["group名"] == groupKey]
            self.main(dataSet, groupKey)

def main(self, data_set, group_key):

        for dialog in self.client.iter_dialogs():
            print(dialog.name, 'has ID', dialog.id)
     count = 0
        for i in data_set["user id"]:
            if count % 10 == 0:
                time.sleep(10)
                print("待機中")

            time.sleep(0.1)
            self.client.get_participants(group_key)
            self.client.send_message(int(i), 'hi, Are you interested in poker?')
            count += 1



自分で試したこと

self.client.get_participants(group_key)
の処理に時間がかかっている可能性を考慮し待機時間を設定0.5秒

print(type(group_key))でstring型と確認。

変数に何か入れ直してるわけではなく使い回している時のエラーで困っていたので質問しました。

是非解決方法を教えていただけると助かります。

        async def main(self, data_set, group_key):

        async for dialog in self.client.iter_dialogs():
            print(dialog.name, 'has ID', dialog.id)

        print(type(group_key))
        print(group_key)

        count = 1
        async for i in data_set["user id"]:
            print(str(group_key) + "###" + str(count))

            await self.client.get_participants(group_key, aggressive=False, limit=2000)
            await self.client.send_message(int(i), 'today is happy? ')

    def test(self):
        uni_id = self.df_member["user id"].unique()
        selectKey = uni_id.tolist()
        uni_id = self.df_member[self.df_member["user id"].isin(selectKey)]

        groupSet = uni_id["group名"].unique()
        for groupKey in groupSet:
            print(f"グループ名{groupKey}に送信します。")
            dataSet = uni_id[uni_id["group名"] == groupKey]
            print(dataSet)
            loop = asyncio.get_event_loop()

            loop.run_until_complete(self.main(dataSet, groupKey))

    def ask_exit():
        for task in asyncio.Task.all_tasks():
            task.cancel()
        asyncio.get_event_loop().stop()


if __name__ == '__main__':
    telegram = Scraper()
    telegram.connect()

    telegram.getGroups()
    telegram.test()

発生している問題・エラー

プロセスは終了コード 1 で終了しました

0

1Answer

エラーメッセージでググってみると

await client.get_participants(group, aggressive=False, limit=2000) とかすればいいという回答が見つかりますね。
await なので asyncio を使って待ち合わせが必要なのかも。

0

Comments

  1. @UDs

    Questioner
    aggressive=False
    これっぽいですね!!

    別の場所でエラー出た時(同じエラーだったか忘れました💦)待機処理のためにこちらをfalseにする必要があった気がします。

    帰宅後に試してみます。
  2. @UDs

    Questioner
    変わらず…

    待ち合わせがうまくできないのです
  3. 「python asyncio」で検索してasync/awaitの使い方を調べて、await してみてはいかがでしょうか?
  4. @UDs

    Questioner
    色々調べてみましたが、うまくいきませんでした。

    追記でコードを掲載したので教えていただけると幸いです。
  5. asyncの中ではtime.sleepはご法度
  6. @UDs

    Questioner
    ありがとうございます。
    time.sleep を外して実行してみたところ、の部分に問題があるのではないかと考えました


    async for i in data_set["user id"]:
    プロセスは終了コード 1 で終了しました

    となり下に変えると同様のエラーのままです
    for i in data_set["user id"]:

    TypeError: 'ChannelParticipants' object is not subscriptable

Your answer might help someone💌